forked from KATT/graphql-with-sequelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema-relay.js
143 lines (133 loc) · 2.78 KB
/
schema-relay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import Db from './db';
import {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLList
} from 'graphql';
import {
nodeDefinitions,
fromGlobalId,
globalIdField,
connectionArgs,
connectionDefinitions,
connectionFromPromisedArray
} from 'graphql-relay';
const Post = new GraphQLObjectType({
name: 'Post',
description: 'Blog post',
fields () {
return {
title: {
type: GraphQLString,
resolve (post) {
return post.title;
}
},
content: {
type: GraphQLString,
resolve (post) {
return post.content;
}
},
person: {
type: personType,
resolve (post) {
return post.getPerson();
}
}
};
}
});
const { nodeInterface, nodeField } = nodeDefinitions(
globalId => {
const { type, id } = fromGlobalId(globalId);
console.log('type=', type);
console.log('id=', id);
if (type === 'Person') {
return Db.models.person.findById(id);
}
return null;
},
obj => {
return personType;
}
);
const personType = new GraphQLObjectType({
name: 'Person',
description: 'This represents a Person',
fields: () => {
return {
id: globalIdField('Person'),
firstName: {
type: GraphQLString,
resolve (person) {
return person.firstName;
}
},
lastName: {
type: GraphQLString,
resolve (person) {
return person.lastName;
}
},
email: {
type: GraphQLString,
resolve (person) {
return person.email;
}
},
posts: {
type: new GraphQLList(Post),
resolve (person) {
return person.getPosts();
}
}
};
},
interfaces: [nodeInterface]
});
// Connections
const { connectionType: PersonConnection } = connectionDefinitions({
name: 'Person',
nodeType: personType
});
const queryType = new GraphQLObjectType({
name: 'Query',
description: 'Root query',
fields: () => ({
node: nodeField,
peopleRelay: {
type: PersonConnection,
description: 'Person connection test',
args: connectionArgs,
resolve (root, args) {
return connectionFromPromisedArray(Db.models.person.findAll(), args);
}
},
person: {
type: personType,
resolve (root, args) {
return Db.models.person.findOne({ where: args });
}
},
people: {
type: new GraphQLList(personType),
args: {
id: {
type: GraphQLInt
},
email: {
type: GraphQLString
}
},
resolve (root, args) {
return Db.models.person.findAll({ where: args });
}
}
})
});
export default new GraphQLSchema({
query: queryType
});