Skip to content

Commit

Permalink
fix(wrap): transforming wrapped input fields (#4676)
Browse files Browse the repository at this point in the history
* fix: Fixed Transforming wrapped input fields

* chore: prettier changes

* fix: Added unit test

* fix: Refactored to use getNamedType

* chore: Added changeset

Co-authored-by: bruno.ribeiro <[email protected]>
  • Loading branch information
NullScope and bruno.ribeiro authored Aug 26, 2022
1 parent 4fe3d9c commit 4e4fac0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-kiwis-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/wrap': patch
---

Fix transforming/renaming Wrapped GraphQL Arguments
9 changes: 4 additions & 5 deletions packages/wrap/src/transforms/TransformInputObjectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
visitWithTypeInfo,
Kind,
FragmentDefinitionNode,
GraphQLInputObjectType,
ObjectValueNode,
ObjectFieldNode,
OperationDefinitionNode,
isInputType,
NamedTypeNode,
getNamedType,
} from 'graphql';

import { Maybe, ExecutionRequest, MapperKind, mapSchema, transformInputValue } from '@graphql-tools/utils';
import { ExecutionRequest, MapperKind, mapSchema, transformInputValue } from '@graphql-tools/utils';

import { Transform, DelegationContext, SubschemaConfig } from '@graphql-tools/delegate';

Expand Down Expand Up @@ -162,10 +162,9 @@ export default class TransformInputObjectFields<TContext = Record<string, any>>
visitWithTypeInfo(typeInfo, {
[Kind.OBJECT]: {
leave: (node: ObjectValueNode): ObjectValueNode | undefined => {
// The casting is kind of legit here as we are in a visitor
const parentType = typeInfo.getInputType() as Maybe<GraphQLInputObjectType>;
const parentType = typeInfo.getInputType();
if (parentType != null) {
const parentTypeName = parentType.name;
const parentTypeName = getNamedType(parentType).name;
const newInputFields: Array<ObjectFieldNode> = [];

for (const inputField of node.fields) {
Expand Down
56 changes: 56 additions & 0 deletions packages/wrap/tests/transformRenameInputObjectFields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,62 @@ describe('RenameInputObjectFields', () => {
expect(testData.field2).toBe('field2');
});

test('renaming with non-null arguments works', async () => {
const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
input InputObject {
field1: String
field2: String
}
type OutputObject {
field1: String
field2: String
}
type Query {
test(argument: InputObject!): OutputObject
}
`,
resolvers: {
Query: {
test: (_root, args) => {
return args.argument;
},
},
},
});

const transformedSchema = wrapSchema({
schema,
transforms: [
new RenameInputObjectFields((typeName, fieldName) => {
if (typeName === 'InputObject' && fieldName === 'field2') {
return 'field3';
}
}),
],
});

const query = /* GraphQL */ `
{
test(argument: { field1: "field1", field3: "field2" }) {
field1
field2
}
}
`;

const result = await execute({
schema: transformedSchema,
document: parse(query),
});
assertSome(result.data);
const testData: any = result.data['test'];
expect(testData.field1).toBe('field1');
expect(testData.field2).toBe('field2');
});

test('renaming with variables works', async () => {
const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
Expand Down

0 comments on commit 4e4fac0

Please sign in to comment.