Skip to content

Commit 6012d28

Browse files
validation-test: Improve typings (#2710)
1 parent dd3ed1c commit 6012d28

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

src/type/__tests__/validation-test.js

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import type {
1616
GraphQLNamedType,
1717
GraphQLInputType,
1818
GraphQLOutputType,
19+
GraphQLFieldConfig,
20+
GraphQLArgumentConfig,
21+
GraphQLInputFieldConfig,
22+
GraphQLEnumValueConfigMap,
1923
} from '../definition';
2024
import { GraphQLSchema } from '../schema';
2125
import { GraphQLString } from '../scalars';
@@ -103,13 +107,12 @@ const notInputTypes: Array<GraphQLOutputType> = [
103107
...withModifiers(SomeInterfaceType),
104108
];
105109

106-
function schemaWithFieldType(type) {
110+
function schemaWithFieldType(type: GraphQLOutputType): GraphQLSchema {
107111
return new GraphQLSchema({
108112
query: new GraphQLObjectType({
109113
name: 'Query',
110114
fields: { f: { type } },
111115
}),
112-
types: [type],
113116
});
114117
}
115118

@@ -516,18 +519,20 @@ describe('Type System: Fields args must be properly named', () => {
516519
});
517520

518521
it('rejects field arg with invalid names', () => {
519-
const QueryType = new GraphQLObjectType({
520-
name: 'SomeObject',
521-
fields: {
522-
badField: {
523-
type: GraphQLString,
524-
args: {
525-
'bad-name-with-dashes': { type: GraphQLString },
522+
const schema = schemaWithFieldType(
523+
new GraphQLObjectType({
524+
name: 'SomeObject',
525+
fields: {
526+
badField: {
527+
type: GraphQLString,
528+
args: {
529+
'bad-name-with-dashes': { type: GraphQLString },
530+
},
526531
},
527532
},
528-
},
529-
});
530-
const schema = new GraphQLSchema({ query: QueryType });
533+
}),
534+
);
535+
531536
expect(validateSchema(schema)).to.deep.equal([
532537
{
533538
message:
@@ -928,7 +933,7 @@ describe('Type System: Enum types must be well defined', () => {
928933
});
929934

930935
it('rejects an Enum type with incorrectly named values', () => {
931-
function schemaWithEnum(values) {
936+
function schemaWithEnum(values: GraphQLEnumValueConfigMap): GraphQLSchema {
932937
return schemaWithFieldType(
933938
new GraphQLEnumType({
934939
name: 'SomeEnum',
@@ -979,11 +984,13 @@ describe('Type System: Enum types must be well defined', () => {
979984
});
980985

981986
describe('Type System: Object fields must have output types', () => {
982-
function schemaWithObjectFieldOfType(fieldType: GraphQLOutputType) {
987+
function schemaWithObjectField(
988+
fieldConfig: GraphQLFieldConfig<mixed, mixed>,
989+
): GraphQLSchema {
983990
const BadObjectType = new GraphQLObjectType({
984991
name: 'BadObject',
985992
fields: {
986-
badField: { type: fieldType },
993+
badField: fieldConfig,
987994
},
988995
});
989996

@@ -1001,14 +1008,14 @@ describe('Type System: Object fields must have output types', () => {
10011008
for (const type of outputTypes) {
10021009
const typeName = inspect(type);
10031010
it(`accepts an output type as an Object field type: ${typeName}`, () => {
1004-
const schema = schemaWithObjectFieldOfType(type);
1011+
const schema = schemaWithObjectField({ type });
10051012
expect(validateSchema(schema)).to.deep.equal([]);
10061013
});
10071014
}
10081015

10091016
it('rejects an empty Object field type', () => {
10101017
// $FlowExpectedError
1011-
const schema = schemaWithObjectFieldOfType(undefined);
1018+
const schema = schemaWithObjectField({ type: undefined });
10121019
expect(validateSchema(schema)).to.deep.equal([
10131020
{
10141021
message:
@@ -1021,7 +1028,7 @@ describe('Type System: Object fields must have output types', () => {
10211028
const typeStr = inspect(type);
10221029
it(`rejects a non-output type as an Object field type: ${typeStr}`, () => {
10231030
// $FlowExpectedError
1024-
const schema = schemaWithObjectFieldOfType(type);
1031+
const schema = schemaWithObjectField({ type });
10251032
expect(validateSchema(schema)).to.deep.equal([
10261033
{
10271034
message: `The type of BadObject.badField must be Output Type but got: ${typeStr}.`,
@@ -1032,7 +1039,7 @@ describe('Type System: Object fields must have output types', () => {
10321039

10331040
it('rejects a non-type value as an Object field type', () => {
10341041
// $FlowExpectedError
1035-
const schema = schemaWithObjectFieldOfType(Number);
1042+
const schema = schemaWithObjectField({ type: Number });
10361043
expect(validateSchema(schema)).to.deep.equal([
10371044
{
10381045
message:
@@ -1292,20 +1299,20 @@ describe('Type System: Interface extensions should be valid', () => {
12921299
});
12931300

12941301
describe('Type System: Interface fields must have output types', () => {
1295-
function schemaWithInterfaceFieldOfType(fieldType: GraphQLOutputType) {
1302+
function schemaWithInterfaceField(
1303+
fieldConfig: GraphQLFieldConfig<mixed, mixed>,
1304+
): GraphQLSchema {
1305+
const fields = { badField: fieldConfig };
1306+
12961307
const BadInterfaceType = new GraphQLInterfaceType({
12971308
name: 'BadInterface',
1298-
fields: {
1299-
badField: { type: fieldType },
1300-
},
1309+
fields,
13011310
});
13021311

13031312
const BadImplementingType = new GraphQLObjectType({
13041313
name: 'BadImplementing',
13051314
interfaces: [BadInterfaceType],
1306-
fields: {
1307-
badField: { type: fieldType },
1308-
},
1315+
fields,
13091316
});
13101317

13111318
return new GraphQLSchema({
@@ -1322,14 +1329,14 @@ describe('Type System: Interface fields must have output types', () => {
13221329
for (const type of outputTypes) {
13231330
const typeName = inspect(type);
13241331
it(`accepts an output type as an Interface field type: ${typeName}`, () => {
1325-
const schema = schemaWithInterfaceFieldOfType(type);
1332+
const schema = schemaWithInterfaceField({ type });
13261333
expect(validateSchema(schema)).to.deep.equal([]);
13271334
});
13281335
}
13291336

13301337
it('rejects an empty Interface field type', () => {
13311338
// $FlowExpectedError
1332-
const schema = schemaWithInterfaceFieldOfType(undefined);
1339+
const schema = schemaWithInterfaceField({ type: undefined });
13331340
expect(validateSchema(schema)).to.deep.equal([
13341341
{
13351342
message:
@@ -1346,7 +1353,7 @@ describe('Type System: Interface fields must have output types', () => {
13461353
const typeStr = inspect(type);
13471354
it(`rejects a non-output type as an Interface field type: ${typeStr}`, () => {
13481355
// $FlowExpectedError
1349-
const schema = schemaWithInterfaceFieldOfType(type);
1356+
const schema = schemaWithInterfaceField({ type });
13501357
expect(validateSchema(schema)).to.deep.equal([
13511358
{
13521359
message: `The type of BadImplementing.badField must be Output Type but got: ${typeStr}.`,
@@ -1360,7 +1367,7 @@ describe('Type System: Interface fields must have output types', () => {
13601367

13611368
it('rejects a non-type value as an Interface field type', () => {
13621369
// $FlowExpectedError
1363-
const schema = schemaWithInterfaceFieldOfType(Number);
1370+
const schema = schemaWithInterfaceField({ type: Number });
13641371
expect(validateSchema(schema)).to.deep.equal([
13651372
{
13661373
message:
@@ -1423,14 +1430,14 @@ describe('Type System: Interface fields must have output types', () => {
14231430
});
14241431

14251432
describe('Type System: Arguments must have input types', () => {
1426-
function schemaWithArgOfType(argType: GraphQLInputType) {
1433+
function schemaWithArg(argConfig: GraphQLArgumentConfig): GraphQLSchema {
14271434
const BadObjectType = new GraphQLObjectType({
14281435
name: 'BadObject',
14291436
fields: {
14301437
badField: {
14311438
type: GraphQLString,
14321439
args: {
1433-
badArg: { type: argType },
1440+
badArg: argConfig,
14341441
},
14351442
},
14361443
},
@@ -1447,7 +1454,7 @@ describe('Type System: Arguments must have input types', () => {
14471454
new GraphQLDirective({
14481455
name: 'BadDirective',
14491456
args: {
1450-
badArg: { type: argType },
1457+
badArg: argConfig,
14511458
},
14521459
locations: ['QUERY'],
14531460
}),
@@ -1458,14 +1465,14 @@ describe('Type System: Arguments must have input types', () => {
14581465
for (const type of inputTypes) {
14591466
const typeName = inspect(type);
14601467
it(`accepts an input type as a field arg type: ${typeName}`, () => {
1461-
const schema = schemaWithArgOfType(type);
1468+
const schema = schemaWithArg({ type });
14621469
expect(validateSchema(schema)).to.deep.equal([]);
14631470
});
14641471
}
14651472

14661473
it('rejects an empty field arg type', () => {
14671474
// $FlowExpectedError
1468-
const schema = schemaWithArgOfType(undefined);
1475+
const schema = schemaWithArg({ type: undefined });
14691476
expect(validateSchema(schema)).to.deep.equal([
14701477
{
14711478
message:
@@ -1482,7 +1489,7 @@ describe('Type System: Arguments must have input types', () => {
14821489
const typeStr = inspect(type);
14831490
it(`rejects a non-input type as a field arg type: ${typeStr}`, () => {
14841491
// $FlowExpectedError
1485-
const schema = schemaWithArgOfType(type);
1492+
const schema = schemaWithArg({ type });
14861493
expect(validateSchema(schema)).to.deep.equal([
14871494
{
14881495
message: `The type of @BadDirective(badArg:) must be Input Type but got: ${typeStr}.`,
@@ -1496,7 +1503,7 @@ describe('Type System: Arguments must have input types', () => {
14961503

14971504
it('rejects a non-type value as a field arg type', () => {
14981505
// $FlowExpectedError
1499-
const schema = schemaWithArgOfType(Number);
1506+
const schema = schemaWithArg({ type: Number });
15001507
expect(validateSchema(schema)).to.deep.equal([
15011508
{
15021509
message:
@@ -1533,11 +1540,13 @@ describe('Type System: Arguments must have input types', () => {
15331540
});
15341541

15351542
describe('Type System: Input Object fields must have input types', () => {
1536-
function schemaWithInputFieldOfType(inputFieldType: GraphQLInputType) {
1543+
function schemaWithInputField(
1544+
inputFieldConfig: GraphQLInputFieldConfig,
1545+
): GraphQLSchema {
15371546
const BadInputObjectType = new GraphQLInputObjectType({
15381547
name: 'BadInputObject',
15391548
fields: {
1540-
badField: { type: inputFieldType },
1549+
badField: inputFieldConfig,
15411550
},
15421551
});
15431552

@@ -1559,14 +1568,14 @@ describe('Type System: Input Object fields must have input types', () => {
15591568
for (const type of inputTypes) {
15601569
const typeName = inspect(type);
15611570
it(`accepts an input type as an input field type: ${typeName}`, () => {
1562-
const schema = schemaWithInputFieldOfType(type);
1571+
const schema = schemaWithInputField({ type });
15631572
expect(validateSchema(schema)).to.deep.equal([]);
15641573
});
15651574
}
15661575

15671576
it('rejects an empty input field type', () => {
15681577
// $FlowExpectedError
1569-
const schema = schemaWithInputFieldOfType(undefined);
1578+
const schema = schemaWithInputField({ type: undefined });
15701579
expect(validateSchema(schema)).to.deep.equal([
15711580
{
15721581
message:
@@ -1579,7 +1588,7 @@ describe('Type System: Input Object fields must have input types', () => {
15791588
const typeStr = inspect(type);
15801589
it(`rejects a non-input type as an input field type: ${typeStr}`, () => {
15811590
// $FlowExpectedError
1582-
const schema = schemaWithInputFieldOfType(type);
1591+
const schema = schemaWithInputField({ type });
15831592
expect(validateSchema(schema)).to.deep.equal([
15841593
{
15851594
message: `The type of BadInputObject.badField must be Input Type but got: ${typeStr}.`,
@@ -1590,7 +1599,7 @@ describe('Type System: Input Object fields must have input types', () => {
15901599

15911600
it('rejects a non-type value as an input field type', () => {
15921601
// $FlowExpectedError
1593-
const schema = schemaWithInputFieldOfType(Number);
1602+
const schema = schemaWithInputField({ type: Number });
15941603
expect(validateSchema(schema)).to.deep.equal([
15951604
{
15961605
message:

0 commit comments

Comments
 (0)