/
schema-test.ts
188 lines (175 loc) · 4.72 KB
/
schema-test.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parse } from '../../language/parser';
import { GraphQLSchema } from '../../type/schema';
import {
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
} from '../../type/definition';
import {
GraphQLID,
GraphQLInt,
GraphQLString,
GraphQLBoolean,
} from '../../type/scalars';
import { executeSync } from '../execute';
describe('Execute: Handles execution with a complex schema', () => {
it('executes using a schema', () => {
const BlogImage = new GraphQLObjectType({
name: 'Image',
fields: {
url: { type: GraphQLString },
width: { type: GraphQLInt },
height: { type: GraphQLInt },
},
});
const BlogAuthor: GraphQLObjectType = new GraphQLObjectType({
name: 'Author',
fields: () => ({
id: { type: GraphQLString },
name: { type: GraphQLString },
pic: {
args: { width: { type: GraphQLInt }, height: { type: GraphQLInt } },
type: BlogImage,
resolve: (obj, { width, height }) => obj.pic(width, height),
},
recentArticle: { type: BlogArticle },
}),
});
const BlogArticle = new GraphQLObjectType({
name: 'Article',
fields: {
id: { type: new GraphQLNonNull(GraphQLString) },
isPublished: { type: GraphQLBoolean },
author: { type: BlogAuthor },
title: { type: GraphQLString },
body: { type: GraphQLString },
keywords: { type: new GraphQLList(GraphQLString) },
},
});
const BlogQuery = new GraphQLObjectType({
name: 'Query',
fields: {
article: {
type: BlogArticle,
args: { id: { type: GraphQLID } },
resolve: (_, { id }) => article(id),
},
feed: {
type: new GraphQLList(BlogArticle),
resolve: () => [
article(1),
article(2),
article(3),
article(4),
article(5),
article(6),
article(7),
article(8),
article(9),
article(10),
],
},
},
});
const BlogSchema = new GraphQLSchema({
query: BlogQuery,
});
function article(id: number) {
return {
id,
isPublished: true,
author: {
id: 123,
name: 'John Smith',
pic: (width: number, height: number) => getPic(123, width, height),
recentArticle: () => article(1),
},
title: 'My Article ' + id,
body: 'This is a post',
hidden: 'This data is not exposed in the schema',
keywords: ['foo', 'bar', 1, true, null],
};
}
function getPic(uid: number, width: number, height: number) {
return {
url: `cdn://${uid}`,
width: `${width}`,
height: `${height}`,
};
}
const document = parse(`
{
feed {
id,
title
},
article(id: "1") {
...articleFields,
author {
id,
name,
pic(width: 640, height: 480) {
url,
width,
height
},
recentArticle {
...articleFields,
keywords
}
}
}
}
fragment articleFields on Article {
id,
isPublished,
title,
body,
hidden,
notDefined
}
`);
// Note: this is intentionally not validating to ensure appropriate
// behavior occurs when executing an invalid query.
expect(executeSync({ schema: BlogSchema, document })).to.deep.equal({
data: {
feed: [
{ id: '1', title: 'My Article 1' },
{ id: '2', title: 'My Article 2' },
{ id: '3', title: 'My Article 3' },
{ id: '4', title: 'My Article 4' },
{ id: '5', title: 'My Article 5' },
{ id: '6', title: 'My Article 6' },
{ id: '7', title: 'My Article 7' },
{ id: '8', title: 'My Article 8' },
{ id: '9', title: 'My Article 9' },
{ id: '10', title: 'My Article 10' },
],
article: {
id: '1',
isPublished: true,
title: 'My Article 1',
body: 'This is a post',
author: {
id: '123',
name: 'John Smith',
pic: {
url: 'cdn://123',
width: 640,
height: 480,
},
recentArticle: {
id: '1',
isPublished: true,
title: 'My Article 1',
body: 'This is a post',
keywords: ['foo', 'bar', '1', 'true', null],
},
},
},
},
});
});
});