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

Commit 8bd3aef

Browse files
committed
Restore original example-hooks
1 parent 01cc377 commit 8bd3aef

Some content is hidden

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

44 files changed

+11011
-0
lines changed

example-hooks/.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"passPerPreset": true,
3+
"plugins": [
4+
["relay", { "artifactDirectory": "./ts/__relay_artifacts__" }],
5+
"transform-runtime"
6+
],
7+
"presets": ["react", "env", "stage-0"]
8+
}

example-hooks/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

example-hooks/.watchmanconfig

Whitespace-only changes.

example-hooks/README.md

Lines changed: 60 additions & 0 deletions
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/data/database.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
16+
// Mock authenticated ID
17+
const VIEWER_ID = 'me';
18+
19+
// Mock user data
20+
const viewer = new User();
21+
viewer.id = VIEWER_ID;
22+
const usersById = {
23+
[VIEWER_ID]: viewer,
24+
};
25+
26+
// Mock todo data
27+
const todosById = {};
28+
const todoIdsByUser = {
29+
[VIEWER_ID]: [],
30+
};
31+
let nextTodoId = 0;
32+
addTodo('Taste JavaScript', true);
33+
addTodo('Buy a unicorn', false);
34+
35+
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;
43+
}
44+
45+
export function changeTodoStatus(id, complete) {
46+
const todo = getTodo(id);
47+
todo.complete = complete;
48+
}
49+
50+
export function getTodo(id) {
51+
return todosById[id];
52+
}
53+
54+
export function getTodos(status = 'any') {
55+
const todos = todoIdsByUser[VIEWER_ID].map(id => todosById[id]);
56+
if (status === 'any') {
57+
return todos;
58+
}
59+
return todos.filter(todo => todo.complete === (status === 'completed'));
60+
}
61+
62+
export function getUser(id) {
63+
return usersById[id];
64+
}
65+
66+
export function getViewer() {
67+
return getUser(VIEWER_ID);
68+
}
69+
70+
export function markAllTodos(complete) {
71+
const changedTodos = [];
72+
getTodos().forEach(todo => {
73+
if (todo.complete !== complete) {
74+
todo.complete = complete;
75+
changedTodos.push(todo);
76+
}
77+
});
78+
return changedTodos.map(todo => todo.id);
79+
}
80+
81+
export function removeTodo(id) {
82+
const todoIndex = todoIdsByUser[VIEWER_ID].indexOf(id);
83+
if (todoIndex !== -1) {
84+
todoIdsByUser[VIEWER_ID].splice(todoIndex, 1);
85+
}
86+
delete todosById[id];
87+
}
88+
89+
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);
93+
}
94+
95+
export function renameTodo(id, text) {
96+
const todo = getTodo(id);
97+
todo.text = text;
98+
}

example-hooks/data/schema.graphql

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
input AddTodoInput {
2+
text: String!
3+
clientMutationId: String
4+
}
5+
6+
type AddTodoPayload {
7+
todoEdge: TodoEdge
8+
viewer: User
9+
clientMutationId: String
10+
}
11+
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
33+
}
34+
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
42+
}
43+
44+
"""An object with an ID"""
45+
interface Node {
46+
"""The id of the object."""
47+
id: ID!
48+
}
49+
50+
"""Information about pagination in a connection."""
51+
type PageInfo {
52+
"""When paginating forwards, are there more items?"""
53+
hasNextPage: Boolean!
54+
55+
"""When paginating backwards, are there more items?"""
56+
hasPreviousPage: Boolean!
57+
58+
"""When paginating backwards, the cursor to continue."""
59+
startCursor: String
60+
61+
"""When paginating forwards, the cursor to continue."""
62+
endCursor: String
63+
}
64+
65+
type Query {
66+
viewer: User
67+
68+
"""Fetches an object given its ID"""
69+
node(
70+
"""The ID of an object"""
71+
id: ID!
72+
): Node
73+
}
74+
75+
input RemoveCompletedTodosInput {
76+
clientMutationId: String
77+
}
78+
79+
type RemoveCompletedTodosPayload {
80+
deletedTodoIds: [String]
81+
viewer: User
82+
clientMutationId: String
83+
}
84+
85+
input RemoveTodoInput {
86+
id: ID!
87+
clientMutationId: String
88+
}
89+
90+
type RemoveTodoPayload {
91+
deletedTodoId: ID
92+
viewer: User
93+
clientMutationId: String
94+
}
95+
96+
input RenameTodoInput {
97+
id: ID!
98+
text: String!
99+
clientMutationId: String
100+
}
101+
102+
type RenameTodoPayload {
103+
todo: Todo
104+
clientMutationId: String
105+
}
106+
107+
type Todo implements Node {
108+
"""The ID of an object"""
109+
id: ID!
110+
text: String
111+
complete: Boolean
112+
}
113+
114+
"""A connection to a list of items."""
115+
type TodoConnection {
116+
"""Information to aid in pagination."""
117+
pageInfo: PageInfo!
118+
119+
"""A list of edges."""
120+
edges: [TodoEdge]
121+
}
122+
123+
"""An edge in a connection."""
124+
type TodoEdge {
125+
"""The item at the end of the edge"""
126+
node: Todo
127+
128+
"""A cursor for use in pagination"""
129+
cursor: String!
130+
}
131+
132+
type User implements Node {
133+
"""The ID of an object"""
134+
id: ID!
135+
todos(status: String = "any", after: String, first: Int, before: String, last: Int): TodoConnection
136+
totalCount: Int
137+
completedCount: Int
138+
}

0 commit comments

Comments
 (0)