Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit 948e9a8

Browse files
Adding todo component and GraphQL queries/mutations
- Added TodoApp, TodoFooter, and TodoItem components - Handler functions for queries and mutations - Added GraphQL schema file, README docs, and config template JSON - Util js files: GraphQLData, defs, history, and Utils
1 parent f3fedc6 commit 948e9a8

19 files changed

+840
-48
lines changed

todo-app-react/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
25+
# configurations
26+
config.json

todo-app-react/README.md

Lines changed: 245 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,276 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
1+
# Todo React App powered by Dgraph
22

3-
## Available Scripts
3+
Todo React App using GraphQL powered by Dgraph.
44

5-
In the project directory, you can run:
5+
### GraphQL Schema Design for Dgraph
66

7-
### `yarn start`
7+
At first, when we visualize the main components of Todo App, we get node as shown below:
88

9-
Runs the app in the development mode.<br />
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
9+
![Todo Graph](./todo-graph.png)
1110

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:
1412

15-
### `yarn test`
13+
```graphql
14+
type Task {
15+
...
16+
}
1617

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+
```
1922

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+
![Todo Graph complete](./todo-graph-2.png)
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+
}
2170

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+
```
2477

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:
2779

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+
```
2983

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.
3187

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.
3389

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
3591

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.
3793

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+
```
39131

40-
## Learn More
132+
### Querying Data
41133

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:
43135

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+
```
45148

46-
### Code Splitting
149+
Running the query above should return JSON response as shown below:
47150

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+
```
49191

50-
### Analyzing the Bundle Size
192+
### Querying Data with Filters
51193

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.
53196

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:
55199

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+
```
57207

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`
59267

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.
61269

62-
### Deployment
270+
## Screenshots
63271

64-
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
272+
![Todo App 1](./todo-1.png)
65273

66-
### `yarn build` fails to minify
274+
![Todo App 2](./todo-2.png)
67275

68-
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
276+
---

todo-app-react/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
"apollo-cache-inmemory": "^1.6.6",
1111
"apollo-client": "^2.6.10",
1212
"apollo-link-http": "^1.5.17",
13+
"classnames": "^2.2.6",
14+
"graphql-tag": "^2.10.3",
15+
"history": "^4.10.1",
1316
"react": "^16.13.1",
1417
"react-dom": "^16.13.1",
18+
"react-router-dom": "^5.2.0",
1519
"react-scripts": "3.4.1",
1620
"todomvc-app-css": "^2.3.0"
1721
},

todo-app-react/schema.graphql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type Task {
2+
id: ID!
3+
title: String! @search(by: [fulltext])
4+
completed: Boolean! @search
5+
user: User!
6+
}
7+
8+
type User {
9+
username: String! @id @search(by: [hash])
10+
name: String
11+
tasks: [Task] @hasInverse(field: user)
12+
}
13+

todo-app-react/src/App.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import React from 'react'
2+
import { Router, Switch } from "react-router-dom";
23

34
import ApolloClient from "apollo-client";
45
import { InMemoryCache } from "apollo-cache-inmemory";
56
import { ApolloProvider } from "@apollo/react-hooks";
67
import { createHttpLink } from "apollo-link-http";
78

9+
import TodoApp from "./TodoApp";
10+
import config from "./config.json";
811
import './App.css';
912

1013
const createApolloClient = () => {
1114
const httpLink = createHttpLink({
12-
uri: "http://localhost:8080/graphql",
15+
uri: config.graphqlUrl,
1316
options: {
1417
reconnect: true,
1518
},
@@ -27,12 +30,8 @@ const App = () => {
2730
<ApolloProvider client={client}>
2831
<div>
2932
<h1>todos</h1>
30-
<input
31-
className="new-todo"
32-
placeholder="What needs to be done?"
33-
autoFocus={true}
34-
/>
35-
</div>
33+
<TodoApp />
34+
</div>
3635
</ApolloProvider>
3736
);
3837
}

0 commit comments

Comments
 (0)