@@ -2,65 +2,30 @@ title: Instantiate an ApolloClient
2
2
ref : instantiate-an-apolloclient
3
3
level : 4
4
4
content : |
5
- The ``RealmApolloProvider`` component calls ``createApolloClient()`` to
6
- instantiate the client. Update the function with the following code to create
7
- an ``ApolloClient`` object that connects to your app:
5
+ The ``RealmApolloProvider`` component should call
6
+ ``createRealmApolloClient()`` to instantiate the client. Update the
7
+ component with the following code to create an ``ApolloClient`` object that
8
+ connects to your app:
8
9
9
- .. code-block:: typescript
10
- :caption: ``src/realm/RealmApolloProvider.tsx``
11
-
12
- function createApolloClient(realmAppId: string, user: Realm.User): ApolloClient<NormalizedCacheObject> {
13
- const graphql_url = `https://realm.mongodb.com/api/client/v2.0/app/${realmAppId}/graphql`;
14
-
15
- return new ApolloClient({
16
- link: new HttpLink({
17
- uri: graphql_url
18
- }),
19
- cache: new InMemoryCache(),
20
- });
21
- }
10
+ .. literalinclude:: RealmApolloProvider.codeblock.realmApolloProvider.js
11
+ :caption: ``src/graphql/RealmApolloProvider.js``
12
+ :emphasize-lines: 2-6
22
13
---
23
14
title : Authenticate GraphQL Requests
24
15
ref : authenticate-graph-ql-requests
25
16
level : 4
26
17
content : |
27
- The ``createApolloClient ()`` function now instantiates a client object, but
18
+ The ``createRealmApolloClient ()`` function now instantiates a client object, but
28
19
you won't be able to run any GraphQL queries or mutations just yet. Every
29
20
GraphQL request must include an Authorization header that specifies a valid
30
21
user access token. The current client does not include any Authorization
31
22
headers, so all requests it makes will fail.
32
23
33
- To fix this, update the ``createApolloClient ()`` function to include the
24
+ To fix this, update the ``createRealmApolloClient ()`` function to include the
34
25
current user's access token in an Authorization header with every request:
35
26
36
- .. code-block :: typescript
37
- :caption: ``src/realm /RealmApolloProvider.tsx ``
38
- :emphasize-lines: 17
27
+ .. literalinclude :: RealmApolloProvider.codeblock.createRealmApolloClient.js
28
+ :caption: ``src/graphql /RealmApolloProvider.js ``
29
+ :emphasize-lines: 4, 13
39
30
40
- function createApolloClient(realmAppId: string, user: Realm.User): ApolloClient<NormalizedCacheObject> {
41
- const graphql_url = `https://realm.mongodb.com/api/client/v2.0/app/${realmAppId}/graphql`;
42
-
43
- return new ApolloClient({
44
- link: new HttpLink({
45
- uri: graphql_url,
46
- fetch: async (uri: RequestInfo, options: RequestInit) => {
47
- if (!options.headers) {
48
- options.headers = {} as Record<string, string>;
49
- }
50
- // Refreshing custom data also ensures a valid access token
51
- await user.refreshCustomData();
52
- const authenticatedOptions: RequestInit = {
53
- ...options,
54
- headers: {
55
- ...options.headers,
56
- Authorization: `Bearer ${user.accessToken}`
57
- }
58
- }
59
- return fetch(uri, authenticatedOptions);
60
- },
61
- }),
62
- cache: new InMemoryCache(),
63
- });
64
- }
65
- }
66
31
...
0 commit comments