Skip to content

Commit

Permalink
fix(apollo-graphql): support complex directive args (#2335)
Browse files Browse the repository at this point in the history
This change fixes `transformSchema`, which previously left directives as-is
after transforming types. This meant that Input types used in directive
arguments would appear duplicated in the schema. It now recreates schema
directives and their arguments like it does with named schema types.

Fixes #2162
  • Loading branch information
lennyburdette authored May 24, 2021
1 parent 6ff7ccd commit 7753ddc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- `apollo-env`
- <First `apollo-env` related entry goes here>
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- Complex directive arguments don't break `transformSchema` (Fixes #2162)
- `apollo-language-server`
- <First `apollo-language-server` related entry goes here>
- `apollo-tools`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { buildSchema, printSchema } from "graphql";
import { transformSchema } from "../transformSchema";

describe("transformSchema", () => {
test("no-op transform leaves types untouched", () => {
const schema = buildSchema(`#graphql
type Query {
foo: String @test(baz: { bar: "hello" })
}
input DirectiveArg {
bar: String
}
# https://github.com/apollographql/apollo-tooling/issues/2162
directive @test(baz: DirectiveArg) on FIELD_DEFINITION
`);

const newSchema = transformSchema(schema, namedType => namedType);

expect(printSchema(newSchema)).toEqual(printSchema(schema));
});
});
16 changes: 14 additions & 2 deletions packages/apollo-graphql/src/schema/transformSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
GraphQLUnionType,
isInputObjectType,
GraphQLInputObjectType,
GraphQLInputFieldConfigMap
GraphQLInputFieldConfigMap,
GraphQLDirective
} from "graphql";
import { mapValues } from "../utilities/mapValues";

Expand Down Expand Up @@ -53,7 +54,8 @@ export function transformSchema(
types: Object.values(typeMap),
query: replaceMaybeType(schemaConfig.query),
mutation: replaceMaybeType(schemaConfig.mutation),
subscription: replaceMaybeType(schemaConfig.subscription)
subscription: replaceMaybeType(schemaConfig.subscription),
directives: replaceDirectives(schemaConfig.directives)
});

function recreateNamedType(type: GraphQLNamedType): GraphQLNamedType {
Expand Down Expand Up @@ -145,4 +147,14 @@ export function transformSchema(
type: replaceType(arg.type)
}));
}

function replaceDirectives(directives: GraphQLDirective[]) {
return directives.map(directive => {
const config = directive.toConfig();
return new GraphQLDirective({
...config,
args: replaceArgs(config.args)
});
});
}
}

0 comments on commit 7753ddc

Please sign in to comment.