-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathgetIntrospectionQuery-test.ts
147 lines (113 loc) · 3.97 KB
/
getIntrospectionQuery-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
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parse } from '../../language/parser';
import { validate } from '../../validation/validate';
import { buildSchema } from '../buildASTSchema';
import type { IntrospectionOptions } from '../getIntrospectionQuery';
import { getIntrospectionQuery } from '../getIntrospectionQuery';
const dummySchema = buildSchema(`
type Query {
dummy: String
}
`);
function expectIntrospectionQuery(options?: IntrospectionOptions) {
const query = getIntrospectionQuery(options);
const validationErrors = validate(dummySchema, parse(query));
expect(validationErrors).to.deep.equal([]);
return {
toMatch(name: string, times: number = 1): void {
const pattern = toRegExp(name);
expect(query).to.match(pattern);
expect(query.match(pattern)).to.have.lengthOf(times);
},
toNotMatch(name: string): void {
expect(query).to.not.match(toRegExp(name));
},
};
function toRegExp(name: string): RegExp {
return new RegExp('\\b' + name + '\\b', 'g');
}
}
describe('getIntrospectionQuery', () => {
it('skip all "description" fields', () => {
expectIntrospectionQuery().toMatch('description', 5);
expectIntrospectionQuery({ descriptions: true }).toMatch('description', 5);
expectIntrospectionQuery({ descriptions: false }).toNotMatch('description');
});
it('include "isRepeatable" field on directives', () => {
expectIntrospectionQuery().toNotMatch('isRepeatable');
expectIntrospectionQuery({ directiveIsRepeatable: true }).toMatch(
'isRepeatable',
);
expectIntrospectionQuery({ directiveIsRepeatable: false }).toNotMatch(
'isRepeatable',
);
});
it('include "description" field on schema', () => {
expectIntrospectionQuery().toMatch('description', 5);
expectIntrospectionQuery({ schemaDescription: false }).toMatch(
'description',
5,
);
expectIntrospectionQuery({ schemaDescription: true }).toMatch(
'description',
6,
);
expectIntrospectionQuery({
descriptions: false,
schemaDescription: true,
}).toNotMatch('description');
});
it('include "specifiedBy" field', () => {
expectIntrospectionQuery().toNotMatch('specifiedByURL');
expectIntrospectionQuery({ specifiedByUrl: true }).toMatch(
'specifiedByURL',
);
expectIntrospectionQuery({ specifiedByUrl: false }).toNotMatch(
'specifiedByURL',
);
});
it('include "isDeprecated" field on input values', () => {
expectIntrospectionQuery().toMatch('isDeprecated', 2);
expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch(
'isDeprecated',
3,
);
expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch(
'isDeprecated',
2,
);
});
it('include "deprecationReason" field on input values', () => {
expectIntrospectionQuery().toMatch('deprecationReason', 2);
expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch(
'deprecationReason',
3,
);
expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch(
'deprecationReason',
2,
);
});
it('include "isOneOf" field on input objects', () => {
expectIntrospectionQuery().toNotMatch('isOneOf');
expectIntrospectionQuery({ oneOf: true }).toMatch('isOneOf', 1);
expectIntrospectionQuery({ oneOf: false }).toNotMatch('isOneOf');
});
it('include deprecated input field and args', () => {
expectIntrospectionQuery().toMatch('includeDeprecated: true', 2);
expectIntrospectionQuery({ inputValueDeprecation: true }).toMatch(
'includeDeprecated: true',
5,
);
expectIntrospectionQuery({ inputValueDeprecation: false }).toMatch(
'includeDeprecated: true',
2,
);
});
it('throw error if typeDepth is too high', () => {
expect(() => getIntrospectionQuery({ typeDepth: 101 })).to.throw(
'Please set typeDepth to a reasonable value between 0 and 100; the default is 9.',
);
});
});