Skip to content

Commit 305a1a0

Browse files
author
Robert Zhu
committed
Validate Input Types do not define resolvers
1 parent 5cf4cef commit 305a1a0

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/type/__tests__/validation-test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,97 @@ describe('Type System: Input Objects must have fields', () => {
726726
});
727727

728728

729+
describe('Type System: Input Object fields must not have resolvers', () => {
730+
731+
function schemaWithInputObject(inputObjectType) {
732+
return new GraphQLSchema({
733+
query: new GraphQLObjectType({
734+
name: 'Query',
735+
fields: {
736+
f: {
737+
type: GraphQLString,
738+
args: {
739+
input: { type: inputObjectType }
740+
}
741+
}
742+
}
743+
})
744+
});
745+
}
746+
747+
it('accepts an Input Object type with no resolver', () => {
748+
expect(
749+
() => schemaWithInputObject(new GraphQLInputObjectType({
750+
name: 'SomeInputObject',
751+
fields: {
752+
f: {
753+
type: GraphQLString,
754+
}
755+
}
756+
}))
757+
).not.to.throw();
758+
});
759+
760+
it('accepts an Input Object type with null resolver', () => {
761+
expect(
762+
() => schemaWithInputObject(new GraphQLInputObjectType({
763+
name: 'SomeInputObject',
764+
fields: {
765+
f: {
766+
type: GraphQLString,
767+
resolve: null,
768+
}
769+
}
770+
}))
771+
).not.to.throw();
772+
});
773+
774+
it('accepts an Input Object type with undefined resolver', () => {
775+
expect(
776+
() => schemaWithInputObject(new GraphQLInputObjectType({
777+
name: 'SomeInputObject',
778+
fields: {
779+
f: {
780+
type: GraphQLString,
781+
resolve: undefined,
782+
}
783+
}
784+
}))
785+
).not.to.throw();
786+
});
787+
788+
it('rejects an Input Object type with resolver function', () => {
789+
expect(
790+
() => schemaWithInputObject(new GraphQLInputObjectType({
791+
name: 'SomeInputObject',
792+
fields: {
793+
f: {
794+
type: GraphQLString,
795+
resolve: () => { return 0; },
796+
}
797+
}
798+
}))
799+
).to.throw('SomeInputObject.f field type has a resolve property,' +
800+
' but Input Types cannot define resolvers.');
801+
});
802+
803+
it('rejects an Input Object type with resolver constant', () => {
804+
expect(
805+
() => schemaWithInputObject(new GraphQLInputObjectType({
806+
name: 'SomeInputObject',
807+
fields: {
808+
f: {
809+
type: GraphQLString,
810+
resolve: {},
811+
}
812+
}
813+
}))
814+
).to.throw('SomeInputObject.f field type has a resolve property,' +
815+
' but Input Types cannot define resolvers.');
816+
});
817+
});
818+
819+
729820
describe('Type System: Object types must be assertable', () => {
730821

731822
it('accepts an Object type with an isTypeOf function', () => {

src/type/definition.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,11 @@ export class GraphQLInputObjectType {
10501050
`${this.name}.${fieldName} field type must be Input Type but ` +
10511051
`got: ${String(field.type)}.`
10521052
);
1053+
invariant(
1054+
field.resolve == null,
1055+
`${this.name}.${fieldName} field type has a resolve property, but ` +
1056+
'Input Types cannot define resolvers.'
1057+
);
10531058
resultFieldMap[fieldName] = field;
10541059
});
10551060
return resultFieldMap;

0 commit comments

Comments
 (0)