Skip to content

Commit b527f31

Browse files
authored
(DOCSP-12163): Write Phase 2 Tutorial Text (Web) (#506)
* web annotations and tutorial text * minor indentation fix * removed doubled content
1 parent f26087e commit b527f31

13 files changed

+300
-330
lines changed

source/includes/steps-task-tracker-web-apollo.yaml

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,30 @@ title: Instantiate an ApolloClient
22
ref: instantiate-an-apolloclient
33
level: 4
44
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:
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
---
2314
title: Authenticate GraphQL Requests
2415
ref: authenticate-graph-ql-requests
2516
level: 4
2617
content: |
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
...

source/includes/steps-task-tracker-web-realmapp.yaml

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@ ref: create-a-realm-app-client
33
level: 4
44
content: |
55
The app client is the primary interface to your Realm app from the SDK. In
6-
``src/realm/RealmApp.tsx``, add the following code immediately below the
7-
imports at the top of the file to create the app client:
6+
``src/App.js``, replace ``"TODO"`` with your Realm App ID:
87
9-
.. code-block:: typescript
10-
:caption: ``src/realm/RealmApp.tsx``
11-
12-
const REALM_APP_ID = "<Your App ID>"
13-
const app = new Realm.App({ id: REALM_APP_ID });
8+
.. literalinclude:: App.codeblock.appID.js
9+
:caption: ``src/App.js``
1410
1511
.. admonition:: Use Your App ID
1612
:class: note
1713
18-
Make sure to replace ``<Your App ID>`` with your app's unique
14+
Make sure to replace ``"TODO"`` with your app's unique
1915
:guilabel:`App ID`. You can find your :guilabel:`App ID` by clicking the
2016
copy button next to the name of your app in the lefthand navigation of the
2117
Realm UI.
@@ -29,31 +25,14 @@ level: 4
2925
content: |
3026
The app client provides methods that allow you to authenticate and register
3127
users through the email/password authentication provider. In
32-
``src/realm/RealmApp.tsx``, the ``RealmApp`` component wraps these functions
28+
``src/RealmApp.js``, the ``RealmAppProvider`` component wraps these functions
3329
and keeps the app client in sync with local React state.
3430
3531
These wrapping functions already have the state update calls but don't
3632
currently use the app client you created. You need to update the functions to
3733
actually call the SDK authentication and registration methods.
3834
39-
.. code-block:: typescript
40-
:caption: ``src/realm/RealmApp.tsx``
41-
:emphasize-lines: 3, 8-9, 15
42-
43-
// Let new users register an account
44-
const registerUser = async (email: string, password: string) => {
45-
return await app.emailPasswordAuth.registerUser(email, password);
46-
}
47-
48-
// Let registered users log in
49-
const logIn = async (email: string, password: string) => {
50-
const credentials = Realm.Credentials.emailPassword(email, password);
51-
await app.logIn(credentials);
52-
setUser(app.currentUser);
53-
}
54-
55-
// Let logged in users log out
56-
const logOut = async () => {
57-
await app.logOut();
58-
setUser(app.currentUser);
59-
}
35+
.. literalinclude:: final/realmAppProvider.js
36+
:caption: ``src/RealmApp.tsx``
37+
:emphasize-lines: 10, 16, 19
38+

source/includes/task-tracker-web-client-base-file-structure.rst

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
:copyable: False
33
44
src/
5-
├── index.tsx
6-
├── realm/
7-
│ ├── RealmApp.tsx
8-
│ ├── RealmApolloProvider.tsx
9-
│ ├── schema.graphql
10-
│ └── operations.graphql
5+
├── index.js
6+
├── App.js
7+
├── RealmApp.js
8+
├── TaskApp.js
119
├── components/
12-
│ ├── App.tsx
13-
│ ├── Board.tsx
14-
│ ├── LoginScreen.tsx
15-
│ ├── Navbar.tsx
16-
│ ├── TaskCard.tsx
17-
│ ├── TaskDetail.tsx
18-
│ ├── TaskLists.tsx
19-
│ └── TaskView.tsx
20-
└── hooks/
21-
├── useDraftTask.tsx
22-
├── useTaskLists.tsx
23-
└── useTasks.tsx
10+
│ ├── EditPermissionsModal.js
11+
│ ├── Loading.js
12+
│ ├── LoginScreen.js
13+
│ ├── ProjectScreen.js
14+
│ ├── SideBar.js
15+
│ ├── StatusChange.js
16+
│ ├── TaskContent.js
17+
│ ├── TaskDetailModal.js
18+
│ └── useChangeTaskStatusButton.js
19+
└── graphql/
20+
├── RealmApolloProvider.js
21+
├── useProjects.js
22+
├── useTaskMutations.js
23+
└── useTasks.js

0 commit comments

Comments
 (0)