From d778868655d875843a33796f2dd96e6ffb5c3982 Mon Sep 17 00:00:00 2001 From: dondonz <13839920+dondonz@users.noreply.github.com> Date: Fri, 23 Feb 2024 18:22:11 +1100 Subject: [PATCH 1/2] Add extensionDefinitions to schema builder --- .../java/graphql/schema/GraphQLSchema.java | 1 + .../graphql/schema/GraphQLSchemaTest.groovy | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/graphql/schema/GraphQLSchema.java b/src/main/java/graphql/schema/GraphQLSchema.java index e365ef774b..313403c767 100644 --- a/src/main/java/graphql/schema/GraphQLSchema.java +++ b/src/main/java/graphql/schema/GraphQLSchema.java @@ -641,6 +641,7 @@ public static Builder newSchema(GraphQLSchema existingSchema) { .query(existingSchema.getQueryType()) .mutation(existingSchema.getMutationType()) .subscription(existingSchema.getSubscriptionType()) + .extensionDefinitions(existingSchema.getExtensionDefinitions()) .introspectionSchemaType(existingSchema.getIntrospectionSchemaType()) .codeRegistry(existingSchema.getCodeRegistry()) .clearAdditionalTypes() diff --git a/src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy b/src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy index de2a51bf04..cd8b1c990f 100644 --- a/src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy +++ b/src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy @@ -5,6 +5,8 @@ import graphql.Directives import graphql.ExecutionInput import graphql.GraphQL import graphql.TestUtil +import graphql.language.Directive +import graphql.language.SchemaExtensionDefinition import graphql.schema.idl.RuntimeWiring import graphql.schema.idl.TypeRuntimeWiring import graphql.util.TraversalControl @@ -129,6 +131,22 @@ class GraphQLSchemaTest extends Specification { )) } + def "schema builder copies extension definitions"() { + setup: + def schemaBuilder = basicSchemaBuilder() + def newDirective = Directive.newDirective().name("hello").build() + def extension = SchemaExtensionDefinition.newSchemaExtensionDefinition().directive(newDirective).build() + def oldSchema = schemaBuilder.extensionDefinitions([extension]).build() + + when: + def newSchema = GraphQLSchema.newSchema(oldSchema).build() + + then: + newSchema.extensionDefinitions.size() == 1 + ((Directive) newSchema.extensionDefinitions.first().getDirectives().first()).name == "hello" + + } + def "clear directives works as expected"() { setup: def schemaBuilder = basicSchemaBuilder() @@ -233,10 +251,8 @@ class GraphQLSchemaTest extends Specification { } union UnionType = Cat | Dog - ''' - when: def schema = TestUtil.schema(sdl) From b66841730d8362dd54df12388ebce5b4990de8ce Mon Sep 17 00:00:00 2001 From: dondonz <13839920+dondonz@users.noreply.github.com> Date: Sat, 24 Feb 2024 14:08:46 +1100 Subject: [PATCH 2/2] Tidy up --- .../java/graphql/schema/GraphQLSchema.java | 18 +++++++++--------- .../graphql/schema/GraphQLSchemaTest.groovy | 7 ++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/graphql/schema/GraphQLSchema.java b/src/main/java/graphql/schema/GraphQLSchema.java index 313403c767..e7026ad6c3 100644 --- a/src/main/java/graphql/schema/GraphQLSchema.java +++ b/src/main/java/graphql/schema/GraphQLSchema.java @@ -71,7 +71,7 @@ public class GraphQLSchema { private final ImmutableMap> interfaceNameToObjectTypeNames; /* - * This constructs partial GraphQL schema object which has has the schema (query / mutation / subscription) trees + * This constructs partial GraphQL schema object which has the schema (query / mutation / subscription) trees * in it but it does not have the collected types, code registry nor the type references replaced * * But it can be traversed to discover all that and filled out later via another constructor. @@ -104,7 +104,7 @@ private GraphQLSchema(Builder builder) { } /* - * This constructs a full fledged graphql schema object that has not yet had its type references replaced + * This constructs a fully fledged graphql schema object that has not yet had its type references replaced * but it's otherwise complete */ @Internal @@ -255,7 +255,7 @@ public List getTypes(Collection typeNames) { /** * Gets the named type from the schema or null if it's not present. - * + *

* Warning - you are inviting class cast errors if the types are not what you expect. * * @param typeName the name of the type to retrieve @@ -286,7 +286,7 @@ public boolean containsType(String typeName) { * * @return a graphql object type or null if there is one * - * @throws graphql.GraphQLException if the type is NOT a object type + * @throws graphql.GraphQLException if the type is NOT an object type */ public GraphQLObjectType getObjectType(String typeName) { GraphQLType graphQLType = typeMap.get(typeName); @@ -433,14 +433,14 @@ public List getDirectives() { } /** - * @return a map of non repeatable directives by directive name + * @return a map of non-repeatable directives by directive name */ public Map getDirectivesByName() { return directiveDefinitionsHolder.getDirectivesByName(); } /** - * Returns a named directive that (for legacy reasons) will be only in the set of non repeatable directives + * Returns a named directive that (for legacy reasons) will be only in the set of non-repeatable directives * * @param directiveName the name of the directive to retrieve * @@ -535,7 +535,7 @@ public List getSchemaDirectives(String directiveName) { * directives for all schema elements, whereas this is just for the schema * element itself * - * @return a map of directives + * @return a list of directives */ public List getSchemaAppliedDirectives() { return schemaAppliedDirectivesHolder.getAppliedDirectives(); @@ -874,8 +874,8 @@ public GraphQLSchema build(Set additionalTypes) { /** * Builds the schema * - * @param additionalTypes - please don't use this any more - * @param additionalDirectives - please don't use this any more + * @param additionalTypes - please don't use this anymore + * @param additionalDirectives - please don't use this anymore * * @return the built schema * diff --git a/src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy b/src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy index cd8b1c990f..bf18e083c2 100644 --- a/src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy +++ b/src/test/groovy/graphql/schema/GraphQLSchemaTest.groovy @@ -134,7 +134,7 @@ class GraphQLSchemaTest extends Specification { def "schema builder copies extension definitions"() { setup: def schemaBuilder = basicSchemaBuilder() - def newDirective = Directive.newDirective().name("hello").build() + def newDirective = Directive.newDirective().name("pizza").build() def extension = SchemaExtensionDefinition.newSchemaExtensionDefinition().directive(newDirective).build() def oldSchema = schemaBuilder.extensionDefinitions([extension]).build() @@ -142,9 +142,10 @@ class GraphQLSchemaTest extends Specification { def newSchema = GraphQLSchema.newSchema(oldSchema).build() then: + oldSchema.extensionDefinitions.size() == 1 newSchema.extensionDefinitions.size() == 1 - ((Directive) newSchema.extensionDefinitions.first().getDirectives().first()).name == "hello" - + ((Directive) oldSchema.extensionDefinitions.first().getDirectives().first()).name == "pizza" + ((Directive) newSchema.extensionDefinitions.first().getDirectives().first()).name == "pizza" } def "clear directives works as expected"() {