Skip to content

Commit

Permalink
Fix: resolve correct data if schema is wrapped (#1562)
Browse files Browse the repository at this point in the history
* Fix wrong proxying subscriptios

* fix

* Use targetFieldName
  • Loading branch information
ardatan committed May 30, 2020
1 parent 81c85fd commit fb48136
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
16 changes: 14 additions & 2 deletions packages/delegate/src/delegateToSchema.ts
Expand Up @@ -271,10 +271,22 @@ export function delegateRequest({

function createDefaultExecutor(schema: GraphQLSchema, rootValue: Record<string, any>) {
return ({ document, context, variables, info }: ExecutionParams) =>
execute(schema, document, rootValue ?? info?.rootValue, context, variables);
execute({
schema,
document,
contextValue: context,
variableValues: variables,
rootValue: rootValue ?? info?.rootValue,
});
}

function createDefaultSubscriber(schema: GraphQLSchema, rootValue: Record<string, any>) {
return ({ document, context, variables, info }: ExecutionParams) =>
subscribe(schema, document, rootValue ?? info?.rootValue, context, variables) as any;
subscribe({
schema,
document,
contextValue: context,
variableValues: variables,
rootValue: rootValue ?? info?.rootValue,
}) as any;
}
18 changes: 12 additions & 6 deletions packages/wrap/src/generateProxyingResolvers.ts
@@ -1,4 +1,4 @@
import { GraphQLSchema, GraphQLFieldResolver, GraphQLObjectType } from 'graphql';
import { GraphQLSchema, GraphQLFieldResolver, GraphQLObjectType, GraphQLResolveInfo } from 'graphql';

import { Transform, Operation, getResponseKeyFromInfo, getErrors, applySchemaTransforms } from '@graphql-tools/utils';
import {
Expand Down Expand Up @@ -44,8 +44,6 @@ export function generateProxyingResolvers(

const resolvers = {};
Object.keys(operationTypes).forEach((operation: Operation) => {
const resolveField = operation === 'subscription' ? 'subscribe' : 'resolve';

const rootType = operationTypes[operation];
if (rootType != null) {
const typeName = rootType.name;
Expand All @@ -63,9 +61,17 @@ export function generateProxyingResolvers(

const finalResolver = createPossiblyNestedProxyingResolver(subschemaOrSubschemaConfig, proxyingResolver);

resolvers[typeName][fieldName] = {
[resolveField]: finalResolver,
};
if (operation === 'subscription') {
resolvers[typeName][fieldName] = {
subscribe: finalResolver,
resolve: (payload: any, _: never, __: never, { fieldName: targetFieldName }: GraphQLResolveInfo) =>
payload[targetFieldName],
};
} else {
resolvers[typeName][fieldName] = {
resolve: finalResolver,
};
}
});
}
});
Expand Down

0 comments on commit fb48136

Please sign in to comment.