Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
feat(transforms): map object fields to new structures
Browse files Browse the repository at this point in the history
Provides building blocks to allow transforming object types by wrapping a field with a new object type or by extracting subfields from a field of a certain object type into the parent:

 = Adds new ExtendSchema transformer to extend the wrapping schema within transformSchema with new fields.
 = Adds new functions that create resolvers that can wrap, extract, and rename merged fields, for use within the ExtendSchema transformer
 = Adds new MapFields transformer to allow transformation of field nodes within a request by object type and field name.
  • Loading branch information
yaacovCR committed Aug 13, 2019
1 parent 800c91d commit d017bcd
Show file tree
Hide file tree
Showing 11 changed files with 723 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IResolverValidationOptions {
export interface IAddResolveFunctionsToSchemaOptions {
schema: GraphQLSchema;
resolvers: IResolvers;
defaultFieldResolver?: IFieldResolver<any, any>;
resolverValidationOptions?: IResolverValidationOptions;
inheritResolversFromInterfaces?: boolean;
}
Expand Down
12 changes: 7 additions & 5 deletions src/generate/addResolveFunctionsToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../Interfaces';
import { applySchemaTransforms } from '../transforms/transforms';
import { checkForResolveTypeResolver, extendResolversFromInterfaces } from '.';
import AddEnumAndScalarResolvers from '../transforms/AddEnumAndScalarResolvers';
import { AddDefaultResolver, AddEnumAndScalarResolvers } from '../transforms/index';

function addResolveFunctionsToSchema(
options: IAddResolveFunctionsToSchemaOptions | GraphQLSchema,
Expand All @@ -39,6 +39,7 @@ function addResolveFunctionsToSchema(
const {
schema,
resolvers: inputResolvers,
defaultFieldResolver,
resolverValidationOptions = {},
inheritResolversFromInterfaces = false,
} = options;
Expand Down Expand Up @@ -153,12 +154,13 @@ function addResolveFunctionsToSchema(

checkForResolveTypeResolver(schema, requireResolversForResolveType);

// If there are any enum resolver functions (that are used to return
// internal enum values), create a new schema that includes enums with the
// new internal facing values.
// also parse all defaultValues in all input fields to use internal values for enums/scalars
const updatedSchema = applySchemaTransforms(schema, [
// If there are any enum resolver functions (that are used to return
// internal enum values), create a new schema that includes enums with the
// new internal facing values.
// also parse all defaultValues in all input fields to use internal values for enums/scalars
new AddEnumAndScalarResolvers(enumValueMap, scalarTypeMap),
new AddDefaultResolver(defaultFieldResolver),
]);

return updatedSchema;
Expand Down
7 changes: 2 additions & 5 deletions src/stitching/defaultMergedResolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLFieldResolver } from 'graphql';
import { GraphQLFieldResolver, defaultFieldResolver } from 'graphql';
import { getErrorsFromParent } from './errors';
import { handleResult } from './checkResultAndHandleErrors';
import { getResponseKeyFromInfo } from './getResponseKeyFromInfo';
Expand All @@ -18,10 +18,7 @@ const defaultMergedResolver: GraphQLFieldResolver<any, any> = (parent, args, con
// check to see if parent is not a proxied result, i.e. if parent resolver was manually overwritten
// See https://github.com/apollographql/graphql-tools/issues/967
if (!Array.isArray(errors)) {
if (typeof parent[info.fieldName] === 'function') {
return parent[info.fieldName](parent, args, context, info);
}
return parent[info.fieldName];
return defaultFieldResolver(parent, args, context, info);
}

return handleResult(info, parent[responseKey], errors);
Expand Down

0 comments on commit d017bcd

Please sign in to comment.