Skip to content

Commit ace6727

Browse files
authored
add toConfig tests (#4225)
would address #1679 I think a simpler version than #1683
1 parent 6a379a5 commit ace6727

File tree

1 file changed

+280
-29
lines changed

1 file changed

+280
-29
lines changed

src/type/__tests__/definition-test.ts

Lines changed: 280 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ import { inspect } from '../../jsutils/inspect.js';
77
import { Kind } from '../../language/kinds.js';
88
import { parseConstValue } from '../../language/parser.js';
99

10-
import type { GraphQLNullableType, GraphQLType } from '../definition.js';
10+
import type {
11+
GraphQLEnumTypeConfig,
12+
GraphQLInputObjectTypeConfig,
13+
GraphQLInterfaceTypeConfig,
14+
GraphQLNullableType,
15+
GraphQLObjectTypeConfig,
16+
GraphQLScalarTypeConfig,
17+
GraphQLType,
18+
GraphQLUnionTypeConfig,
19+
} from '../definition.js';
1120
import {
1221
GraphQLEnumType,
1322
GraphQLInputObjectType,
@@ -38,32 +47,43 @@ const ListOfNonNullScalarsType = new GraphQLList(NonNullScalarType);
3847
const NonNullListOfScalars = new GraphQLNonNull(ListOfScalarsType);
3948

4049
/* c8 ignore next */
41-
const dummyFunc = () => expect.fail('Never called and used as a placeholder');
50+
const passThroughFunc = (arg: any) => arg;
51+
const dummyAny = {} as any;
4252

4353
describe('Type System: Scalars', () => {
44-
it('accepts a Scalar type defining serialize', () => {
45-
expect(() => new GraphQLScalarType({ name: 'SomeScalar' })).to.not.throw();
46-
});
47-
48-
it('accepts a Scalar type defining specifiedByURL', () => {
49-
expect(
50-
() =>
51-
new GraphQLScalarType({
52-
name: 'SomeScalar',
53-
specifiedByURL: 'https://example.com/foo_spec',
54-
}),
55-
).not.to.throw();
56-
});
57-
58-
it('accepts a Scalar type defining parseValue and parseConstLiteral', () => {
59-
expect(
60-
() =>
61-
new GraphQLScalarType({
62-
name: 'SomeScalar',
63-
parseValue: dummyFunc,
64-
parseConstLiteral: dummyFunc,
65-
}),
66-
).to.not.throw();
54+
it('can be converted from a minimal configuration object', () => {
55+
const someScalar = new GraphQLScalarType({ name: 'SomeScalar' });
56+
expect(someScalar.toConfig()).to.deep.equal({
57+
name: 'SomeScalar',
58+
description: undefined,
59+
specifiedByURL: undefined,
60+
serialize: someScalar.serialize,
61+
parseValue: someScalar.parseValue,
62+
parseLiteral: someScalar.parseLiteral,
63+
parseConstLiteral: undefined,
64+
valueToLiteral: undefined,
65+
extensions: {},
66+
astNode: undefined,
67+
extensionASTNodes: [],
68+
});
69+
});
70+
71+
it('can be converted to a configuration object', () => {
72+
const someScalarConfig: GraphQLScalarTypeConfig<unknown, unknown> = {
73+
name: 'SomeScalar',
74+
description: 'SomeScalar description.',
75+
specifiedByURL: 'https://example.com/foo_spec',
76+
serialize: passThroughFunc,
77+
parseValue: passThroughFunc,
78+
parseLiteral: passThroughFunc,
79+
parseConstLiteral: passThroughFunc,
80+
valueToLiteral: passThroughFunc,
81+
extensions: { someExtension: 'extension' },
82+
astNode: dummyAny,
83+
extensionASTNodes: [dummyAny],
84+
};
85+
const someScalar = new GraphQLScalarType(someScalarConfig);
86+
expect(someScalar.toConfig()).to.deep.equal(someScalarConfig);
6787
});
6888

6989
it('provides default methods if omitted', () => {
@@ -97,7 +117,7 @@ describe('Type System: Scalars', () => {
97117
() =>
98118
new GraphQLScalarType({
99119
name: 'SomeScalar',
100-
parseLiteral: dummyFunc,
120+
parseLiteral: passThroughFunc,
101121
}),
102122
).to.throw(
103123
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.',
@@ -109,7 +129,7 @@ describe('Type System: Scalars', () => {
109129
() =>
110130
new GraphQLScalarType({
111131
name: 'SomeScalar',
112-
parseConstLiteral: dummyFunc,
132+
parseConstLiteral: passThroughFunc,
113133
}),
114134
).to.throw(
115135
'SomeScalar must provide both "parseValue" and "parseConstLiteral" functions.',
@@ -118,6 +138,59 @@ describe('Type System: Scalars', () => {
118138
});
119139

120140
describe('Type System: Objects', () => {
141+
it('can be converted from a minimal configuration object', () => {
142+
const someObject = new GraphQLObjectType({
143+
name: 'SomeObject',
144+
fields: {},
145+
});
146+
expect(someObject.toConfig()).to.deep.equal({
147+
name: 'SomeObject',
148+
description: undefined,
149+
interfaces: [],
150+
fields: {},
151+
isTypeOf: undefined,
152+
extensions: {},
153+
astNode: undefined,
154+
extensionASTNodes: [],
155+
});
156+
});
157+
158+
it('can be converted to a configuration object', () => {
159+
const someObjectConfig: GraphQLObjectTypeConfig<unknown, unknown> = {
160+
name: 'SomeObject',
161+
description: 'SomeObject description.',
162+
interfaces: [InterfaceType],
163+
fields: {
164+
f: {
165+
description: 'Field description.',
166+
type: ScalarType,
167+
args: {
168+
input: {
169+
description: 'Argument description.',
170+
type: ScalarType,
171+
defaultValue: 'DefaultValue',
172+
defaultValueLiteral: undefined,
173+
deprecationReason: 'Argument deprecation reason.',
174+
extensions: { someExtension: 'extension' },
175+
astNode: dummyAny,
176+
},
177+
},
178+
resolve: passThroughFunc,
179+
subscribe: passThroughFunc,
180+
deprecationReason: 'Field deprecation reason.',
181+
extensions: { someExtension: 'extension' },
182+
astNode: dummyAny,
183+
},
184+
},
185+
isTypeOf: passThroughFunc,
186+
extensions: { someExtension: 'extension' },
187+
astNode: dummyAny,
188+
extensionASTNodes: [dummyAny],
189+
};
190+
const someObject = new GraphQLObjectType(someObjectConfig);
191+
expect(someObject.toConfig()).to.deep.equal(someObjectConfig);
192+
});
193+
121194
it('does not mutate passed field definitions', () => {
122195
const outputFields = {
123196
field1: { type: ScalarType },
@@ -281,7 +354,7 @@ describe('Type System: Objects', () => {
281354
fields: {
282355
f: {
283356
type: ScalarType,
284-
resolve: dummyFunc,
357+
resolve: passThroughFunc,
285358
},
286359
},
287360
});
@@ -336,7 +409,60 @@ describe('Type System: Objects', () => {
336409
});
337410

338411
describe('Type System: Interfaces', () => {
339-
it('accepts an Interface type defining resolveType', () => {
412+
it('can be converted from a minimal configuration object', () => {
413+
const someInterface = new GraphQLInterfaceType({
414+
name: 'SomeInterface',
415+
fields: {},
416+
});
417+
expect(someInterface.toConfig()).to.deep.equal({
418+
name: 'SomeInterface',
419+
description: undefined,
420+
interfaces: [],
421+
fields: {},
422+
resolveType: undefined,
423+
extensions: {},
424+
astNode: undefined,
425+
extensionASTNodes: [],
426+
});
427+
});
428+
429+
it('can be converted to a configuration object', () => {
430+
const someInterfaceConfig: GraphQLInterfaceTypeConfig<unknown, unknown> = {
431+
name: 'SomeInterface',
432+
description: 'SomeInterface description.',
433+
interfaces: [InterfaceType],
434+
fields: {
435+
f: {
436+
description: 'Field description.',
437+
type: ScalarType,
438+
args: {
439+
input: {
440+
description: 'Argument description.',
441+
type: ScalarType,
442+
defaultValue: undefined,
443+
defaultValueLiteral: dummyAny,
444+
deprecationReason: 'Argument deprecation reason.',
445+
extensions: { someExtension: 'extension' },
446+
astNode: dummyAny,
447+
},
448+
},
449+
resolve: passThroughFunc,
450+
subscribe: passThroughFunc,
451+
deprecationReason: 'Field deprecation reason.',
452+
extensions: { someExtension: 'extension' },
453+
astNode: dummyAny,
454+
},
455+
},
456+
resolveType: passThroughFunc,
457+
extensions: {},
458+
astNode: {} as any,
459+
extensionASTNodes: [],
460+
};
461+
const someInterface = new GraphQLInterfaceType(someInterfaceConfig);
462+
expect(someInterface.toConfig()).to.deep.equal(someInterfaceConfig);
463+
});
464+
465+
it('accepts an Interface type defining a field', () => {
340466
expect(
341467
() =>
342468
new GraphQLInterfaceType({
@@ -346,6 +472,28 @@ describe('Type System: Interfaces', () => {
346472
).to.not.throw();
347473
});
348474

475+
it('accepts an Interface type with a field function', () => {
476+
const interfaceType = new GraphQLInterfaceType({
477+
name: 'SomeInterface',
478+
fields: () => ({
479+
f: { type: ScalarType },
480+
}),
481+
});
482+
expect(interfaceType.getFields()).to.deep.equal({
483+
f: {
484+
name: 'f',
485+
description: undefined,
486+
type: ScalarType,
487+
args: [],
488+
resolve: undefined,
489+
subscribe: undefined,
490+
deprecationReason: undefined,
491+
extensions: {},
492+
astNode: undefined,
493+
},
494+
});
495+
});
496+
349497
it('accepts an Interface type with an array of interfaces', () => {
350498
const implementing = new GraphQLInterfaceType({
351499
name: 'AnotherInterface',
@@ -372,6 +520,36 @@ describe('Type System: Interfaces', () => {
372520
});
373521

374522
describe('Type System: Unions', () => {
523+
it('can be converted from a minimal configuration object', () => {
524+
const someUnion = new GraphQLUnionType({
525+
name: 'SomeUnion',
526+
types: [],
527+
});
528+
expect(someUnion.toConfig()).to.deep.equal({
529+
name: 'SomeUnion',
530+
description: undefined,
531+
types: [],
532+
resolveType: undefined,
533+
extensions: {},
534+
astNode: undefined,
535+
extensionASTNodes: [],
536+
});
537+
});
538+
539+
it('can be converted to a configuration object', () => {
540+
const someUnionConfig: GraphQLUnionTypeConfig<unknown, unknown> = {
541+
name: 'SomeUnion',
542+
description: 'SomeUnion description.',
543+
types: [ObjectType],
544+
resolveType: passThroughFunc,
545+
extensions: {},
546+
astNode: {} as any,
547+
extensionASTNodes: [],
548+
};
549+
const someUnion = new GraphQLUnionType(someUnionConfig);
550+
expect(someUnion.toConfig()).to.deep.equal(someUnionConfig);
551+
});
552+
375553
it('accepts a Union type defining resolveType', () => {
376554
expect(
377555
() =>
@@ -414,6 +592,39 @@ describe('Type System: Unions', () => {
414592
});
415593

416594
describe('Type System: Enums', () => {
595+
it('can be converted from a minimal configuration object', () => {
596+
const someEnum = new GraphQLEnumType({ name: 'SomeEnum', values: {} });
597+
expect(someEnum.toConfig()).to.deep.equal({
598+
name: 'SomeEnum',
599+
description: undefined,
600+
values: {},
601+
extensions: {},
602+
astNode: undefined,
603+
extensionASTNodes: [],
604+
});
605+
});
606+
607+
it('can be converted to a configuration object', () => {
608+
const someEnumConfig: GraphQLEnumTypeConfig = {
609+
name: 'SomeEnum',
610+
description: 'SomeEnum description.',
611+
values: {
612+
FOO: {
613+
description: 'FOO description.',
614+
value: 'foo',
615+
deprecationReason: 'Value deprecation reason.',
616+
extensions: { someExtension: 'extension' },
617+
astNode: dummyAny,
618+
},
619+
},
620+
extensions: { someExtension: 'extension' },
621+
astNode: dummyAny,
622+
extensionASTNodes: [dummyAny],
623+
};
624+
const someEnum = new GraphQLEnumType(someEnumConfig);
625+
expect(someEnum.toConfig()).to.deep.equal(someEnumConfig);
626+
});
627+
417628
it('defines an enum type with deprecated value', () => {
418629
const EnumTypeWithDeprecatedValue = new GraphQLEnumType({
419630
name: 'EnumWithDeprecatedValue',
@@ -516,6 +727,46 @@ describe('Type System: Enums', () => {
516727
});
517728

518729
describe('Type System: Input Objects', () => {
730+
it('can be converted from a minimal configuration object', () => {
731+
const inputObject = new GraphQLInputObjectType({
732+
name: 'SomeInputObject',
733+
fields: {},
734+
});
735+
expect(inputObject.toConfig()).to.deep.equal({
736+
name: 'SomeInputObject',
737+
description: undefined,
738+
fields: {},
739+
isOneOf: false,
740+
extensions: {},
741+
astNode: undefined,
742+
extensionASTNodes: [],
743+
});
744+
});
745+
746+
it('can be converted to a configuration object', () => {
747+
const someInputObjectConfig: GraphQLInputObjectTypeConfig = {
748+
name: 'SomeInputObject',
749+
description: 'SomeObject description.',
750+
fields: {
751+
input: {
752+
description: 'Argument description.',
753+
type: ScalarType,
754+
defaultValue: 'DefaultValue',
755+
defaultValueLiteral: undefined,
756+
deprecationReason: 'Argument deprecation reason.',
757+
extensions: { someExtension: 'extension' },
758+
astNode: dummyAny,
759+
},
760+
},
761+
isOneOf: true,
762+
extensions: { someExtension: 'extension' },
763+
astNode: dummyAny,
764+
extensionASTNodes: [dummyAny],
765+
};
766+
const someInputObject = new GraphQLInputObjectType(someInputObjectConfig);
767+
expect(someInputObject.toConfig()).to.deep.equal(someInputObjectConfig);
768+
});
769+
519770
describe('Input Objects must have fields', () => {
520771
it('accepts an Input Object type with fields', () => {
521772
const inputObjType = new GraphQLInputObjectType({

0 commit comments

Comments
 (0)