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

Commit 4a99a75

Browse files
committed
improve examples for hooks gen
1 parent 8bd3aef commit 4a99a75

33 files changed

+3634
-2164
lines changed

example-hooks-gen/.babelrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"passPerPreset": true,
32
"plugins": [
43
["relay", { "artifactDirectory": "./ts/__relay_artifacts__" }],
5-
"transform-runtime"
4+
"@babel/plugin-transform-runtime",
5+
"@babel/plugin-proposal-class-properties"
66
],
7-
"presets": ["react", "env", "stage-0"]
7+
"presets": ["@babel/preset-typescript", "@babel/preset-react", "@babel/preset-env"]
88
}

example-hooks-gen/data/database.js

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,88 +11,100 @@
1111
*/
1212

1313
export class Todo {}
14-
export class User {}
14+
export class User {
15+
isAppending = true
16+
}
1517

1618
// Mock authenticated ID
17-
const VIEWER_ID = 'me';
19+
const VIEWER_ID = "me"
1820

1921
// Mock user data
20-
const viewer = new User();
21-
viewer.id = VIEWER_ID;
22+
const viewer = new User()
23+
viewer.id = VIEWER_ID
2224
const usersById = {
2325
[VIEWER_ID]: viewer,
24-
};
26+
}
2527

2628
// Mock todo data
27-
const todosById = {};
29+
const todosById = {}
2830
const todoIdsByUser = {
2931
[VIEWER_ID]: [],
30-
};
31-
let nextTodoId = 0;
32-
addTodo('Taste JavaScript', true);
33-
addTodo('Buy a unicorn', false);
32+
}
33+
let nextTodoId = 0
34+
35+
for (let i = 0; i < 20; i++) {
36+
addTodo(`Something Todo: ${i}`, false)
37+
}
3438

3539
export function addTodo(text, complete) {
36-
const todo = new Todo();
37-
todo.complete = !!complete;
38-
todo.id = `${nextTodoId++}`;
39-
todo.text = text;
40-
todosById[todo.id] = todo;
41-
todoIdsByUser[VIEWER_ID].push(todo.id);
42-
return todo.id;
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
4351
}
4452

4553
export function changeTodoStatus(id, complete) {
46-
const todo = getTodo(id);
47-
todo.complete = complete;
54+
const todo = getTodo(id)
55+
todo.complete = complete
4856
}
4957

5058
export function getTodo(id) {
51-
return todosById[id];
59+
return todosById[id]
5260
}
5361

54-
export function getTodos(status = 'any') {
55-
const todos = todoIdsByUser[VIEWER_ID].map(id => todosById[id]);
56-
if (status === 'any') {
57-
return todos;
62+
export function getTodos(status = "any") {
63+
const todos = todoIdsByUser[VIEWER_ID].map((id) => todosById[id])
64+
if (status === "any") {
65+
return todos
5866
}
59-
return todos.filter(todo => todo.complete === (status === 'completed'));
67+
return todos.filter((todo) => todo.complete === (status === "completed"))
6068
}
6169

6270
export function getUser(id) {
63-
return usersById[id];
71+
return usersById[id]
6472
}
6573

6674
export function getViewer() {
67-
return getUser(VIEWER_ID);
75+
return getUser(VIEWER_ID)
6876
}
6977

7078
export function markAllTodos(complete) {
71-
const changedTodos = [];
72-
getTodos().forEach(todo => {
79+
const changedTodos = []
80+
getTodos().forEach((todo) => {
7381
if (todo.complete !== complete) {
74-
todo.complete = complete;
75-
changedTodos.push(todo);
82+
todo.complete = complete
83+
changedTodos.push(todo)
7684
}
77-
});
78-
return changedTodos.map(todo => todo.id);
85+
})
86+
return changedTodos.map((todo) => todo.id)
7987
}
8088

8189
export function removeTodo(id) {
82-
const todoIndex = todoIdsByUser[VIEWER_ID].indexOf(id);
90+
const todoIndex = todoIdsByUser[VIEWER_ID].indexOf(id)
8391
if (todoIndex !== -1) {
84-
todoIdsByUser[VIEWER_ID].splice(todoIndex, 1);
92+
todoIdsByUser[VIEWER_ID].splice(todoIndex, 1)
8593
}
86-
delete todosById[id];
94+
delete todosById[id]
8795
}
8896

8997
export function removeCompletedTodos() {
90-
const todosToRemove = getTodos().filter(todo => todo.complete);
91-
todosToRemove.forEach(todo => removeTodo(todo.id));
92-
return todosToRemove.map(todo => todo.id);
98+
const todosToRemove = getTodos().filter((todo) => todo.complete)
99+
todosToRemove.forEach((todo) => removeTodo(todo.id))
100+
return todosToRemove.map((todo) => todo.id)
93101
}
94102

95103
export function renameTodo(id, text) {
96-
const todo = getTodo(id);
97-
todo.text = text;
104+
const todo = getTodo(id)
105+
todo.text = text
106+
}
107+
108+
export function setIsAppending(isAppending) {
109+
viewer.isAppending = isAppending
98110
}

example-hooks-gen/data/schema.graphql

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,20 @@
1-
input AddTodoInput {
2-
text: String!
3-
clientMutationId: String
4-
}
5-
6-
type AddTodoPayload {
7-
todoEdge: TodoEdge
1+
type Query {
82
viewer: User
9-
clientMutationId: String
10-
}
113

12-
input ChangeTodoStatusInput {
13-
complete: Boolean!
14-
id: ID!
15-
clientMutationId: String
16-
}
17-
18-
type ChangeTodoStatusPayload {
19-
todo: Todo
20-
viewer: User
21-
clientMutationId: String
22-
}
23-
24-
input MarkAllTodosInput {
25-
complete: Boolean!
26-
clientMutationId: String
27-
}
28-
29-
type MarkAllTodosPayload {
30-
changedTodos: [Todo]
31-
viewer: User
32-
clientMutationId: String
4+
"""Fetches an object given its ID"""
5+
node(
6+
"""The ID of an object"""
7+
id: ID!
8+
): Node
339
}
3410

35-
type Mutation {
36-
addTodo(input: AddTodoInput!): AddTodoPayload
37-
changeTodoStatus(input: ChangeTodoStatusInput!): ChangeTodoStatusPayload
38-
markAllTodos(input: MarkAllTodosInput!): MarkAllTodosPayload
39-
removeCompletedTodos(input: RemoveCompletedTodosInput!): RemoveCompletedTodosPayload
40-
removeTodo(input: RemoveTodoInput!): RemoveTodoPayload
41-
renameTodo(input: RenameTodoInput!): RenameTodoPayload
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!
4218
}
4319

4420
"""An object with an ID"""
@@ -47,6 +23,15 @@ interface Node {
4723
id: ID!
4824
}
4925

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+
5035
"""Information about pagination in a connection."""
5136
type PageInfo {
5237
"""When paginating forwards, are there more items?"""
@@ -62,77 +47,94 @@ type PageInfo {
6247
endCursor: String
6348
}
6449

65-
type Query {
66-
viewer: User
50+
"""An edge in a connection."""
51+
type TodoEdge {
52+
"""The item at the end of the edge"""
53+
node: Todo
6754

68-
"""Fetches an object given its ID"""
69-
node(
70-
"""The ID of an object"""
71-
id: ID!
72-
): Node
55+
"""A cursor for use in pagination"""
56+
cursor: String!
7357
}
7458

75-
input RemoveCompletedTodosInput {
76-
clientMutationId: String
59+
type Todo implements Node {
60+
"""The ID of an object"""
61+
id: ID!
62+
text: String
63+
complete: Boolean
7764
}
7865

79-
type RemoveCompletedTodosPayload {
80-
deletedTodoIds: [String]
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
8178
viewer: User
8279
clientMutationId: String
8380
}
8481

85-
input RemoveTodoInput {
86-
id: ID!
82+
input AddTodoInput {
83+
text: String!
8784
clientMutationId: String
8885
}
8986

90-
type RemoveTodoPayload {
91-
deletedTodoId: ID
87+
type ChangeTodoStatusPayload {
88+
todo: Todo
9289
viewer: User
9390
clientMutationId: String
9491
}
9592

96-
input RenameTodoInput {
93+
input ChangeTodoStatusInput {
94+
complete: Boolean!
9795
id: ID!
98-
text: String!
9996
clientMutationId: String
10097
}
10198

102-
type RenameTodoPayload {
103-
todo: Todo
99+
type MarkAllTodosPayload {
100+
changedTodos: [Todo]
101+
viewer: User
104102
clientMutationId: String
105103
}
106104

107-
type Todo implements Node {
108-
"""The ID of an object"""
109-
id: ID!
110-
text: String
111-
complete: Boolean
105+
input MarkAllTodosInput {
106+
complete: Boolean!
107+
clientMutationId: String
112108
}
113109

114-
"""A connection to a list of items."""
115-
type TodoConnection {
116-
"""Information to aid in pagination."""
117-
pageInfo: PageInfo!
110+
type RemoveCompletedTodosPayload {
111+
deletedTodoIds: [String]
112+
viewer: User
113+
clientMutationId: String
114+
}
118115

119-
"""A list of edges."""
120-
edges: [TodoEdge]
116+
input RemoveCompletedTodosInput {
117+
clientMutationId: String
121118
}
122119

123-
"""An edge in a connection."""
124-
type TodoEdge {
125-
"""The item at the end of the edge"""
126-
node: Todo
120+
type RemoveTodoPayload {
121+
deletedTodoId: ID
122+
viewer: User
123+
clientMutationId: String
124+
}
127125

128-
"""A cursor for use in pagination"""
129-
cursor: String!
126+
input RemoveTodoInput {
127+
id: ID!
128+
clientMutationId: String
130129
}
131130

132-
type User implements Node {
133-
"""The ID of an object"""
131+
type RenameTodoPayload {
132+
todo: Todo
133+
clientMutationId: String
134+
}
135+
136+
input RenameTodoInput {
134137
id: ID!
135-
todos(status: String = "any", after: String, first: Int, before: String, last: Int): TodoConnection
136-
totalCount: Int
137-
completedCount: Int
138+
text: String!
139+
clientMutationId: String
138140
}

0 commit comments

Comments
 (0)