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

Commit

Permalink
feat(stitching): add returnType option to delegateToSchema
Browse files Browse the repository at this point in the history
facilitates proxying from objects to lists or possibly otherwise incompatible schemas, see #33.
  • Loading branch information
yaacovCR committed Jan 16, 2020
1 parent 3545d73 commit abe0ee8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
GraphQLInterfaceType,
GraphQLObjectType,
InlineFragmentNode,
GraphQLOutputType,
} from 'graphql';

import { SchemaDirectiveVisitor } from './utils/SchemaDirectiveVisitor';
Expand Down Expand Up @@ -115,6 +116,7 @@ export interface IDelegateToSchemaOptions<TContext = { [key: string]: any }> {
schema: GraphQLSchema | SubschemaConfig;
operation: Operation;
fieldName: string;
returnType?: GraphQLOutputType;
args?: { [key: string]: any };
context: TContext;
info: IGraphQLToolsResolveInfo;
Expand Down
7 changes: 5 additions & 2 deletions src/stitching/checkResultAndHandleErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
GraphQLCompositeType,
GraphQLError,
GraphQLList,
GraphQLOutputType,
GraphQLType,
GraphQLSchema,
FieldNode,
Expand All @@ -34,6 +35,7 @@ export function checkResultAndHandleErrors(
info: GraphQLResolveInfo,
responseKey?: string,
subschema?: GraphQLSchema | SubschemaConfig,
returnType: GraphQLOutputType = info.returnType,
): any {
if (!responseKey) {
responseKey = getResponseKeyFromInfo(info);
Expand All @@ -43,7 +45,7 @@ export function checkResultAndHandleErrors(
const data = result.data && result.data[responseKey];
const subschemas = [subschema];

return handleResult(data, errors, subschemas, context, info);
return handleResult(data, errors, subschemas, context, info, returnType);
}

export function handleResult(
Expand All @@ -52,8 +54,9 @@ export function handleResult(
subschemas: Array<GraphQLSchema | SubschemaConfig>,
context: Record<string, any>,
info: IGraphQLToolsResolveInfo,
returnType = info.returnType,
): any {
const type = getNullableType(info.returnType);
const type = getNullableType(returnType);

if (result == null) {
return handleNull(info.fieldNodes, responsePathAsArray(info.path), errors);
Expand Down
3 changes: 2 additions & 1 deletion src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function delegateToSchemaImplementation({
info,
operation = info.operation.operation,
fieldName,
returnType = info.returnType,
args,
context,
transforms = [],
Expand Down Expand Up @@ -101,7 +102,7 @@ function delegateToSchemaImplementation({
};

transforms = [
new CheckResultAndHandleErrors(info, fieldName, subschema, context),
new CheckResultAndHandleErrors(info, fieldName, subschema, context, returnType),
...transforms,
new ExpandAbstractTypes(info.schema, targetSchema),
];
Expand Down
6 changes: 2 additions & 4 deletions src/test/testDataloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ describe('dataloader', () => {
ids: keys.map((k: { id: any }) => k.id)
},
context: null,
info: {
...keys[0].info,
returnType: new GraphQLList(keys[0].info.returnType),
}
info: keys[0].info,
returnType: new GraphQLList(keys[0].info.returnType),
});

expect(users).to.deep.equal([{
Expand Down
8 changes: 6 additions & 2 deletions src/transforms/CheckResultAndHandleErrors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLSchema } from 'graphql';
import { GraphQLSchema, GraphQLOutputType } from 'graphql';
import { checkResultAndHandleErrors } from '../stitching/checkResultAndHandleErrors';
import { Transform } from './transforms';
import { SubschemaConfig, IGraphQLToolsResolveInfo } from '../Interfaces';
Expand All @@ -8,17 +8,20 @@ export default class CheckResultAndHandleErrors implements Transform {
private info: IGraphQLToolsResolveInfo;
private fieldName?: string;
private subschema?: GraphQLSchema | SubschemaConfig;
private returnType?: GraphQLOutputType;

constructor(
info: IGraphQLToolsResolveInfo,
fieldName?: string,
subschema?: GraphQLSchema | SubschemaConfig,
context?: Record<string, any>,
returnType: GraphQLOutputType = info.returnType,
) {
this.context = context;
this.info = info;
this.fieldName = fieldName;
this.subschema = subschema;
this.returnType = returnType;
}

public transformResult(result: any): any {
Expand All @@ -27,7 +30,8 @@ export default class CheckResultAndHandleErrors implements Transform {
this.context,
this.info,
this.fieldName,
this.subschema
this.subschema,
this.returnType,
);
}
}

0 comments on commit abe0ee8

Please sign in to comment.