Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resolver validation, check if value is a function #669

Merged
merged 2 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"dot-location": [2, "property"],
"dot-notation": 0,
"eol-last": 2,
"eqeqeq": 2,
"eqeqeq": ["error", "smart"],
"func-names": 0,
"func-style": 0,
"generator-star-spacing": [2, {"before": true, "after": false}],
Expand Down
43 changes: 43 additions & 0 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,49 @@ describe('Type System: Object fields must have output types', () => {
});


describe('Type System: Object fields must have valid resolve values', () => {

function schemaWithObjectWithFieldResolver(resolveValue) {
const BadResolverType = new GraphQLObjectType({
name: 'BadResolver',
fields: {
badField: {
type: GraphQLString,
resolve: resolveValue
}
}
});

return new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
f: { type: BadResolverType }
}
})
});
}

it('accepts a lambda as an Object field resolver', () => {
expect(() => schemaWithObjectWithFieldResolver(() => ({}))).not.to.throw();
});

it('rejects an empty Object field resolver', () => {
expect(() => schemaWithObjectWithFieldResolver({})).to.throw(
'BadResolver.badField field resolver must be a function if provided, ' +
'but got: [object Object].'
);
});

it('rejects a constant scalar value resolver', () => {
expect(() => schemaWithObjectWithFieldResolver(0)).to.throw(
'BadResolver.badField field resolver must be a function if provided, ' +
'but got: 0.'
);
});
});


describe('Type System: Objects can only implement interfaces', () => {

function schemaWithObjectImplementingType(implementedType) {
Expand Down
10 changes: 10 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ function defineFieldMap<TSource, TContext>(
`${type.name}.${fieldName} field type must be Output Type but ` +
`got: ${String(field.type)}.`
);
invariant(
isValidResolver(field.resolve),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could probably just inline this as !field.resolve || typeof field.resolve === 'function'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"!field.resolve" means we will accept any "falsey" value such as 0 or the empty string. Is that what we want?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that it would let in falsey values. We could instead use field.resolve == null || typeof field.resolve === 'function' to be more specific

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, but the linter treats == as an error. I can update the linter to allow it if you feel strongly that == is more expressive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably enable == null in the linter. I think eslint now has the ability to whitelist that particular kind of usage. It's equivalent to the longer form.

`${type.name}.${fieldName} field resolver must be a function if ` +
`provided, but got: ${String(field.resolve)}.`
);
const argsConfig = fieldConfig.args;
if (!argsConfig) {
field.args = [];
Expand Down Expand Up @@ -540,6 +545,11 @@ function isPlainObj(obj) {
return obj && typeof obj === 'object' && !Array.isArray(obj);
}

// If a resolver is defined, it must be a function.
function isValidResolver(resolver: any): boolean {
return (resolver == null || typeof resolver === 'function');
}

export type GraphQLObjectTypeConfig<TSource, TContext> = {
name: string;
interfaces?: Thunk<?Array<GraphQLInterfaceType>>;
Expand Down