From db6ff47d424a3a62715b5d09d6d8ccb1335ac87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dimitri=20Ko=CC=88nig?= Date: Wed, 2 Aug 2017 19:16:19 +0200 Subject: [PATCH] Use defaultFieldResolver from graphql-js package instead of own one --- src/schemaGenerator.ts | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/src/schemaGenerator.ts b/src/schemaGenerator.ts index 779df317c57..55c55a59166 100644 --- a/src/schemaGenerator.ts +++ b/src/schemaGenerator.ts @@ -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, @@ -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); }); }; } @@ -427,7 +427,7 @@ function chainResolvers(resolvers: GraphQLFieldResolver[]) { return curResolver(prev, args, ctx, info); } - return defaultResolveFn(prev, args, ctx, info); + return defaultFieldResolver(prev, args, ctx, info); }, root, ); @@ -441,7 +441,7 @@ function chainResolvers(resolvers: GraphQLFieldResolver[]) { */ function decorateWithLogger(fn: GraphQLFieldResolver | undefined, logger: ILogger, hint: string): GraphQLFieldResolver { if (typeof fn === 'undefined') { - fn = defaultResolveFn; + fn = defaultFieldResolver; } const logError = (e: Error) => { @@ -488,7 +488,7 @@ function addCatchUndefinedToSchema(schema: GraphQLSchema): void { function decorateToCatchUndefined(fn: GraphQLFieldResolver, hint: string): GraphQLFieldResolver { if (typeof fn === 'undefined') { - fn = defaultResolveFn; + fn = defaultFieldResolver; } return (root, args, ctx, info) => { const result = fn(root, args, ctx, info); @@ -520,27 +520,6 @@ function runAtMostOncePerRequest(fn: GraphQLFieldResolver): 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,