Skip to content

Commit

Permalink
remove use of createNamedStub
Browse files Browse the repository at this point in the history
and add comment to explain how this works
  • Loading branch information
yaacovCR committed May 7, 2020
1 parent bbd1c9d commit 5a16871
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions packages/wrap/src/transforms/WrapFields.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLSchema, GraphQLObjectType } from 'graphql';

import { Transform, Request, hoistFieldNodes, getFields, modifyFields, createNamedStub } from '@graphql-tools/utils';
import { Transform, Request, hoistFieldNodes, getFields, modifyFields } from '@graphql-tools/utils';
import { createMergedResolver, defaultMergedResolver } from '@graphql-tools/delegate';

import MapFields from './MapFields';
Expand Down Expand Up @@ -41,7 +41,7 @@ export default class WrapFields implements Transform {
}

public transformSchema(schema: GraphQLSchema): GraphQLSchema {
const targetFieldConfigMap = getFields(
let targetFieldConfigMap = getFields(
schema,
this.outerTypeName,
!this.fieldNames ? () => true : fieldName => this.fieldNames.includes(fieldName)
Expand All @@ -59,6 +59,19 @@ export default class WrapFields implements Transform {
let wrapIndex = this.numWraps - 1;

const innerMostWrappingTypeName = this.wrappingTypeNames[wrapIndex];

let baseWrappingType = new GraphQLObjectType({
name: innerMostWrappingTypeName,
fields: targetFieldConfigMap,
});

// Appending is still necessary to support wrapping with a pre-existing type.
// modifyFields lets you use the incomplete type within a field config map
// as it employes rewiring and will use the correct final type.
//
// In fact, the baseWrappingType just could even be just a stub type
// with the correct name.

const append = [
{
typeName: innerMostWrappingTypeName,
Expand All @@ -67,22 +80,31 @@ export default class WrapFields implements Transform {
];

for (wrapIndex--; wrapIndex > -1; wrapIndex--) {
append.push({
typeName: this.wrappingTypeNames[wrapIndex],
additionalFields: {
[this.wrappingFieldNames[wrapIndex + 1]]: {
type: createNamedStub(this.wrappingTypeNames[wrapIndex + 1], 'object') as GraphQLObjectType,
resolve: defaultMergedResolver,
},
targetFieldConfigMap = {
[this.wrappingFieldNames[wrapIndex + 1]]: {
type: baseWrappingType,
resolve: defaultMergedResolver,
},
};

const wrappingTypeName = this.wrappingTypeNames[wrapIndex];

baseWrappingType = new GraphQLObjectType({
name: wrappingTypeName,
fields: targetFieldConfigMap,
});

append.push({
typeName: wrappingTypeName,
additionalFields: targetFieldConfigMap,
});
}

append.push({
typeName: this.outerTypeName,
additionalFields: {
[this.wrappingFieldNames[0]]: {
type: createNamedStub(this.wrappingTypeNames[0], 'object') as GraphQLObjectType,
type: baseWrappingType,
resolve: createMergedResolver({ dehoist: true }),
},
},
Expand Down

0 comments on commit 5a16871

Please sign in to comment.