From 305a1a0a9211c071f0df8e189956160b42043543 Mon Sep 17 00:00:00 2001 From: Robert Zhu Date: Thu, 19 Jan 2017 16:23:03 -0500 Subject: [PATCH] Validate Input Types do not define resolvers --- src/type/__tests__/validation-test.js | 91 +++++++++++++++++++++++++++ src/type/definition.js | 5 ++ 2 files changed, 96 insertions(+) diff --git a/src/type/__tests__/validation-test.js b/src/type/__tests__/validation-test.js index 1e321d0bba..f448025498 100644 --- a/src/type/__tests__/validation-test.js +++ b/src/type/__tests__/validation-test.js @@ -726,6 +726,97 @@ describe('Type System: Input Objects must have fields', () => { }); +describe('Type System: Input Object fields must not have resolvers', () => { + + function schemaWithInputObject(inputObjectType) { + return new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: { + f: { + type: GraphQLString, + args: { + input: { type: inputObjectType } + } + } + } + }) + }); + } + + it('accepts an Input Object type with no resolver', () => { + expect( + () => schemaWithInputObject(new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + f: { + type: GraphQLString, + } + } + })) + ).not.to.throw(); + }); + + it('accepts an Input Object type with null resolver', () => { + expect( + () => schemaWithInputObject(new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + f: { + type: GraphQLString, + resolve: null, + } + } + })) + ).not.to.throw(); + }); + + it('accepts an Input Object type with undefined resolver', () => { + expect( + () => schemaWithInputObject(new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + f: { + type: GraphQLString, + resolve: undefined, + } + } + })) + ).not.to.throw(); + }); + + it('rejects an Input Object type with resolver function', () => { + expect( + () => schemaWithInputObject(new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + f: { + type: GraphQLString, + resolve: () => { return 0; }, + } + } + })) + ).to.throw('SomeInputObject.f field type has a resolve property,' + + ' but Input Types cannot define resolvers.'); + }); + + it('rejects an Input Object type with resolver constant', () => { + expect( + () => schemaWithInputObject(new GraphQLInputObjectType({ + name: 'SomeInputObject', + fields: { + f: { + type: GraphQLString, + resolve: {}, + } + } + })) + ).to.throw('SomeInputObject.f field type has a resolve property,' + + ' but Input Types cannot define resolvers.'); + }); +}); + + describe('Type System: Object types must be assertable', () => { it('accepts an Object type with an isTypeOf function', () => { diff --git a/src/type/definition.js b/src/type/definition.js index 1a20ccb737..fb8c5e7cc7 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -1050,6 +1050,11 @@ export class GraphQLInputObjectType { `${this.name}.${fieldName} field type must be Input Type but ` + `got: ${String(field.type)}.` ); + invariant( + field.resolve == null, + `${this.name}.${fieldName} field type has a resolve property, but ` + + 'Input Types cannot define resolvers.' + ); resultFieldMap[fieldName] = field; }); return resultFieldMap;