From 309e66e9d875ea63b8521d553c50075d039a295b Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Wed, 24 Aug 2022 12:23:55 +1000 Subject: [PATCH 1/6] Reproduction of https://github.com/graphql-java/graphql-java/issues/2933 --- .../schema/SchemaTransformerTest.groovy | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy b/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy index 6cda5ee32..63a8e102f 100644 --- a/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy +++ b/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy @@ -870,4 +870,41 @@ type Query { newSchema.getType("Foo") == null (newSchema.getObjectType("Query").getFieldDefinition("field").getType() as GraphQLScalarType).getName() == "Bar" } + + def "rename scalars are changed in applied arguments"() { + + def schema = TestUtil.schema(""" + scalar Foo + directive @myDirective(foo: Foo) on FIELD_DEFINITION + type Query { + field: Foo @myDirective + } +""") + + def visitor = new GraphQLTypeVisitorStub() { + + @Override + TraversalControl visitGraphQLScalarType(GraphQLScalarType node, TraverserContext context) { + if (node.getName().equals("Foo")) { + GraphQLScalarType newNode = node.transform({sc -> sc.name("Bar")}) + return changeNode(context, newNode) + } + return super.visitGraphQLScalarType(node, context) + } + } + + when: + def newSchema = SchemaTransformer.transformSchema(schema, visitor) + then: + newSchema.getType("Bar") instanceof GraphQLScalarType + newSchema.getType("Foo") == null + + def fieldDef = newSchema.getObjectType("Query").getFieldDefinition("field") + (fieldDef.getType() as GraphQLScalarType).getName() == "Bar" + + def appliedDirective = fieldDef.getAppliedDirective("myDirective") + (appliedDirective.getArgument("foo").getType() as GraphQLScalarType).getName() == "Bar" + + + } } From 63b36b3f10747e9d32dcd12f98fbe147f955d97e Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Wed, 24 Aug 2022 12:28:31 +1000 Subject: [PATCH 2/6] Reproduction of https://github.com/graphql-java/graphql-java/issues/2933 Old directive mechanism works --- .../graphql/schema/SchemaTransformerTest.groovy | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy b/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy index 63a8e102f..d38aaac5e 100644 --- a/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy +++ b/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy @@ -896,15 +896,18 @@ type Query { when: def newSchema = SchemaTransformer.transformSchema(schema, visitor) then: - newSchema.getType("Bar") instanceof GraphQLScalarType - newSchema.getType("Foo") == null def fieldDef = newSchema.getObjectType("Query").getFieldDefinition("field") - (fieldDef.getType() as GraphQLScalarType).getName() == "Bar" - def appliedDirective = fieldDef.getAppliedDirective("myDirective") + def oldDirective = fieldDef.getDirective("myDirective") + + (fieldDef.getType() as GraphQLScalarType).getName() == "Bar" + (oldDirective.getArgument("foo").getType() as GraphQLScalarType).getName() == "Bar" (appliedDirective.getArgument("foo").getType() as GraphQLScalarType).getName() == "Bar" + newSchema.getType("Bar") instanceof GraphQLScalarType + newSchema.getType("Foo") == null + } } From 6509a388c36bb5d4b049b836cf290002af2f7b35 Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Wed, 24 Aug 2022 12:39:15 +1000 Subject: [PATCH 3/6] Reproduction of https://github.com/graphql-java/graphql-java/issues/2933 Old directive mechanism works as does field arguments --- .../graphql/schema/SchemaTransformerTest.groovy | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy b/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy index d38aaac5e..3e88cce5f 100644 --- a/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy +++ b/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy @@ -877,7 +877,7 @@ type Query { scalar Foo directive @myDirective(foo: Foo) on FIELD_DEFINITION type Query { - field: Foo @myDirective + foo(foo: Foo) : Foo @myDirective } """) @@ -897,17 +897,21 @@ type Query { def newSchema = SchemaTransformer.transformSchema(schema, visitor) then: - def fieldDef = newSchema.getObjectType("Query").getFieldDefinition("field") + def fieldDef = newSchema.getObjectType("Query").getFieldDefinition("foo") def appliedDirective = fieldDef.getAppliedDirective("myDirective") - def oldDirective = fieldDef.getDirective("myDirective") + def oldSkoolDirective = fieldDef.getDirective("myDirective") + def argument = fieldDef.getArgument("foo") + def directive = newSchema.getDirective("myDirective") + def directiveArgument = directive.getArgument("foo") (fieldDef.getType() as GraphQLScalarType).getName() == "Bar" - (oldDirective.getArgument("foo").getType() as GraphQLScalarType).getName() == "Bar" + (argument.getType() as GraphQLScalarType).getName() == "Bar" + (directiveArgument.getType() as GraphQLScalarType).getName() == "Bar" + + (oldSkoolDirective.getArgument("foo").getType() as GraphQLScalarType).getName() == "Bar" (appliedDirective.getArgument("foo").getType() as GraphQLScalarType).getName() == "Bar" newSchema.getType("Bar") instanceof GraphQLScalarType newSchema.getType("Foo") == null - - } } From 0cf5ed94fec03586a7b2f1eafcd54b53bc902086 Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Wed, 24 Aug 2022 13:00:47 +1000 Subject: [PATCH 4/6] Reproduction of https://github.com/graphql-java/graphql-java/issues/2933 It seems to only be on applied directives --- .../graphql/schema/SchemaTransformerTest.groovy | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy b/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy index 3e88cce5f..c315553f1 100644 --- a/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy +++ b/src/test/groovy/graphql/schema/SchemaTransformerTest.groovy @@ -875,9 +875,9 @@ type Query { def schema = TestUtil.schema(""" scalar Foo - directive @myDirective(foo: Foo) on FIELD_DEFINITION + directive @myDirective(fooArgOnDirective: Foo) on FIELD_DEFINITION type Query { - foo(foo: Foo) : Foo @myDirective + foo(fooArgOnField: Foo) : Foo @myDirective } """) @@ -900,18 +900,20 @@ type Query { def fieldDef = newSchema.getObjectType("Query").getFieldDefinition("foo") def appliedDirective = fieldDef.getAppliedDirective("myDirective") def oldSkoolDirective = fieldDef.getDirective("myDirective") - def argument = fieldDef.getArgument("foo") - def directive = newSchema.getDirective("myDirective") - def directiveArgument = directive.getArgument("foo") + def argument = fieldDef.getArgument("fooArgOnField") + def directiveDecl = newSchema.getDirective("myDirective") + def directiveArgument = directiveDecl.getArgument("fooArgOnDirective") (fieldDef.getType() as GraphQLScalarType).getName() == "Bar" (argument.getType() as GraphQLScalarType).getName() == "Bar" (directiveArgument.getType() as GraphQLScalarType).getName() == "Bar" - (oldSkoolDirective.getArgument("foo").getType() as GraphQLScalarType).getName() == "Bar" - (appliedDirective.getArgument("foo").getType() as GraphQLScalarType).getName() == "Bar" + (oldSkoolDirective.getArgument("fooArgOnDirective").getType() as GraphQLScalarType).getName() == "Bar" newSchema.getType("Bar") instanceof GraphQLScalarType + + // not working at this stage + (appliedDirective.getArgument("fooArgOnDirective").getType() as GraphQLScalarType).getName() == "Bar" newSchema.getType("Foo") == null } } From d9c13996b05a184f53f62d68fb5b1cd343c5dc1b Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Wed, 24 Aug 2022 14:10:03 +1000 Subject: [PATCH 5/6] Reproduction of https://github.com/graphql-java/graphql-java/issues/2933 Better toString on Breadcrumbs --- src/main/java/graphql/util/Breadcrumb.java | 10 +++++++++- src/main/java/graphql/util/NodeLocation.java | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/graphql/util/Breadcrumb.java b/src/main/java/graphql/util/Breadcrumb.java index cae637efd..ad1415a00 100644 --- a/src/main/java/graphql/util/Breadcrumb.java +++ b/src/main/java/graphql/util/Breadcrumb.java @@ -3,6 +3,7 @@ import graphql.PublicApi; import java.util.Objects; +import java.util.StringJoiner; /** * A specific {@link NodeLocation} inside a node. This means {@link #getNode()} returns a Node which has a child @@ -47,9 +48,16 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = 1; - result = 31 * result + Objects.hashCode(node); + result = 31 * result + Objects.hashCode(node); result = 31 * result + Objects.hashCode(location); return result; } + @Override + public String toString() { + return new StringJoiner(", ", "[", "]") + .add("" + location) + .add("" + node) + .toString(); + } } diff --git a/src/main/java/graphql/util/NodeLocation.java b/src/main/java/graphql/util/NodeLocation.java index 61851b0b6..8a1f9f10b 100644 --- a/src/main/java/graphql/util/NodeLocation.java +++ b/src/main/java/graphql/util/NodeLocation.java @@ -33,7 +33,7 @@ public int getIndex() { @Override public String toString() { - return "NodeLocation{" + + return "{" + "name='" + name + '\'' + ", index=" + index + '}'; From 431affc8aae4e3d105feda2e98fb99cd8fe5c8da Mon Sep 17 00:00:00 2001 From: Andreas Marek Date: Wed, 24 Aug 2022 15:59:11 +1000 Subject: [PATCH 6/6] fix missing GraphQLAppliedDirectiveArgument: type child was not considered --- .../schema/GraphQLAppliedDirectiveArgument.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/graphql/schema/GraphQLAppliedDirectiveArgument.java b/src/main/java/graphql/schema/GraphQLAppliedDirectiveArgument.java index dd595a539..594242520 100644 --- a/src/main/java/graphql/schema/GraphQLAppliedDirectiveArgument.java +++ b/src/main/java/graphql/schema/GraphQLAppliedDirectiveArgument.java @@ -11,6 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; @@ -34,6 +35,8 @@ public class GraphQLAppliedDirectiveArgument implements GraphQLNamedSchemaElemen private final Argument definition; + public static final String CHILD_TYPE = "type"; + private GraphQLAppliedDirectiveArgument(String name, InputValueWithState value, GraphQLInputType type, @@ -104,19 +107,23 @@ public Argument getDefinition() { @Override public List getChildren() { - return ImmutableKit.emptyList(); + List children = new ArrayList<>(); + children.add(getType()); + return children; } - @Override public SchemaElementChildrenContainer getChildrenWithTypeReferences() { return SchemaElementChildrenContainer.newSchemaElementChildrenContainer() + .child(CHILD_TYPE, originalType) .build(); } @Override public GraphQLAppliedDirectiveArgument withNewChildren(SchemaElementChildrenContainer newChildren) { - return this; + return transform(builder -> + builder.type(newChildren.getChildOrNull(CHILD_TYPE)) + ); } @Override