Skip to content

Commit 7753ddc

Browse files
fix(apollo-graphql): support complex directive args (#2335)
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
1 parent 6ff7ccd commit 7753ddc

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- `apollo-env`
1818
- <First `apollo-env` related entry goes here>
1919
- `apollo-graphql`
20-
- <First `apollo-graphql` related entry goes here>
20+
- Complex directive arguments don't break `transformSchema` (Fixes #2162)
2121
- `apollo-language-server`
2222
- <First `apollo-language-server` related entry goes here>
2323
- `apollo-tools`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { buildSchema, printSchema } from "graphql";
2+
import { transformSchema } from "../transformSchema";
3+
4+
describe("transformSchema", () => {
5+
test("no-op transform leaves types untouched", () => {
6+
const schema = buildSchema(`#graphql
7+
type Query {
8+
foo: String @test(baz: { bar: "hello" })
9+
}
10+
11+
input DirectiveArg {
12+
bar: String
13+
}
14+
15+
# https://github.com/apollographql/apollo-tooling/issues/2162
16+
directive @test(baz: DirectiveArg) on FIELD_DEFINITION
17+
`);
18+
19+
const newSchema = transformSchema(schema, namedType => namedType);
20+
21+
expect(printSchema(newSchema)).toEqual(printSchema(schema));
22+
});
23+
});

packages/apollo-graphql/src/schema/transformSchema.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
GraphQLUnionType,
2020
isInputObjectType,
2121
GraphQLInputObjectType,
22-
GraphQLInputFieldConfigMap
22+
GraphQLInputFieldConfigMap,
23+
GraphQLDirective
2324
} from "graphql";
2425
import { mapValues } from "../utilities/mapValues";
2526

@@ -53,7 +54,8 @@ export function transformSchema(
5354
types: Object.values(typeMap),
5455
query: replaceMaybeType(schemaConfig.query),
5556
mutation: replaceMaybeType(schemaConfig.mutation),
56-
subscription: replaceMaybeType(schemaConfig.subscription)
57+
subscription: replaceMaybeType(schemaConfig.subscription),
58+
directives: replaceDirectives(schemaConfig.directives)
5759
});
5860

5961
function recreateNamedType(type: GraphQLNamedType): GraphQLNamedType {
@@ -145,4 +147,14 @@ export function transformSchema(
145147
type: replaceType(arg.type)
146148
}));
147149
}
150+
151+
function replaceDirectives(directives: GraphQLDirective[]) {
152+
return directives.map(directive => {
153+
const config = directive.toConfig();
154+
return new GraphQLDirective({
155+
...config,
156+
args: replaceArgs(config.args)
157+
});
158+
});
159+
}
148160
}

0 commit comments

Comments
 (0)