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

Commit 37a4af6

Browse files
alloykastermester
authored andcommitted
1 parent 9ebe729 commit 37a4af6

29 files changed

+7933
-0
lines changed

example/.babelrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"passPerPreset": true,
3+
"plugins": [
4+
"relay",
5+
"transform-runtime"
6+
],
7+
"presets": [
8+
"react",
9+
"es2015",
10+
"stage-0"
11+
]
12+
}

example/.eslintignore

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

example/.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
3+
extends:
4+
- fbjs

example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
__generated__/

example/.watchmanconfig

Whitespace-only changes.

example/README.md

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

example/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/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)