diff --git a/src/type/__tests__/validation-test.js b/src/type/__tests__/validation-test.js index 2119ec1f67..20b8a5e6b3 100644 --- a/src/type/__tests__/validation-test.js +++ b/src/type/__tests__/validation-test.js @@ -16,6 +16,10 @@ import type { GraphQLNamedType, GraphQLInputType, GraphQLOutputType, + GraphQLFieldConfig, + GraphQLArgumentConfig, + GraphQLInputFieldConfig, + GraphQLEnumValueConfigMap, } from '../definition'; import { GraphQLSchema } from '../schema'; import { GraphQLString } from '../scalars'; @@ -103,13 +107,12 @@ const notInputTypes: Array = [ ...withModifiers(SomeInterfaceType), ]; -function schemaWithFieldType(type) { +function schemaWithFieldType(type: GraphQLOutputType): GraphQLSchema { return new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: { f: { type } }, }), - types: [type], }); } @@ -516,18 +519,20 @@ describe('Type System: Fields args must be properly named', () => { }); it('rejects field arg with invalid names', () => { - const QueryType = new GraphQLObjectType({ - name: 'SomeObject', - fields: { - badField: { - type: GraphQLString, - args: { - 'bad-name-with-dashes': { type: GraphQLString }, + const schema = schemaWithFieldType( + new GraphQLObjectType({ + name: 'SomeObject', + fields: { + badField: { + type: GraphQLString, + args: { + 'bad-name-with-dashes': { type: GraphQLString }, + }, }, }, - }, - }); - const schema = new GraphQLSchema({ query: QueryType }); + }), + ); + expect(validateSchema(schema)).to.deep.equal([ { message: @@ -928,7 +933,7 @@ describe('Type System: Enum types must be well defined', () => { }); it('rejects an Enum type with incorrectly named values', () => { - function schemaWithEnum(values) { + function schemaWithEnum(values: GraphQLEnumValueConfigMap): GraphQLSchema { return schemaWithFieldType( new GraphQLEnumType({ name: 'SomeEnum', @@ -979,11 +984,13 @@ describe('Type System: Enum types must be well defined', () => { }); describe('Type System: Object fields must have output types', () => { - function schemaWithObjectFieldOfType(fieldType: GraphQLOutputType) { + function schemaWithObjectField( + fieldConfig: GraphQLFieldConfig, + ): GraphQLSchema { const BadObjectType = new GraphQLObjectType({ name: 'BadObject', fields: { - badField: { type: fieldType }, + badField: fieldConfig, }, }); @@ -1001,14 +1008,14 @@ describe('Type System: Object fields must have output types', () => { for (const type of outputTypes) { const typeName = inspect(type); it(`accepts an output type as an Object field type: ${typeName}`, () => { - const schema = schemaWithObjectFieldOfType(type); + const schema = schemaWithObjectField({ type }); expect(validateSchema(schema)).to.deep.equal([]); }); } it('rejects an empty Object field type', () => { // $FlowExpectedError - const schema = schemaWithObjectFieldOfType(undefined); + const schema = schemaWithObjectField({ type: undefined }); expect(validateSchema(schema)).to.deep.equal([ { message: @@ -1021,7 +1028,7 @@ describe('Type System: Object fields must have output types', () => { const typeStr = inspect(type); it(`rejects a non-output type as an Object field type: ${typeStr}`, () => { // $FlowExpectedError - const schema = schemaWithObjectFieldOfType(type); + const schema = schemaWithObjectField({ type }); expect(validateSchema(schema)).to.deep.equal([ { 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', () => { it('rejects a non-type value as an Object field type', () => { // $FlowExpectedError - const schema = schemaWithObjectFieldOfType(Number); + const schema = schemaWithObjectField({ type: Number }); expect(validateSchema(schema)).to.deep.equal([ { message: @@ -1292,20 +1299,20 @@ describe('Type System: Interface extensions should be valid', () => { }); describe('Type System: Interface fields must have output types', () => { - function schemaWithInterfaceFieldOfType(fieldType: GraphQLOutputType) { + function schemaWithInterfaceField( + fieldConfig: GraphQLFieldConfig, + ): GraphQLSchema { + const fields = { badField: fieldConfig }; + const BadInterfaceType = new GraphQLInterfaceType({ name: 'BadInterface', - fields: { - badField: { type: fieldType }, - }, + fields, }); const BadImplementingType = new GraphQLObjectType({ name: 'BadImplementing', interfaces: [BadInterfaceType], - fields: { - badField: { type: fieldType }, - }, + fields, }); return new GraphQLSchema({ @@ -1322,14 +1329,14 @@ describe('Type System: Interface fields must have output types', () => { for (const type of outputTypes) { const typeName = inspect(type); it(`accepts an output type as an Interface field type: ${typeName}`, () => { - const schema = schemaWithInterfaceFieldOfType(type); + const schema = schemaWithInterfaceField({ type }); expect(validateSchema(schema)).to.deep.equal([]); }); } it('rejects an empty Interface field type', () => { // $FlowExpectedError - const schema = schemaWithInterfaceFieldOfType(undefined); + const schema = schemaWithInterfaceField({ type: undefined }); expect(validateSchema(schema)).to.deep.equal([ { message: @@ -1346,7 +1353,7 @@ describe('Type System: Interface fields must have output types', () => { const typeStr = inspect(type); it(`rejects a non-output type as an Interface field type: ${typeStr}`, () => { // $FlowExpectedError - const schema = schemaWithInterfaceFieldOfType(type); + const schema = schemaWithInterfaceField({ type }); expect(validateSchema(schema)).to.deep.equal([ { 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', () => { it('rejects a non-type value as an Interface field type', () => { // $FlowExpectedError - const schema = schemaWithInterfaceFieldOfType(Number); + const schema = schemaWithInterfaceField({ type: Number }); expect(validateSchema(schema)).to.deep.equal([ { message: @@ -1423,14 +1430,14 @@ describe('Type System: Interface fields must have output types', () => { }); describe('Type System: Arguments must have input types', () => { - function schemaWithArgOfType(argType: GraphQLInputType) { + function schemaWithArg(argConfig: GraphQLArgumentConfig): GraphQLSchema { const BadObjectType = new GraphQLObjectType({ name: 'BadObject', fields: { badField: { type: GraphQLString, args: { - badArg: { type: argType }, + badArg: argConfig, }, }, }, @@ -1447,7 +1454,7 @@ describe('Type System: Arguments must have input types', () => { new GraphQLDirective({ name: 'BadDirective', args: { - badArg: { type: argType }, + badArg: argConfig, }, locations: ['QUERY'], }), @@ -1458,14 +1465,14 @@ describe('Type System: Arguments must have input types', () => { for (const type of inputTypes) { const typeName = inspect(type); it(`accepts an input type as a field arg type: ${typeName}`, () => { - const schema = schemaWithArgOfType(type); + const schema = schemaWithArg({ type }); expect(validateSchema(schema)).to.deep.equal([]); }); } it('rejects an empty field arg type', () => { // $FlowExpectedError - const schema = schemaWithArgOfType(undefined); + const schema = schemaWithArg({ type: undefined }); expect(validateSchema(schema)).to.deep.equal([ { message: @@ -1482,7 +1489,7 @@ describe('Type System: Arguments must have input types', () => { const typeStr = inspect(type); it(`rejects a non-input type as a field arg type: ${typeStr}`, () => { // $FlowExpectedError - const schema = schemaWithArgOfType(type); + const schema = schemaWithArg({ type }); expect(validateSchema(schema)).to.deep.equal([ { 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', () => { it('rejects a non-type value as a field arg type', () => { // $FlowExpectedError - const schema = schemaWithArgOfType(Number); + const schema = schemaWithArg({ type: Number }); expect(validateSchema(schema)).to.deep.equal([ { message: @@ -1533,11 +1540,13 @@ describe('Type System: Arguments must have input types', () => { }); describe('Type System: Input Object fields must have input types', () => { - function schemaWithInputFieldOfType(inputFieldType: GraphQLInputType) { + function schemaWithInputField( + inputFieldConfig: GraphQLInputFieldConfig, + ): GraphQLSchema { const BadInputObjectType = new GraphQLInputObjectType({ name: 'BadInputObject', fields: { - badField: { type: inputFieldType }, + badField: inputFieldConfig, }, }); @@ -1559,14 +1568,14 @@ describe('Type System: Input Object fields must have input types', () => { for (const type of inputTypes) { const typeName = inspect(type); it(`accepts an input type as an input field type: ${typeName}`, () => { - const schema = schemaWithInputFieldOfType(type); + const schema = schemaWithInputField({ type }); expect(validateSchema(schema)).to.deep.equal([]); }); } it('rejects an empty input field type', () => { // $FlowExpectedError - const schema = schemaWithInputFieldOfType(undefined); + const schema = schemaWithInputField({ type: undefined }); expect(validateSchema(schema)).to.deep.equal([ { message: @@ -1579,7 +1588,7 @@ describe('Type System: Input Object fields must have input types', () => { const typeStr = inspect(type); it(`rejects a non-input type as an input field type: ${typeStr}`, () => { // $FlowExpectedError - const schema = schemaWithInputFieldOfType(type); + const schema = schemaWithInputField({ type }); expect(validateSchema(schema)).to.deep.equal([ { 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', () => { it('rejects a non-type value as an input field type', () => { // $FlowExpectedError - const schema = schemaWithInputFieldOfType(Number); + const schema = schemaWithInputField({ type: Number }); expect(validateSchema(schema)).to.deep.equal([ { message: