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 object level default resolve function #734

Closed
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
22 changes: 20 additions & 2 deletions src/execution/__tests__/resolve-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import {

describe('Execute: resolve function', () => {

function testSchema(testField) {
function testSchema(testField, resolve) {
return new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
resolve,
fields: {
test: testField
}
Expand Down Expand Up @@ -94,7 +95,7 @@ describe('Execute: resolve function', () => {
});
});

it('uses provided resolve function', async () => {
it('uses field provided resolve function', async () => {
const schema = testSchema({
type: GraphQLString,
args: {
Expand Down Expand Up @@ -139,4 +140,21 @@ describe('Execute: resolve function', () => {
});
});

it('uses object provided resolve function', async () => {
const schema = testSchema({
type: GraphQLString
},
() => {
return 'defaultResult';
});

expect(
await graphql(schema, '{ test }')
).to.deep.equal({
data: {
test: 'defaultResult'
}
});
});

});
8 changes: 7 additions & 1 deletion src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,13 @@ function resolveField(
}

const returnType = fieldDef.type;
const resolveFn = fieldDef.resolve || defaultFieldResolver;

// If no resolve function is specified on the field object, graphQL will
// fall back to the parent type's resolve function followed by the
// built-in default resolver.
const resolveFn = fieldDef.resolve ||
parentType.resolve ||
defaultFieldResolver;

// The resolve function's optional third argument is a context value that
// is provided to every resolve function within an execution. It is commonly
Expand Down
20 changes: 20 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,28 @@ export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
* })
* });
*
* You can specify a root level resolve function when creating a new object
* type. This function will be used as the default resolver for any fields
* that do not specify their own resolve function.
*
* Example:
*
* const ArticleType = new GraphQLObjectType({
* name: 'Article',
* resolve: (obj, args, context, info) => {
* return context.loader(obj, info.fieldName)
* },
* fields: () => ({
* title: { type: GraphQLString },
* text: { type: GraphQLString }
* })
* });
*
*/
export class GraphQLObjectType {
name: string;
description: ?string;
resolve: ?GraphQLFieldResolver<*, *>
isTypeOf: ?GraphQLIsTypeOfFn<*, *>;

_typeConfig: GraphQLObjectTypeConfig<*, *>;
Expand All @@ -401,6 +419,7 @@ export class GraphQLObjectType {
assertValidName(config.name, config.isIntrospection);
this.name = config.name;
this.description = config.description;
this.resolve = config.resolve;
if (config.isTypeOf) {
invariant(
typeof config.isTypeOf === 'function',
Expand Down Expand Up @@ -555,6 +574,7 @@ function isValidResolver(resolver: any): boolean {

export type GraphQLObjectTypeConfig<TSource, TContext> = {
name: string;
resolve?: GraphQLFieldResolver<TSource, TContext>;
interfaces?: Thunk<?Array<GraphQLInterfaceType>>;
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>;
isTypeOf?: ?GraphQLIsTypeOfFn<TSource, TContext>;
Expand Down