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

Commit

Permalink
refactor(AddArgumentsAsVariables): to AddArguments
Browse files Browse the repository at this point in the history
Why add arguments as variables when you can just add as an AST with graphql utility method?
  • Loading branch information
yaacovCR committed Jan 21, 2020
1 parent 62c9684 commit 58038c4
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/source/schema-transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ transforms: [
The following transforms are automatically applied by `delegateToSchema` during schema delegation, to translate between new and old types and fields:
* `AddArgumentsAsVariables`: Given a schema and arguments passed to a root field, make those arguments document variables.
* `AddArguments`: Given a schema and arguments passed to a root field, add arguments to request.
* `ExpandAbstractTypes`: If an abstract type within a document does not exist in the inner schema, expand the type to each and any of its implementations that do exist in the inner schema.
* `FilterToSchema`: Given a schema and document, remove all fields, variables and fragments for types that don't exist in that schema.
* `AddTypenameToAbstract`: Add `__typename` to all abstract types in the document, necessary for type resolution of interfaces within the outer schema to work.
Expand Down
4 changes: 2 additions & 2 deletions src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
applyResultTransforms,
} from '../transforms/transforms';

import AddArgumentsAsVariables from '../transforms/AddArgumentsAsVariables';
import AddArguments from '../transforms/AddArguments';
import FilterToSchema from '../transforms/FilterToSchema';
import AddTypenameToAbstract from '../transforms/AddTypenameToAbstract';
import CheckResultAndHandleErrors from '../transforms/CheckResultAndHandleErrors';
Expand Down Expand Up @@ -149,7 +149,7 @@ export function createDelegatingRequest({

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

Expand Down
108 changes: 108 additions & 0 deletions src/transforms/AddArguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
ArgumentNode,
DocumentNode,
FragmentDefinitionNode,
GraphQLArgument,
GraphQLField,
GraphQLObjectType,
GraphQLSchema,
Kind,
OperationDefinitionNode,
SelectionNode,
astFromValue,
} from 'graphql';
import { Request } from '../Interfaces';
import { Transform } from './transforms';

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

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

public transformRequest(originalRequest: Request): Request {
const document = addVariablesToRootField(
this.targetSchema,
originalRequest.document,
this.args,
);
return {
...originalRequest,
document,
};
}
}

function addVariablesToRootField(
targetSchema: GraphQLSchema,
document: DocumentNode,
args: { [key: string]: any },
): DocumentNode {
const operations: Array<
OperationDefinitionNode
> = document.definitions.filter(
def => def.kind === Kind.OPERATION_DEFINITION,
) as Array<OperationDefinitionNode>;
const fragments: Array<FragmentDefinitionNode> = document.definitions.filter(
def => def.kind === Kind.FRAGMENT_DEFINITION,
) as Array<FragmentDefinitionNode>;

const newOperations = operations.map((operation: OperationDefinitionNode) => {
let type: GraphQLObjectType;
if (operation.operation === 'subscription') {
type = targetSchema.getSubscriptionType();
} else if (operation.operation === 'mutation') {
type = targetSchema.getMutationType();
} else {
type = targetSchema.getQueryType();
}

const newSelectionSet: Array<SelectionNode> = [];

operation.selectionSet.selections.forEach((selection: SelectionNode) => {
if (selection.kind === Kind.FIELD) {
let newArgs: { [name: string]: ArgumentNode } = {};
selection.arguments.forEach((argument: ArgumentNode) => {
newArgs[argument.name.value] = argument;
});
const name: string = selection.name.value;
const field: GraphQLField<any, any> = type.getFields()[name];
field.args.forEach((argument: GraphQLArgument) => {
if (argument.name in args) {
newArgs[argument.name] = {
kind: Kind.ARGUMENT,
name: {
kind: Kind.NAME,
value: argument.name,
},
value: astFromValue(args[argument.name], argument.type),
};
}
});

newSelectionSet.push({
...selection,
arguments: Object.keys(newArgs).map(argName => newArgs[argName]),
});
} else {
newSelectionSet.push(selection);
}
});

return {
...operation,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: newSelectionSet,
},
};
});

return {
...document,
definitions: [...newOperations, ...fragments],
};
}
15 changes: 10 additions & 5 deletions src/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ export { Transform };
export { default as filterSchema } from './filterSchema';
export { default as transformSchema, wrapSchema } from './transformSchema';

export { default as AddArgumentsAsVariables } from './AddArgumentsAsVariables';
export { default as CheckResultAndHandleErrors } from './CheckResultAndHandleErrors';
export { default as ExpandAbstractTypes } from './ExpandAbstractTypes';
export { default as AddReplacementFragments } from './AddReplacementFragments';
export { default as AddMergedTypeFragments } from './AddMergedTypeFragments';
export { default as AddTypenameToAbstract } from './AddTypenameToAbstract';
export { default as AddArguments } from './AddArguments';
export { default as FilterToSchema } from './FilterToSchema';
export { default as AddTypenameToAbstract } from './AddTypenameToAbstract';

export { default as RenameTypes } from './RenameTypes';
export { default as FilterTypes } from './FilterTypes';
Expand All @@ -19,14 +20,18 @@ export { default as FilterRootFields } from './FilterRootFields';
export { default as TransformObjectFields } from './TransformObjectFields';
export { default as RenameObjectFields } from './RenameObjectFields';
export { default as FilterObjectFields } from './FilterObjectFields';
export { default as ExpandAbstractTypes } from './ExpandAbstractTypes';
export { default as ExtractField } from './ExtractField';
export { default as WrapQuery } from './WrapQuery';
export { default as TransformQuery } from './TransformQuery';

export { default as ExtendSchema } from './ExtendSchema';
export { default as WrapType } from './WrapType';
export { default as WrapFields } from './WrapFields';
export { default as HoistField } from './HoistField';
export { default as MapFields } from './MapFields';

// superseded by AddReplacementFragments
export { default as ReplaceFieldWithFragment } from './ReplaceFieldWithFragment';
// superseded by AddArguments
export { default as AddArgumentsAsVariables } from './AddArgumentsAsVariables';
// superseded by TransformQuery
export { default as WrapQuery } from './WrapQuery';
export { default as ExtractField } from './ExtractField';

0 comments on commit 58038c4

Please sign in to comment.