Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit 2f79778

Browse files
committed
feat: add example project for generated hooks
1 parent 061b8a6 commit 2f79778

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+12426
-0
lines changed

example-hooks-gen/.babelrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"plugins": [
3+
["relay", { "artifactDirectory": "./ts/__relay_artifacts__" }],
4+
"@babel/plugin-transform-runtime",
5+
"@babel/plugin-proposal-class-properties"
6+
],
7+
"presets": ["@babel/preset-typescript", "@babel/preset-react", "@babel/preset-env"]
8+
}

example-hooks-gen/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

example-hooks-gen/.watchmanconfig

Whitespace-only changes.

example-hooks-gen/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Relay TodoMVC
2+
3+
## Prerequisites
4+
5+
```
6+
yarn global add yalc
7+
```
8+
9+
And in the project root folder
10+
11+
```
12+
yarn watch
13+
```
14+
15+
## Installation
16+
17+
```
18+
yarn install
19+
```
20+
21+
## Running
22+
23+
Set up generated files:
24+
25+
```
26+
yarn update-schema
27+
yarn build
28+
```
29+
30+
Start a local server:
31+
32+
```
33+
yarn start
34+
```
35+
36+
## Developing
37+
38+
Any changes you make to files in the `ts/` directory will cause the server to
39+
automatically rebuild the app and refresh your browser.
40+
41+
If at any time you make changes to `data/schema.js`, stop the server,
42+
regenerate `data/schema.graphql`, and restart the server:
43+
44+
```
45+
yarn update-schema
46+
yarn build
47+
yarn start
48+
```
49+
50+
## License
51+
52+
This file provided by Facebook is for non-commercial testing and evaluation
53+
purposes only. Facebook reserves all rights not expressly granted.
54+
55+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
58+
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
59+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
60+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

example-hooks-gen/data/database.js

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* This file provided by Facebook is for non-commercial testing and evaluation
3+
* purposes only. Facebook reserves all rights not expressly granted.
4+
*
5+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
8+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
9+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
10+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
export class Todo {}
14+
export class User {
15+
isAppending = true
16+
}
17+
18+
// Mock authenticated ID
19+
const VIEWER_ID = "me"
20+
21+
// Mock user data
22+
const viewer = new User()
23+
viewer.id = VIEWER_ID
24+
const usersById = {
25+
[VIEWER_ID]: viewer,
26+
}
27+
28+
// Mock todo data
29+
const todosById = {}
30+
const todoIdsByUser = {
31+
[VIEWER_ID]: [],
32+
}
33+
let nextTodoId = 0
34+
35+
for (let i = 0; i < 20; i++) {
36+
addTodo(`Something Todo: ${i}`, false)
37+
}
38+
39+
export function addTodo(text, complete) {
40+
const todo = new Todo()
41+
todo.complete = !!complete
42+
todo.id = `${nextTodoId++}`
43+
todo.text = text
44+
todosById[todo.id] = todo
45+
if (viewer.isAppending) {
46+
todoIdsByUser[VIEWER_ID].push(todo.id)
47+
} else {
48+
todoIdsByUser[VIEWER_ID].unshift(todo.id)
49+
}
50+
return todo.id
51+
}
52+
53+
export function changeTodoStatus(id, complete) {
54+
const todo = getTodo(id)
55+
todo.complete = complete
56+
}
57+
58+
export function getTodo(id) {
59+
return todosById[id]
60+
}
61+
62+
export function getTodos(status = "any") {
63+
const todos = todoIdsByUser[VIEWER_ID].map((id) => todosById[id])
64+
if (status === "any") {
65+
return todos
66+
}
67+
return todos.filter((todo) => todo.complete === (status === "completed"))
68+
}
69+
70+
export function getUser(id) {
71+
return usersById[id]
72+
}
73+
74+
export function getViewer() {
75+
return getUser(VIEWER_ID)
76+
}
77+
78+
export function markAllTodos(complete) {
79+
const changedTodos = []
80+
getTodos().forEach((todo) => {
81+
if (todo.complete !== complete) {
82+
todo.complete = complete
83+
changedTodos.push(todo)
84+
}
85+
})
86+
return changedTodos.map((todo) => todo.id)
87+
}
88+
89+
export function removeTodo(id) {
90+
const todoIndex = todoIdsByUser[VIEWER_ID].indexOf(id)
91+
if (todoIndex !== -1) {
92+
todoIdsByUser[VIEWER_ID].splice(todoIndex, 1)
93+
}
94+
delete todosById[id]
95+
}
96+
97+
export function removeCompletedTodos() {
98+
const todosToRemove = getTodos().filter((todo) => todo.complete)
99+
todosToRemove.forEach((todo) => removeTodo(todo.id))
100+
return todosToRemove.map((todo) => todo.id)
101+
}
102+
103+
export function renameTodo(id, text) {
104+
const todo = getTodo(id)
105+
todo.text = text
106+
}
107+
108+
export function setIsAppending(isAppending) {
109+
viewer.isAppending = isAppending
110+
}

example-hooks-gen/data/schema.graphql

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
type Query {
2+
viewer: User
3+
4+
"""Fetches an object given its ID"""
5+
node(
6+
"""The ID of an object"""
7+
id: ID!
8+
): Node
9+
}
10+
11+
type User implements Node {
12+
"""The ID of an object"""
13+
id: ID!
14+
todos(status: String = "any", after: String, first: Int, before: String, last: Int): TodoConnection
15+
totalCount: Int
16+
completedCount: Int
17+
isAppending: Boolean!
18+
}
19+
20+
"""An object with an ID"""
21+
interface Node {
22+
"""The id of the object."""
23+
id: ID!
24+
}
25+
26+
"""A connection to a list of items."""
27+
type TodoConnection {
28+
"""Information to aid in pagination."""
29+
pageInfo: PageInfo!
30+
31+
"""A list of edges."""
32+
edges: [TodoEdge]
33+
}
34+
35+
"""Information about pagination in a connection."""
36+
type PageInfo {
37+
"""When paginating forwards, are there more items?"""
38+
hasNextPage: Boolean!
39+
40+
"""When paginating backwards, are there more items?"""
41+
hasPreviousPage: Boolean!
42+
43+
"""When paginating backwards, the cursor to continue."""
44+
startCursor: String
45+
46+
"""When paginating forwards, the cursor to continue."""
47+
endCursor: String
48+
}
49+
50+
"""An edge in a connection."""
51+
type TodoEdge {
52+
"""The item at the end of the edge"""
53+
node: Todo
54+
55+
"""A cursor for use in pagination"""
56+
cursor: String!
57+
}
58+
59+
type Todo implements Node {
60+
"""The ID of an object"""
61+
id: ID!
62+
text: String
63+
complete: Boolean
64+
}
65+
66+
type Mutation {
67+
addTodo(input: AddTodoInput!): AddTodoPayload
68+
changeTodoStatus(input: ChangeTodoStatusInput!): ChangeTodoStatusPayload
69+
markAllTodos(input: MarkAllTodosInput!): MarkAllTodosPayload
70+
removeCompletedTodos(input: RemoveCompletedTodosInput!): RemoveCompletedTodosPayload
71+
removeTodo(input: RemoveTodoInput!): RemoveTodoPayload
72+
renameTodo(input: RenameTodoInput!): RenameTodoPayload
73+
setAppending(appending: Boolean!): User
74+
}
75+
76+
type AddTodoPayload {
77+
todoEdge: TodoEdge
78+
viewer: User
79+
clientMutationId: String
80+
}
81+
82+
input AddTodoInput {
83+
text: String!
84+
clientMutationId: String
85+
}
86+
87+
type ChangeTodoStatusPayload {
88+
todo: Todo
89+
viewer: User
90+
clientMutationId: String
91+
}
92+
93+
input ChangeTodoStatusInput {
94+
complete: Boolean!
95+
id: ID!
96+
clientMutationId: String
97+
}
98+
99+
type MarkAllTodosPayload {
100+
changedTodos: [Todo]
101+
viewer: User
102+
clientMutationId: String
103+
}
104+
105+
input MarkAllTodosInput {
106+
complete: Boolean!
107+
clientMutationId: String
108+
}
109+
110+
type RemoveCompletedTodosPayload {
111+
deletedTodoIds: [String]
112+
viewer: User
113+
clientMutationId: String
114+
}
115+
116+
input RemoveCompletedTodosInput {
117+
clientMutationId: String
118+
}
119+
120+
type RemoveTodoPayload {
121+
deletedTodoId: ID
122+
viewer: User
123+
clientMutationId: String
124+
}
125+
126+
input RemoveTodoInput {
127+
id: ID!
128+
clientMutationId: String
129+
}
130+
131+
type RenameTodoPayload {
132+
todo: Todo
133+
clientMutationId: String
134+
}
135+
136+
input RenameTodoInput {
137+
id: ID!
138+
text: String!
139+
clientMutationId: String
140+
}

0 commit comments

Comments
 (0)