Skip to content

Commit

Permalink
Merge pull request #675 from robzhu/master
Browse files Browse the repository at this point in the history
Validate Input Types do not define resolvers
  • Loading branch information
leebyron authored Jan 20, 2017
2 parents d1f586e + 305a1a0 commit 7064b9d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7064b9d

Please sign in to comment.