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

Commit

Permalink
fix(delegateToSchema): standardize args format
Browse files Browse the repository at this point in the history
now that all directly proxied args are serialized automatically, including variables, appropriate to standardize args passed to
delegateToSchema.

Previously, the typeName would be inferred from the proxy target, but serialization would be according to the gateway schema if the type names matched. This would introduce a lot of edge cases and was only necessary as a workaround to facilitate serialization of variables.

Now we can standardize as follows: args format is the internal representation of the proxy target schema. By default, local schemas internal representation will be imported, so that local schema custom enums/scalars do not require manual serialization. For remote schemas should, the internal and external representation is the same, so this is the same as the external representation. If the gateway introduces a new internal representation, args must be converted to the old internal representation manually.

addresses #34
reverts fd1bd8f#diff-06b180be3290dd53bea4c1c98c9dfdd2
  • Loading branch information
yaacovCR committed Jan 21, 2020
1 parent 4683810 commit 1a7ffc8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function createDelegatingRequest({

if (args) {
transforms.push(
new AddArgumentsAsVariables(targetSchema, args, info.schema)
new AddArgumentsAsVariables(targetSchema, args)
);
}

Expand Down
30 changes: 6 additions & 24 deletions src/transforms/AddArgumentsAsVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@ import {
SelectionNode,
TypeNode,
VariableDefinitionNode,
GraphQLEnumType,
GraphQLScalarType,
} from 'graphql';
import { Request } from '../Interfaces';
import { Transform } from './transforms';
import { transformInputValue } from '../utils';
import { serializeInputValue } from '../utils';

export default class AddArgumentsAsVariablesTransform implements Transform {
private targetSchema: GraphQLSchema;
private args: { [key: string]: any };
private sourceSchema: GraphQLSchema;

constructor(targetSchema: GraphQLSchema, args: { [key: string]: any }, sourceSchema: GraphQLSchema) {
constructor(targetSchema: GraphQLSchema, args: { [key: string]: any }) {
this.targetSchema = targetSchema;
this.args = args;
this.sourceSchema = sourceSchema;
}

public transformRequest(originalRequest: Request): Request {
const { document, newVariables } = addVariablesToRootField(
this.targetSchema,
originalRequest.document,
this.args,
this.sourceSchema,
);
const variables = {
...originalRequest.variables,
Expand All @@ -54,7 +49,6 @@ function addVariablesToRootField(
targetSchema: GraphQLSchema,
document: DocumentNode,
args: { [key: string]: any },
sourceSchema: GraphQLSchema,
): {
document: DocumentNode;
newVariables: { [key: string]: any };
Expand Down Expand Up @@ -138,22 +132,10 @@ function addVariablesToRootField(
},
type: typeToAst(argument.type),
};
if (sourceSchema) {
newVariables[variableName] = transformInputValue(
argument.type,
args[argument.name],
(t, v) => {
const type = sourceSchema.getType(t.name) as GraphQLEnumType | GraphQLScalarType;
return type ? type.serialize(v) : v;
}
);
} else {
// tslint:disable-next-line:max-line-length
console.warn(
'AddArgumentsAsVariables should be passed the wrapping schema so that arguments can be properly serialized prior to delegation.'
);
newVariables[variableName] = args[argument.name];
}
newVariables[variableName] = serializeInputValue(
argument.type,
args[argument.name],
);
}
});

Expand Down

0 comments on commit 1a7ffc8

Please sign in to comment.