-
Notifications
You must be signed in to change notification settings - Fork 2k
/
schema-test.js
200 lines (178 loc) · 5.44 KB
/
schema-test.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
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
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import {
GraphQLSchema,
GraphQLScalarType,
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLString,
GraphQLInputObjectType,
GraphQLDirective,
GraphQLList,
} from '../';
import { describe, it } from 'mocha';
import { expect } from 'chai';
const InterfaceType = new GraphQLInterfaceType({
name: 'Interface',
fields: { fieldName: { type: GraphQLString } },
});
const DirectiveInputType = new GraphQLInputObjectType({
name: 'DirInput',
fields: {
field: {
type: GraphQLString,
},
},
});
const WrappedDirectiveInputType = new GraphQLInputObjectType({
name: 'WrappedDirInput',
fields: {
field: {
type: GraphQLString,
},
},
});
const Directive = new GraphQLDirective({
name: 'dir',
locations: ['OBJECT'],
args: {
arg: {
type: DirectiveInputType,
},
argList: {
type: new GraphQLList(WrappedDirectiveInputType),
},
},
});
const Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
getObject: {
type: InterfaceType,
resolve() {
return {};
},
},
},
}),
directives: [Directive],
});
describe('Type System: Schema', () => {
describe('Type Map', () => {
it('includes input types only used in directives', () => {
expect(Schema.getTypeMap()).to.include.key('DirInput');
expect(Schema.getTypeMap()).to.include.key('WrappedDirInput');
});
});
describe('Validity', () => {
describe('when not assumed valid', () => {
it('configures the schema to still needing validation', () => {
expect(
new GraphQLSchema({
assumeValid: false,
}).__validationErrors,
).to.equal(undefined);
});
it('configures the schema for allowed legacy names', () => {
expect(
new GraphQLSchema({
allowedLegacyNames: ['__badName'],
}).__allowedLegacyNames,
).to.deep.equal(['__badName']);
});
it('checks the configuration for mistakes', () => {
// $DisableFlowOnNegativeTest
expect(() => new GraphQLSchema(() => null)).to.throw();
// $DisableFlowOnNegativeTest
expect(() => new GraphQLSchema({ types: {} })).to.throw();
// $DisableFlowOnNegativeTest
expect(() => new GraphQLSchema({ directives: {} })).to.throw();
// $DisableFlowOnNegativeTest
expect(() => new GraphQLSchema({ allowedLegacyNames: {} })).to.throw();
});
});
describe('A Schema must contain uniquely named types', () => {
it('rejects a Schema which redefines a built-in type', () => {
const FakeString = new GraphQLScalarType({
name: 'String',
serialize: () => null,
});
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
normal: { type: GraphQLString },
fake: { type: FakeString },
},
});
expect(() => new GraphQLSchema({ query: QueryType })).to.throw(
'Schema must contain unique named types but contains multiple types named "String".',
);
});
it('rejects a Schema which defines an object type twice', () => {
const types = [
new GraphQLObjectType({ name: 'SameName', fields: {} }),
new GraphQLObjectType({ name: 'SameName', fields: {} }),
];
expect(() => new GraphQLSchema({ types })).to.throw(
'Schema must contain unique named types but contains multiple types named "SameName".',
);
});
it('rejects a Schema which defines fields with conflicting types', () => {
const fields = {};
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
a: { type: new GraphQLObjectType({ name: 'SameName', fields }) },
b: { type: new GraphQLObjectType({ name: 'SameName', fields }) },
},
});
expect(() => new GraphQLSchema({ query: QueryType })).to.throw(
'Schema must contain unique named types but contains multiple types named "SameName".',
);
});
});
describe('when assumed valid', () => {
it('configures the schema to have no errors', () => {
expect(
new GraphQLSchema({
assumeValid: true,
}).__validationErrors,
).to.deep.equal([]);
});
it('still configures the schema for allowed legacy names', () => {
expect(
new GraphQLSchema({
assumeValid: true,
allowedLegacyNames: ['__badName'],
}).__allowedLegacyNames,
).to.deep.equal(['__badName']);
});
it('does not check the configuration for mistakes', () => {
expect(() => {
const config = () => null;
config.assumeValid = true;
// $DisableFlowOnNegativeTest
return new GraphQLSchema(config);
}).to.not.throw();
expect(() => {
return new GraphQLSchema({
assumeValid: true,
// $DisableFlowOnNegativeTest
types: {},
// $DisableFlowOnNegativeTest
directives: { reduce: () => [] },
// $DisableFlowOnNegativeTest
allowedLegacyNames: {},
});
}).to.not.throw();
});
});
});
});