Skip to content

Commit

Permalink
Test (#1)
Browse files Browse the repository at this point in the history
- Add instruction for sending introspection query
- Refactor Apollo+OneGraph auth
  • Loading branch information
yukims19 authored Jan 16, 2019
1 parent 51fdaab commit a37aea0
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 15 deletions.
12 changes: 12 additions & 0 deletions spotify-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ Note that any hot reload on a route will fall back to the root (`/`), so `Reason

To use a port other than 8000 set the `PORT` environment variable (`PORT=8080 npm run server`).

## Send Introspection Query

To access the queries provided by OneGraph, run

`
yarn send-introspection-query https://serve.onegraph.com/dynamic?app_id=bafd4254-c229-48c2-8c53-44a01477a43e
`

This will generate a graphql_schema.json which will be used to safely type your GraphQL queries/mutations.



## Build for Production

```sh
Expand Down
21 changes: 14 additions & 7 deletions spotify-app/src/App.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions spotify-app/src/App.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ let component = ReasonReact.reducerComponent("App");

let make = _children => {
...component,
initialState: () => {
isLoggedIn: false,
auth: OneGraphAuth.newAuth(OneGraphAuth.config),
userName: None,
},
initialState: () => {isLoggedIn: false, auth: Client.auth, userName: None},
didMount: self =>
Js.Promise.(
OneGraphAuth.(
Expand All @@ -45,7 +41,14 @@ let make = _children => {
{
self.state.isLoggedIn ?
<div>
<Query />
<Query
token={
switch (OneGraphAuth.authToken(self.state.auth)) {
| Some(token) => token
| None => ""
}
}
/>
<User
auth={self.state.auth}
setLogInStatus={status => self.send(SetLogInStatus(status))}
Expand Down
15 changes: 14 additions & 1 deletion spotify-app/src/Client.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion spotify-app/src/Client.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,22 @@ let httpLink =
(),
);

let auth = OneGraphAuth.newAuth(OneGraphAuth.config);

let authLink =
ApolloLinks.createContextLink(
OneGraphAuth.(
() => {
"headers": {
"authorization": auth |> authHeaders |> authenticationHeaderGet,
},
}
),
);

let instance =
ReasonApollo.createApolloClient(~link=httpLink, ~cache=inMemoryCache, ());
ReasonApollo.createApolloClient(
~link=ApolloLinks.from([|authLink, httpLink|]),
~cache=inMemoryCache,
(),
);
166 changes: 166 additions & 0 deletions spotify-app/src/query.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions spotify-app/src/query.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module GetUsername = [%graphql
{|query findMe {
me {
gmail {
email
}
}
}|}
];

module GetUserNameQuery = ReasonApollo.CreateQuery(GetUsername);

let component = ReasonReact.statelessComponent("Query");

let make = (~token, _children) => {
...component,
didMount: self => Js.log(OneGraphAuth.authHeaders),
render: _ =>
<GetUserNameQuery>
...{
({result}) =>
switch (result) {
| Loading => <div> {ReasonReact.string("Loading")} </div>
| Error(error) =>
<div> {ReasonReact.string(error##message)} </div>
| Data(response) =>
<div>
{
ReasonReact.string(
switch (response##me##gmail) {
| Some(_gmail) => "Found email"
| None => "Not Found"
},
)
}
</div>
/* Handles a deeply nested optional response */
}
}
</GetUserNameQuery>,
};

0 comments on commit a37aea0

Please sign in to comment.