|
1 | | -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). |
| 1 | +# Todo React App powered by Dgraph |
2 | 2 |
|
3 | | -## Available Scripts |
| 3 | +Todo React App using GraphQL powered by Dgraph. |
4 | 4 |
|
5 | | -In the project directory, you can run: |
| 5 | +### GraphQL Schema Design for Dgraph |
6 | 6 |
|
7 | | -### `yarn start` |
| 7 | +At first, when we visualize the main components of Todo App, we get node as shown below: |
8 | 8 |
|
9 | | -Runs the app in the development mode.<br /> |
10 | | -Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
| 9 | + |
11 | 10 |
|
12 | | -The page will reload if you make edits.<br /> |
13 | | -You will also see any lint errors in the console. |
| 11 | +Equivalent GraphQL schema for the graph above would be as follow: |
14 | 12 |
|
15 | | -### `yarn test` |
| 13 | +```graphql |
| 14 | +type Task { |
| 15 | + ... |
| 16 | +} |
16 | 17 |
|
17 | | -Launches the test runner in the interactive watch mode.<br /> |
18 | | -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. |
| 18 | +type User { |
| 19 | + ... |
| 20 | +} |
| 21 | +``` |
19 | 22 |
|
20 | | -### `yarn build` |
| 23 | +So what do you think we should have for the Task and User nodes? |
| 24 | + |
| 25 | +We have mainly a title and a status to check if the Todo was completed. |
| 26 | +Next up, we know User has username, a unique identifier for the user, |
| 27 | +and name of the user. |
| 28 | + |
| 29 | +Apart from the fields of the nodes, we also have relationships between Task and User nodes. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +We represent that in the GraphQL schema shown below: |
| 34 | + |
| 35 | +```graphql |
| 36 | +type Task { |
| 37 | + id: ID! |
| 38 | + title: String! |
| 39 | + completed: Boolean! |
| 40 | +} |
| 41 | + |
| 42 | +type User { |
| 43 | + username: String! |
| 44 | + name: String |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +_Note: You will be required to add custom directives to support additional functionalities of Dgraph._ |
| 49 | + |
| 50 | +### Set Up the Environment |
| 51 | + |
| 52 | +Before we begin, make sure that you have [Docker](https://docs.docker.com/install/) |
| 53 | +installed on your machine. |
| 54 | + |
| 55 | +Let's begin by starting Dgraph standalone by running the command below: |
| 56 | + |
| 57 | +```bash |
| 58 | +docker run --rm -it -p 8080:8080 -v ~/dgraph:/dgraph dgraph/standalone:v20.03.1 |
| 59 | +``` |
| 60 | + |
| 61 | +Save the content below as `schema.graphql`. |
| 62 | + |
| 63 | +```graphql |
| 64 | +type Task { |
| 65 | + id: ID! |
| 66 | + title: String! @search(by: [fulltext]) |
| 67 | + completed: Boolean! @search |
| 68 | + user: User! |
| 69 | +} |
21 | 70 |
|
22 | | -Builds the app for production to the `build` folder.<br /> |
23 | | -It correctly bundles React in production mode and optimizes the build for the best performance. |
| 71 | +type User { |
| 72 | + username: String! @id @search(by: [hash]) |
| 73 | + name: String |
| 74 | + tasks: [Task] @hasInverse(field: user) |
| 75 | +} |
| 76 | +``` |
24 | 77 |
|
25 | | -The build is minified and the filenames include the hashes.<br /> |
26 | | -Your app is ready to be deployed! |
| 78 | +Let's load up the GraphQL schema file to Dgraph: |
27 | 79 |
|
28 | | -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. |
| 80 | +```bash |
| 81 | +curl -X POST localhost:8080/admin/schema --data-binary '@schema.graphql' |
| 82 | +``` |
29 | 83 |
|
30 | | -### `yarn eject` |
| 84 | +If you’ve followed the steps above correctly, there’s a GraphQL server up and running. |
| 85 | +You can access that GraphQL endpoint with any of the great GraphQL developer tools. |
| 86 | +Good choices include GraphQL Playground, Insomnia, GraphiQL and Altair. |
31 | 87 |
|
32 | | -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** |
| 88 | +Set up any of them and point it at `http://localhost:8080/graphql`. If you know lots about GraphQL, you might want to explore the schema, queries and mutations that were generated from the input. |
33 | 89 |
|
34 | | -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. |
| 90 | +### Mutating Data |
35 | 91 |
|
36 | | -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. |
| 92 | +Let's add a user and some todos in our Todo App. |
37 | 93 |
|
38 | | -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. |
| 94 | +```graphql |
| 95 | +mutation { |
| 96 | + addUser(input: [ |
| 97 | + { |
| 98 | + username: "alice@dgraph.io", |
| 99 | + name: "Alice", |
| 100 | + tasks: [ |
| 101 | + { |
| 102 | + title: "Avoid touching your face", |
| 103 | + completed: false, |
| 104 | + }, |
| 105 | + { |
| 106 | + title: "Stay safe", |
| 107 | + completed: false |
| 108 | + }, |
| 109 | + { |
| 110 | + title: "Avoid crowd", |
| 111 | + completed: true, |
| 112 | + }, |
| 113 | + { |
| 114 | + title: "Wash your hands often", |
| 115 | + completed: true |
| 116 | + } |
| 117 | + ] |
| 118 | + } |
| 119 | + ]) { |
| 120 | + user { |
| 121 | + username |
| 122 | + name |
| 123 | + tasks { |
| 124 | + id |
| 125 | + title |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
39 | 131 |
|
40 | | -## Learn More |
| 132 | +### Querying Data |
41 | 133 |
|
42 | | -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). |
| 134 | +Let's fetch the todos to list in our Todo App: |
43 | 135 |
|
44 | | -To learn React, check out the [React documentation](https://reactjs.org/). |
| 136 | +```graphql |
| 137 | +query { |
| 138 | + queryTask { |
| 139 | + id |
| 140 | + title |
| 141 | + completed |
| 142 | + user { |
| 143 | + username |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
45 | 148 |
|
46 | | -### Code Splitting |
| 149 | +Running the query above should return JSON response as shown below: |
47 | 150 |
|
48 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting |
| 151 | +```json |
| 152 | +{ |
| 153 | + "data": { |
| 154 | + "queryTask": [ |
| 155 | + { |
| 156 | + "id": "0x3", |
| 157 | + "title": "Avoid touching your face", |
| 158 | + "completed": false, |
| 159 | + "user": { |
| 160 | + "username": "alice@dgraph.io" |
| 161 | + } |
| 162 | + }, |
| 163 | + { |
| 164 | + "id": "0x4", |
| 165 | + "title": "Stay safe", |
| 166 | + "completed": false, |
| 167 | + "user": { |
| 168 | + "username": "alice@dgraph.io" |
| 169 | + } |
| 170 | + }, |
| 171 | + { |
| 172 | + "id": "0x5", |
| 173 | + "title": "Avoid crowd", |
| 174 | + "completed": true, |
| 175 | + "user": { |
| 176 | + "username": "alice@dgraph.io" |
| 177 | + } |
| 178 | + }, |
| 179 | + { |
| 180 | + "id": "0x6", |
| 181 | + "title": "Wash your hands often", |
| 182 | + "completed": true, |
| 183 | + "user": { |
| 184 | + "username": "alice@dgraph.io" |
| 185 | + } |
| 186 | + } |
| 187 | + ] |
| 188 | + } |
| 189 | +} |
| 190 | +``` |
49 | 191 |
|
50 | | -### Analyzing the Bundle Size |
| 192 | +### Querying Data with Filters |
51 | 193 |
|
52 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size |
| 194 | +Before we get into querying data with filters, we will be required |
| 195 | +to define search indexes to the specific fields. |
53 | 196 |
|
54 | | -### Making a Progressive Web App |
| 197 | +Let's say we have to run a query on the _completed_ field, for which |
| 198 | +we add `@search` directive to the field as shown in the schema below: |
55 | 199 |
|
56 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app |
| 200 | +```graphql |
| 201 | +type Task { |
| 202 | + id: ID! |
| 203 | + title: String! |
| 204 | + completed: Boolean! @search |
| 205 | +} |
| 206 | +``` |
57 | 207 |
|
58 | | -### Advanced Configuration |
| 208 | +The `@search` directive are added to support the native search indexes of **Dgraph**. |
| 209 | + |
| 210 | +Now, let's fetch all todos which are completed : |
| 211 | + |
| 212 | +```graphql |
| 213 | +query { |
| 214 | + queryTask(filter: { |
| 215 | + complete: { |
| 216 | + eq: "true" |
| 217 | + } |
| 218 | + }) { |
| 219 | + id |
| 220 | + title |
| 221 | + } |
| 222 | +} |
| 223 | +``` |
| 224 | + |
| 225 | +Next, let's say we have to run a query on the _title_ field, for which |
| 226 | +we add another `@search` directive to the field as shown in the schema below: |
| 227 | + |
| 228 | +```graph |
| 229 | +type Task { |
| 230 | + id: ID! |
| 231 | + title: String! @search(by: [fulltext]) |
| 232 | + completed: Boolean! @search |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +The `fulltext` search index provides the advanced search capability to perform equality |
| 237 | +comparision as well as matching with language specific stemming and stopwords. |
| 238 | + |
| 239 | +Now, let's try to fetch todos whose title has the word _"remember"_ : |
| 240 | + |
| 241 | +```graphql |
| 242 | +query { |
| 243 | + queryTask(filter: { |
| 244 | + title: { |
| 245 | + alloftext: "remember" |
| 246 | + } |
| 247 | + }) { |
| 248 | + id |
| 249 | + title |
| 250 | + completed |
| 251 | + } |
| 252 | +} |
| 253 | +``` |
| 254 | + |
| 255 | +## Bring up ToDo App |
| 256 | + |
| 257 | +### `yarn install` |
| 258 | + |
| 259 | +Install the dependencies needed to bring up the application. |
| 260 | + |
| 261 | +### `yarn start` |
| 262 | + |
| 263 | +Runs the Todo application.<br /> |
| 264 | +Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
| 265 | + |
| 266 | +### `yarn build` |
59 | 267 |
|
60 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration |
| 268 | +Compiles the Todo application and minifies to generate the production build. |
61 | 269 |
|
62 | | -### Deployment |
| 270 | +## Screenshots |
63 | 271 |
|
64 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/deployment |
| 272 | + |
65 | 273 |
|
66 | | -### `yarn build` fails to minify |
| 274 | + |
67 | 275 |
|
68 | | -This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify |
| 276 | +--- |
0 commit comments