Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed Transforming wrapped input fields #4676

Merged
merged 5 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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