Skip to content

Commit 25e96ae

Browse files
committed
final CH3
0 parents  commit 25e96ae

File tree

18 files changed

+6749
-0
lines changed

18 files changed

+6749
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.babelrc

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

.gitignore

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

data/data.json

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
[
2+
{
3+
"firstName": "Harrell",
4+
"lastName": "Browning",
5+
"gender": "male",
6+
"language": "english",
7+
"email": "harrellbrowning@barkarama.com",
8+
"image": "https://images.unsplash.com/photo-1418874478290-e7bc1086d373?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
9+
},
10+
{
11+
"firstName": "Penelope",
12+
"lastName": "Thornton",
13+
"gender": "female",
14+
"language": "english",
15+
"email": "penelopethornton@barkarama.com",
16+
"image": "https://images.unsplash.com/photo-1444068700300-d6ef2904d5e4?dpr=1&auto=format&fit=crop&w=1500&h=994&q=80&cs=tinysrgb&crop=&bg="
17+
},
18+
{
19+
"firstName": "Sutton",
20+
"lastName": "Daniel",
21+
"gender": "male",
22+
"language": "english",
23+
"email": "suttondaniel@barkarama.com",
24+
"image": "https://images.unsplash.com/photo-1475669913832-fd187510b578?dpr=1&auto=format&fit=crop&w=1500&h=1001&q=80&cs=tinysrgb&crop=&bg="
25+
},
26+
{
27+
"firstName": "Lenore",
28+
"lastName": "Henson",
29+
"gender": "female",
30+
"language": "english",
31+
"email": "lenorehenson@barkarama.com",
32+
"image": "https://images.unsplash.com/photo-1490911679664-99d34ae56d94?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
33+
},
34+
{
35+
"firstName": "Kelsey",
36+
"lastName": "Vasquez",
37+
"gender": "female",
38+
"language": "english",
39+
"email": "kelseyvasquez@barkarama.com",
40+
"image": "https://images.unsplash.com/photo-1444694828841-5e4fa2f4ccfd?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
41+
},
42+
{
43+
"firstName": "Hodge",
44+
"lastName": "Preston",
45+
"gender": "male",
46+
"language": "english",
47+
"email": "hodgepreston@barkarama.com",
48+
"image": "https://images.unsplash.com/photo-1477954417131-efc62c1b25cb?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
49+
},
50+
{
51+
"firstName": "Doris",
52+
"lastName": "Bentley",
53+
"gender": "female",
54+
"language": "english",
55+
"email": "dorisbentley@barkarama.com",
56+
"image": "https://images.unsplash.com/photo-1444700853742-b57744cb8852?dpr=1&auto=format&fit=crop&w=1500&h=999&q=80&cs=tinysrgb&crop=&bg="
57+
},
58+
{
59+
"firstName": "Carmela",
60+
"lastName": "Becker",
61+
"gender": "female",
62+
"language": "english",
63+
"email": "carmelabecker@barkarama.com",
64+
"image": "https://images.unsplash.com/photo-1444595538250-b05641c01607?dpr=1&auto=format&fit=crop&w=1500&h=845&q=80&cs=tinysrgb&crop=&bg="
65+
},
66+
{
67+
"firstName": "Tonia",
68+
"lastName": "Dillon",
69+
"gender": "female",
70+
"language": "english",
71+
"email": "toniadillon@barkarama.com",
72+
"image": "https://images.unsplash.com/photo-1492400253066-7aa8373adfe1?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
73+
},
74+
{
75+
"firstName": "Holmes",
76+
"lastName": "Witt",
77+
"gender": "male",
78+
"language": "english",
79+
"email": "holmeswitt@barkarama.com",
80+
"image": "https://images.unsplash.com/photo-1490923574825-95e44b2d7242?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
81+
},
82+
{
83+
"firstName": "Paige",
84+
"lastName": "Hurst",
85+
"gender": "female",
86+
"language": "english",
87+
"email": "paigehurst@barkarama.com",
88+
"image": "https://images.unsplash.com/photo-1492204955184-3a524e156c4a?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
89+
},
90+
{
91+
"firstName": "Meyers",
92+
"lastName": "Wheeler",
93+
"gender": "male",
94+
"language": "english",
95+
"email": "meyerswheeler@barkarama.com",
96+
"image": "https://images.unsplash.com/photo-1469033156481-db2cb560c480?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
97+
},
98+
{
99+
"firstName": "Edwards",
100+
"lastName": "Douglas",
101+
"gender": "male",
102+
"language": "english",
103+
"email": "edwardsdouglas@barkarama.com",
104+
"image": "https://images.unsplash.com/photo-1420578509940-73e9f7232eae?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
105+
},
106+
{
107+
"firstName": "Connie",
108+
"lastName": "Ortega",
109+
"gender": "female",
110+
"language": "english",
111+
"email": "connieortega@barkarama.com",
112+
"image": "https://images.unsplash.com/photo-1476901192299-9a86d0454988?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg="
113+
}
114+
]

data/database.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import data from "./data.json";
2+
3+
class User {}
4+
class Friend {}
5+
6+
//Mock data
7+
const viewer = new User();
8+
viewer.id = "1";
9+
viewer.name = "me";
10+
const friends = data.map(obj => {
11+
const friend = new Friend();
12+
friend.id = require("crypto")
13+
.randomBytes(10)
14+
.toString("hex");
15+
friend.firstName = obj.firstName;
16+
friend.lastName = obj.lastName;
17+
friend.gender = obj.gender;
18+
friend.language = obj.language;
19+
friend.email = obj.email;
20+
friend.image = obj.image;
21+
return friend;
22+
});
23+
24+
module.exports = {
25+
getUser: id => (id === viewer.id ? viewer : null),
26+
getViewer: () => viewer,
27+
getFriend: id => friends.find(w => w.id == id),
28+
getFriends: () => friends,
29+
User,
30+
Friend
31+
};

data/schema.graphql

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
type Friend {
2+
# The ID of an object
3+
id: ID!
4+
firstName: String
5+
lastName: String
6+
gender: String
7+
language: String
8+
email: String
9+
image: String
10+
}
11+
12+
# A connection to a list of items.
13+
type FriendConnection {
14+
# Information to aid in pagination.
15+
pageInfo: PageInfo!
16+
17+
# A list of edges.
18+
edges: [FriendEdge]
19+
}
20+
21+
# An edge in a connection.
22+
type FriendEdge {
23+
# The item at the end of the edge
24+
node: Friend
25+
26+
# A cursor for use in pagination
27+
cursor: String!
28+
}
29+
30+
# An object with an ID
31+
interface Node {
32+
# The id of the object.
33+
id: ID!
34+
}
35+
36+
# Information about pagination in a connection.
37+
type PageInfo {
38+
# When paginating forwards, are there more items?
39+
hasNextPage: Boolean!
40+
41+
# When paginating backwards, are there more items?
42+
hasPreviousPage: Boolean!
43+
44+
# When paginating backwards, the cursor to continue.
45+
startCursor: String
46+
47+
# When paginating forwards, the cursor to continue.
48+
endCursor: String
49+
}
50+
51+
type Query {
52+
# Fetches an object given its ID
53+
node(
54+
# The ID of an object
55+
id: ID!
56+
): Node
57+
viewer: User
58+
}
59+
60+
type User {
61+
# The ID of an object
62+
id: ID!
63+
friends(after: String, first: Int, before: String, last: Int): FriendConnection
64+
}

data/schema.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";
2+
import {
3+
connectionArgs,
4+
connectionDefinitions,
5+
connectionFromArray,
6+
fromGlobalId,
7+
globalIdField,
8+
nodeDefinitions,
9+
toGlobalId
10+
} from "graphql-relay";
11+
import {
12+
User,
13+
Friend,
14+
getFriend,
15+
getFriends,
16+
getUser,
17+
getViewer
18+
} from "./database";
19+
20+
const { nodeInerface, nodeField } = nodeDefinitions(
21+
(globalId) => {
22+
const { type, id } = fromGlobalId(globalId);
23+
if (type === 'Friend') {
24+
return getFriend(id);
25+
} else if (type === 'User') {
26+
return getUser(id);
27+
}
28+
return null
29+
},
30+
(obj) => {
31+
if (obj instanceof Friend) {
32+
return GraphQLFriend;
33+
} else if (obj instanceof User) {
34+
return GraphQLUser;
35+
}
36+
return null;
37+
}
38+
)
39+
40+
const GraphQLUser = new GraphQLObjectType({
41+
name: "User",
42+
fields: () => ({
43+
id: globalIdField("User"),
44+
friends: {
45+
type: friendsConnection,
46+
args: connectionArgs,
47+
resolve: (_, args) => connectionFromArray(getFriends(), args)
48+
}
49+
}),
50+
interface: [nodeInerface]
51+
});
52+
53+
const GraphQLFriend = new GraphQLObjectType({
54+
name: "Friend",
55+
fields: () => ({
56+
id: globalIdField("Friend"),
57+
firstName: {
58+
type: GraphQLString
59+
},
60+
lastName: {
61+
type: GraphQLString
62+
},
63+
gender: {
64+
type: GraphQLString
65+
},
66+
language: {
67+
type: GraphQLString
68+
},
69+
email: {
70+
type: GraphQLString
71+
},
72+
image: {
73+
type: GraphQLString
74+
}
75+
}),
76+
interface: [nodeInerface],
77+
});
78+
79+
const { connectionType: friendsConnection } =
80+
connectionDefinitions({ name: 'Friend', nodeType: GraphQLFriend });
81+
82+
const Query = new GraphQLObjectType({
83+
name: 'Query',
84+
fields: () => ({
85+
node: nodeField,
86+
viewer: {
87+
type: GraphQLUser,
88+
resolve: ()=> getViewer(),
89+
},
90+
}),
91+
});
92+
93+
export const schema = new GraphQLSchema({
94+
query: Query,
95+
});

0 commit comments

Comments
 (0)