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

Use defaultFieldResolver from graphql-js package instead of own one #373

Merged
Merged
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
31 changes: 5 additions & 26 deletions src/schemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// TODO: we should refactor this file, rename it to makeExecutableSchema, and move
// a bunch of utility functions into a separate utitlities folder, one file per function.

import { DocumentNode, parse, print, Kind, DefinitionNode } from 'graphql';
import { DocumentNode, parse, print, Kind, DefinitionNode, defaultFieldResolver } from 'graphql';
import { buildASTSchema, extendSchema } from 'graphql';
import {
GraphQLScalarType,
Expand Down Expand Up @@ -414,7 +414,7 @@ function wrapResolver(
if (innerResolver) {
return innerResolver(root, args, ctx, info);
}
return defaultResolveFn(root, args, ctx, info);
return defaultFieldResolver(root, args, ctx, info);
});
};
}
Expand All @@ -427,7 +427,7 @@ function chainResolvers(resolvers: GraphQLFieldResolver<any, any>[]) {
return curResolver(prev, args, ctx, info);
}

return defaultResolveFn(prev, args, ctx, info);
return defaultFieldResolver(prev, args, ctx, info);
},
root,
);
Expand All @@ -441,7 +441,7 @@ function chainResolvers(resolvers: GraphQLFieldResolver<any, any>[]) {
*/
function decorateWithLogger(fn: GraphQLFieldResolver<any, any> | undefined, logger: ILogger, hint: string): GraphQLFieldResolver<any, any> {
if (typeof fn === 'undefined') {
fn = defaultResolveFn;
fn = defaultFieldResolver;
}

const logError = (e: Error) => {
Expand Down Expand Up @@ -488,7 +488,7 @@ function addCatchUndefinedToSchema(schema: GraphQLSchema): void {

function decorateToCatchUndefined(fn: GraphQLFieldResolver<any, any>, hint: string): GraphQLFieldResolver<any, any> {
if (typeof fn === 'undefined') {
fn = defaultResolveFn;
fn = defaultFieldResolver;
}
return (root, args, ctx, info) => {
const result = fn(root, args, ctx, info);
Expand Down Expand Up @@ -520,27 +520,6 @@ function runAtMostOncePerRequest(fn: GraphQLFieldResolver<any, any>): GraphQLFie
};
}

/**
* XXX taken from graphql-js: src/execution/execute.js, because that function
* is not exported
*
* If a resolve function is not given, then a default resolve behavior is used
* which takes the property of the source object of the same name as the field
* and returns it as the result, or if it's a function, returns the result
* of calling that function.
*/
function defaultResolveFn(
source: any, args: any, context: any, { fieldName }: { fieldName: string}) {
// ensure source is a value for which property access is acceptable.
if (typeof source === 'object' || typeof source === 'function') {
const property = source[fieldName];
if (typeof property === 'function') {
return property(args, context);
}
return property;
}
}

export {
makeExecutableSchema,
SchemaError,
Expand Down