@@ -2,65 +2,30 @@ title: Instantiate an ApolloClient
22ref : instantiate-an-apolloclient
33level : 4
44content : |
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:
89
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
2213 ---
2314title : Authenticate GraphQL Requests
2415ref : authenticate-graph-ql-requests
2516level : 4
2617content : |
27- The ``createApolloClient ()`` function now instantiates a client object, but
18+ The ``createRealmApolloClient ()`` function now instantiates a client object, but
2819 you won't be able to run any GraphQL queries or mutations just yet. Every
2920 GraphQL request must include an Authorization header that specifies a valid
3021 user access token. The current client does not include any Authorization
3122 headers, so all requests it makes will fail.
3223
33- To fix this, update the ``createApolloClient ()`` function to include the
24+ To fix this, update the ``createRealmApolloClient ()`` function to include the
3425 current user's access token in an Authorization header with every request:
3526
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
3930
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- }
6631 ...
0 commit comments