diff --git a/README.md b/README.md index 124f1136..5b5255e5 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,33 @@ +![logo](polaris-iconsmalredl.png?raw=true) +# GraphQL-Java Annotations [![Build Status](https://travis-ci.org/graphql-java/graphql-java-annotations.svg?branch=master)](https://travis-ci.org/graphql-java/graphql-java-annotations) [![Maven Central](https://img.shields.io/maven-central/v/io.github.graphql-java/graphql-java-annotations.svg?maxAge=3000)]() -# GraphQL Annotations for Java [GraphQL-Java](https://github.com/andimarek/graphql-java) is a great library, but its syntax is a little bit verbose. This library offers an annotations-based syntax for GraphQL schema definition. + +## Table Of Contents +- [Getting Started](#getting-started) +- [GraphQLAnnotations class](#graphqlannotations-class) +- [Annotations Schema Creator](#annotations-schema-creator) +- [Defining Objects](#defining-objects) +- [Defining Interfaces](#defining-interfaces) +- [Defining Unions](#defining-unions) +- [Fields](#fields) + - [Custom DataFetcher](#custom-data-fetcher) +- [Type Extensions](#type-extensions) + - [Defining Extensions in Annotation](#defining-extensions-in-annotations) + - [Data Fetching with Extensions](#data-fetching-with-extensions) +- [Type Inference](#type-inference) +- [Directives](#directives) + - [Creating/Defining a GraphQL Directive](#creatingdefining-a-graphqldirective) + - [Wiring with Directives](#wiring-with-directives) +- [Relay Support](#relay-support) + - [Mutations](#mutations) + - [Connection](#connection) + - [Customizing Relay Schema](#customizing-relay-schema) + ## Getting Started @@ -12,7 +35,7 @@ syntax for GraphQL schema definition. ```groovy dependencies { - compile "io.github.graphql-java:graphql-java-annotations:6.1" + compile "io.github.graphql-java:graphql-java-annotations:7.0" } ``` @@ -22,10 +45,65 @@ dependencies { io.github.graphql-java graphql-java-annotations - 6.1 + 7.0 ``` +The graphql-java-annotations library is able to create GraphQLType objects out of your Java classes. +These GraphQLType objects can be later injected into the graphql-java schema. + +graphql-java-annotations also allows you to wire your objects with data fetchers and type resolvers while annotating your fields/types. The result of this process will be a ``GraphQLCodeRegistry.Builder`` object that can be later built and injected to the graphql-java schema. + + +## GraphQLAnnotations class + +You can create an instance of the `GraphQLAnnotations` class in order to create the GraphQL types. +```java +GraphQLAnnotations graphqlAnnotations = new GraphQLAnnotations(); +``` + +Using this object, you will be able to create the GraphQL types. +There are few types that can be generated - a `GraphQLObjectType`, a `GraphQLInterfaceType` and a `GraphQLDirective`. + +```java +GraphQLObjectType query = graphqlAnnotations.object(Query.class); +GraphQLDirective upperDirective = graphqlAnnotations.directive(UpperDirective.class); +GraphQLInterfaceType myInterface = graphqlAnnotations.generateInterface(MyInterface.class); +``` + +Then you can use these types in order to create a graphql-java schema. +But, in order to create a graphql-java schema, you need also the ``GraphQLCodeRegistry``, which contains all the data fetchers mapped to their fields (and also type resolvers). + +You can obtain the code registry this way: + +```java +graphqlAnnotations.getContainer().getCodeRegistryBuilder().build(); +``` + +## Annotations Schema Creator + +Using the `GraphQLAnnotations` processor object can be a little bit confusing if you wish to use it to create a GraphQL schema. +So we created a util class to help you create your desired GraphQL schema, in a syntax similiar to the graphql-java syntax. + +In order to do so you can use the ``AnnotationsSchemaCreator.Builder`` in the following way: + +```java + GraphQLSchema schema = AnnotationsSchemaCreator.newAnnotationsSchema() + .query(Query.class) // to create you query object + .mutation(Mutation.class) // to create your mutation object + .subscription(Subscription.class) // to create your subscription object + .directive(UpperDirective.class) // to create a directive + .additionalType(AdditionalType.class) // to create some additional type and add it to the schema + .typeFunction(CustomType.class) // to add a typefunction + .setAlwaysPrettify(true) // to set the global prettifier of field names (removes get/set/is prefixes from names) + .setRelay(customRelay) // to add a custom relay object + .build(); +``` + +Of course you can use this builder with only some of the properties, but the query class must be provided. +note - The GraphQLSchema is a graphql-java type. + +Continue reading in order to understand how your java classes should look in order to be provided to the annotations schema creator. ## Defining Objects @@ -39,7 +117,8 @@ public class SomeObject { } // ... -GraphQLObjectType object = GraphQLAnnotations.object(SomeObject.class); +GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations(); +GraphQLObjectType object = graphQLAnnotations.object(SomeObject.class); ``` ## Defining Interfaces @@ -58,7 +137,8 @@ public class MyTypeResolver implements TypeResolver { } // ... -GraphQLInterfaceType object = GraphQLAnnotations.iface(SomeInterface.class); +GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations(); +GraphQLInterfaceType object = graphQLAnnotations.generateInterface(SomeInterface.class); ``` An instance of the type resolver will be created from the specified class. If a `getInstance` method is present on the @@ -262,13 +342,11 @@ public class HumanExtension { Classes marked as "extensions" will actually not define a new type, but rather set new fields on the class it extends when it will be created. All GraphQL annotations can be used on extension classes. -Extensions are registered in GraphQLAnnotationProcessor by using `registerTypeExtension`. Note that extensions must be registered before the type itself is requested with `getObject()` : +Extensions are registered in GraphQLAnnotations object by using `registerTypeExtension`. Note that extensions must be registered before the type itself is requested with `getObject()` : ``` -GraphQLAnnotationsProcessor processor = GraphQLAnnotations.getInstance(); - // Register extensions -processor.registerTypeExtension(HumanExtension.class); +graphqlAnnotations.registerTypeExtension(HumanExtension.class); // Create type GraphQLObjectType type = processor.getObject(Human.class); @@ -314,7 +392,7 @@ public class UUIDTypeFunction implements TypeFunction { And register it with `GraphQLAnnotations`: ```java -GraphQLAnnotations.register(new UUIDTypeFunction()) +graphqlAnnotations.registerType(new UUIDTypeFunction()) // or if not using a static version of GraphQLAnnotations: // new GraphQLAnnotations().registerType(new UUIDTypeFunction()) @@ -348,7 +426,7 @@ You can also use ``@GraphQLName`` and ``@GraphQLDescription`` annotations on the After you created the class, you will be able to create the ``GraphQLDirective`` object using the following code: ```java -GraphQLAnnotations.directive(UpperDirective.class); +graphqlAnnotations.directive(UpperDirective.class); ``` ### Wiring with directives @@ -361,16 +439,19 @@ public class UpperWiring implements AnnotationsDirectiveWiring { public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) { GraphQLFieldDefinition field = (GraphQLFieldDefinition) environment.getElement(); boolean isActive = (boolean) environment.getDirective().getArgument("isActive").getValue(); - DataFetcher dataFetcher = DataFetcherFactories.wrapDataFetcher(field.getDataFetcher(), (((dataFetchingEnvironment, value) -> { + CodeRegistryUtil.wrapDataFetcher(field, environment, (((dataFetchingEnvironment, value) -> { if (value instanceof String && isActive) { return ((String) value).toUpperCase(); } - return value; - }))); - return field.transform(builder -> builder.dataFetcher(dataFetcher)); + return value; + }))); + return field; } } ``` + +You can also use the `field.transform` method in order to change some of the field's properties. + This class turns your string field to upper case if the directive argument "isActive" is set to true. Now, you have to wire the field itself: ```java @@ -410,7 +491,7 @@ NOTE: because `PropertyDataFetcher` and `FieldDataFetcher` can't handle connecti ### Customizing Relay schema By default, GraphQLAnnotations will use the `graphql.relay.Relay` class to create the Relay specific schema types (Mutations, Connections, Edges, PageInfo, ...). -It is possible to set a custom implementation of the Relay class with `GraphQLAnnotations.setRelay` method. The class should inherit from `graphql.relay.Relay` and +It is possible to set a custom implementation of the Relay class with `graphqlAnnotations.setRelay` method. The class should inherit from `graphql.relay.Relay` and can redefine methods that create Relay types. It is also possible to specify for every connection which relay do you want to use, by giving a value to the annotation: diff --git a/build.gradle b/build.gradle index 9f47c788..0945d8ba 100644 --- a/build.gradle +++ b/build.gradle @@ -37,12 +37,6 @@ task javadocJar(type: Jar, dependsOn: javadoc) { from javadoc.destinationDir } -idea { - project { - languageLevel = '1.8' - vcs = 'Git' - } -} release { tagTemplate = 'v${version}' failOnPublishNeeded = false @@ -69,7 +63,7 @@ gradle.projectsEvaluated { dependencies { compile 'javax.validation:validation-api:1.1.0.Final' - compile 'com.graphql-java:graphql-java:11.0' + compile 'com.graphql-java:graphql-java:12.0' // OSGi compileOnly 'org.osgi:org.osgi.core:6.0.0' @@ -169,8 +163,11 @@ bintray { version { name = project.version released = new Date() + gpg { + sign = false + } mavenCentralSync { - sync = true //[Default: true] Determines whether to sync the version to Maven Central. + sync = true //[Default: true] Determines whether to sync the version to Maven Central.. user = System.getenv('OSS_USER') ?: project.findProperty('OSS_USER') ?: '' password = System.getenv('OSS_PASS') ?: project.findProperty('OSS_PASS') ?: '' close = '1' diff --git a/gradle.properties b/gradle.properties index 169db324..6b5f42ac 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,4 +5,4 @@ org.gradle.daemon=true org.gradle.parallel=true org.gradle.jvmargs=-Dfile.encoding=UTF-8 -version = 6.2 +version = 7.0 diff --git a/polaris-iconsmalredl.png b/polaris-iconsmalredl.png new file mode 100644 index 00000000..a17b9f19 Binary files /dev/null and b/polaris-iconsmalredl.png differ diff --git a/src/main/java/graphql/annotations/AnnotationsSchemaCreator.java b/src/main/java/graphql/annotations/AnnotationsSchemaCreator.java new file mode 100644 index 00000000..a55c3a45 --- /dev/null +++ b/src/main/java/graphql/annotations/AnnotationsSchemaCreator.java @@ -0,0 +1,245 @@ +/** + * Copyright 2016 Yurii Rashkovskii + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + */ +package graphql.annotations; + +import graphql.annotations.processor.GraphQLAnnotations; +import graphql.annotations.processor.typeFunctions.TypeFunction; +import graphql.relay.Relay; +import graphql.schema.GraphQLDirective; +import graphql.schema.GraphQLSchema; +import graphql.schema.GraphQLType; + +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +public class AnnotationsSchemaCreator { + + public static Builder newAnnotationsSchema() { + return new Builder(); + } + + public static class Builder { + private Class queryObject; + private Class mutationObject; + private Class subscriptionObject; + private Set> directivesObjectList = new HashSet<>(); + private Set> additionalTypesList = new HashSet<>(); + private Set> typeExtensions = new HashSet<>(); + private Set typeFunctions = new HashSet<>(); + private Boolean shouldAlwaysPrettify = null; + private GraphQLAnnotations graphQLAnnotations; + private GraphQLSchema.Builder graphqlSchemaBuilder; + + /** + * You can set your own schema builder, but its optional + * + * @param schemaBuilder a graphql schema builder + * @return the builder after setting the schema builder + */ + public Builder setGraphQLSchemaBuilder(GraphQLSchema.Builder schemaBuilder) { + this.graphqlSchemaBuilder = schemaBuilder; + return this; + } + + /** + * You can set your own annotations processor + * + * @param annotationsProcessor the annotations processor which creates the GraphQLTypes + * @return the builder after setting the annotations processor + */ + public Builder setAnnotationsProcessor(GraphQLAnnotations annotationsProcessor) { + this.graphQLAnnotations = annotationsProcessor; + return this; + } + + /** + * Set the Query of the graphql schema + * This method will generate a GraphQL Query type out of your java class using the annotations processor + * + * @param queryClass the Query java class + * @return the builder after setting the query + */ + public Builder query(Class queryClass) { + this.queryObject = queryClass; + return this; + } + + /** + * Set the Mutation of the graphql schema + * This method will generate a GraphQL Mutation type out of your java class using the annotations processor + * + * @param mutationClass the Mutation java class + * @return the builder after setting the mutation + */ + public Builder mutation(Class mutationClass) { + this.mutationObject = mutationClass; + return this; + } + + /** + * Set the Subscription of the graphql schema + * This method will generate a GraphQL Subscription type out of your java class using the annotations processor + * + * @param subscriptionClass the Subscription java class + * @return the builder after setting the subscription + */ + public Builder subscription(Class subscriptionClass) { + this.subscriptionObject = subscriptionClass; + return this; + } + + /** + * Set the directives of the graphql schema + * This method will generate a GraphQL Directive type out of your java classes using the annotations processor + * + * @param directiveClasses a set of directive classes + * @return the builder after setting the directives + */ + public Builder directives(Set> directiveClasses) { + this.directivesObjectList.addAll(directiveClasses); + return this; + } + + /** + * Add a directive to the graphql schema + * This method will generate a GraphQL Directive type out of your java class using the annotations processor + * + * @param directiveClass a Directive java class + * @return the builder after adding the directive + */ + public Builder directive(Class directiveClass) { + this.directivesObjectList.add(directiveClass); + return this; + } + + /** + * Add an additional type to the additional type list + * + * @param additionalTypeClass an additional type class + * @return the builder after adding an additional type + */ + public Builder additionalType(Class additionalTypeClass) { + this.additionalTypesList.add(additionalTypeClass); + return this; + } + + /** + * Add a set of additional types to the additional type lise + * + * @param additionalTypes a set of additional type classes + * @return the builder after adding the additional types + */ + public Builder additionalTypes(Set> additionalTypes) { + this.additionalTypesList.addAll(additionalTypes); + return this; + } + + /** + * Register a type extensions to the graphql processor + * + * @param typeExtension a type extension class + * @return the builder after registering the type extension in the graphql processor + */ + public Builder typeExtension(Class typeExtension) { + this.typeExtensions.add(typeExtension); + return this; + } + + /** + * Register a type function to the graphql processor + * + * @param typeFunction a type function + * @return the builder after registering the type function in the graphql processor + */ + public Builder typeFunction(TypeFunction typeFunction) { + this.typeFunctions.add(typeFunction); + return this; + } + + /** + * Set the always prettify property of the graphql annotations processor (whether or not to prettify the graphql names) + * + * @param shouldAlwaysPrettify a boolean flag + * @return the builder after setting the property + */ + public Builder setAlwaysPrettify(Boolean shouldAlwaysPrettify) { + this.shouldAlwaysPrettify = shouldAlwaysPrettify; + return this; + } + + /** + * Set the relay object in the graphql annotations processor + * + * @param relay a relay object + * @return the builder after setting the relay object + */ + public Builder setRelay(Relay relay) { + this.graphQLAnnotations.setRelay(relay); + return this; + } + + /** + * @return the graphql annotations processor + */ + public GraphQLAnnotations getGraphQLAnnotations() { + return this.graphQLAnnotations; + } + + /** + * Build a graphql schema according to the properties provided + * The method generates the GraphQL objects, directives, additional types, etc using the graphql annotations processor and sets them into the GraphQL Schema + * + * @return a GraphQLSchema which contains generated GraphQL types out of the properties provided to the builder + */ + public GraphQLSchema build() { + assert this.queryObject != null; + + if (this.graphQLAnnotations == null) { + this.graphQLAnnotations = new GraphQLAnnotations(); + } + + if (this.graphqlSchemaBuilder == null) { + this.graphqlSchemaBuilder = new GraphQLSchema.Builder(); + } + + this.typeExtensions.forEach(typeExtension -> this.graphQLAnnotations.registerTypeExtension(typeExtension)); + this.typeFunctions.forEach(typeFunction -> this.graphQLAnnotations.registerTypeFunction(typeFunction)); + + if (this.shouldAlwaysPrettify != null) { + this.graphQLAnnotations.getObjectHandler().getTypeRetriever().getGraphQLFieldRetriever().setAlwaysPrettify(this.shouldAlwaysPrettify); + } + + Set directives = directivesObjectList.stream().map(dir -> graphQLAnnotations.directive(dir)).collect(Collectors.toSet()); + Set additionalTypes = additionalTypesList.stream().map(additionalType -> + additionalType.isInterface() ? + graphQLAnnotations.generateInterface(additionalType) : graphQLAnnotations.object(additionalType)).collect(Collectors.toSet()); + + this.graphqlSchemaBuilder.query(graphQLAnnotations.object(queryObject)); + if (this.mutationObject != null) { + this.graphqlSchemaBuilder.mutation(graphQLAnnotations.object(mutationObject)); + } + if (this.subscriptionObject != null) { + this.graphqlSchemaBuilder.subscription(graphQLAnnotations.object(subscriptionObject)); + } + if (!this.directivesObjectList.isEmpty()) { + graphqlSchemaBuilder.additionalDirectives(directives); + } + this.graphqlSchemaBuilder.additionalTypes(additionalTypes).additionalType(Relay.pageInfoType) + .codeRegistry(graphQLAnnotations.getContainer().getCodeRegistryBuilder().build()); + return this.graphqlSchemaBuilder.build(); + } + } +} diff --git a/src/main/java/graphql/annotations/GraphQLFieldDefinitionWrapper.java b/src/main/java/graphql/annotations/GraphQLFieldDefinitionWrapper.java deleted file mode 100644 index 11cb72aa..00000000 --- a/src/main/java/graphql/annotations/GraphQLFieldDefinitionWrapper.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2016 Yurii Rashkovskii - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - */ -package graphql.annotations; - -import graphql.schema.DataFetcher; -import graphql.schema.DataFetcherFactories; -import graphql.schema.GraphQLFieldDefinition; - -public class GraphQLFieldDefinitionWrapper extends GraphQLFieldDefinition { - - public GraphQLFieldDefinitionWrapper(GraphQLFieldDefinition fieldDefinition) { - super(fieldDefinition.getName(), fieldDefinition.getDescription(), fieldDefinition.getType(), - DataFetcherFactories.useDataFetcher((DataFetcher) fieldDefinition.getDataFetcher()), - fieldDefinition.getArguments(), fieldDefinition.getDeprecationReason(), - fieldDefinition.getDirectives(), - fieldDefinition.getDefinition()); - } - - @Override - public boolean equals(Object obj) { - return obj instanceof GraphQLFieldDefinition && - ((GraphQLFieldDefinition) obj).getName().contentEquals(getName()); - } -} diff --git a/src/main/java/graphql/annotations/dataFetchers/ExtensionDataFetcherWrapper.java b/src/main/java/graphql/annotations/dataFetchers/ExtensionDataFetcherWrapper.java index 48efd673..5b880ff1 100644 --- a/src/main/java/graphql/annotations/dataFetchers/ExtensionDataFetcherWrapper.java +++ b/src/main/java/graphql/annotations/dataFetchers/ExtensionDataFetcherWrapper.java @@ -16,11 +16,11 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import graphql.schema.DataFetchingEnvironmentImpl; import java.util.Map; import static graphql.annotations.processor.util.ReflectionKit.newInstance; +import static graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment; public class ExtensionDataFetcherWrapper implements DataFetcher { @@ -37,16 +37,8 @@ public ExtensionDataFetcherWrapper(Class declaringClass, DataFetcher dataFetc public T get(DataFetchingEnvironment environment) throws Exception { Object source = environment.getSource(); if (source != null && (!declaringClass.isInstance(source)) && !(source instanceof Map)) { - environment = new DataFetchingEnvironmentImpl(newInstance(declaringClass, source), - environment.getArguments(), environment.getContext(), - environment.getRoot(), environment.getFieldDefinition(), - environment.getFields(), environment.getFieldType(), environment.getParentType(), - environment.getGraphQLSchema(), - environment.getFragmentsByName(), environment.getExecutionId(), - environment.getSelectionSet(), environment.getExecutionStepInfo(), - environment.getExecutionContext()); + environment = newDataFetchingEnvironment(environment).source(newInstance(declaringClass, source)).build(); } - return dataFetcher.get(environment); } diff --git a/src/main/java/graphql/annotations/directives/AnnotationsDirectiveWiring.java b/src/main/java/graphql/annotations/directives/AnnotationsDirectiveWiring.java index 07b0967f..5ce55611 100644 --- a/src/main/java/graphql/annotations/directives/AnnotationsDirectiveWiring.java +++ b/src/main/java/graphql/annotations/directives/AnnotationsDirectiveWiring.java @@ -22,7 +22,6 @@ public interface AnnotationsDirectiveWiring { * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLObjectType onObject(AnnotationsWiringEnvironment environment) { @@ -34,7 +33,6 @@ default GraphQLObjectType onObject(AnnotationsWiringEnvironment environment) { * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) { @@ -46,7 +44,6 @@ default GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLArgument onArgument(AnnotationsWiringEnvironment environment) { @@ -58,7 +55,6 @@ default GraphQLArgument onArgument(AnnotationsWiringEnvironment environment) { * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLInterfaceType onInterface(AnnotationsWiringEnvironment environment) { @@ -70,7 +66,6 @@ default GraphQLInterfaceType onInterface(AnnotationsWiringEnvironment environmen * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLUnionType onUnion(AnnotationsWiringEnvironment environment) { @@ -82,7 +77,6 @@ default GraphQLUnionType onUnion(AnnotationsWiringEnvironment environment) { * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLEnumType onEnum(AnnotationsWiringEnvironment environment) { @@ -94,7 +88,6 @@ default GraphQLEnumType onEnum(AnnotationsWiringEnvironment environment) { * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLEnumValueDefinition onEnumValue(AnnotationsWiringEnvironment environment) { @@ -106,7 +99,6 @@ default GraphQLEnumValueDefinition onEnumValue(AnnotationsWiringEnvironment envi * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLScalarType onScalar(AnnotationsWiringEnvironment environment) { @@ -118,7 +110,6 @@ default GraphQLScalarType onScalar(AnnotationsWiringEnvironment environment) { * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLInputObjectType onInputObjectType(AnnotationsWiringEnvironment environment) { @@ -130,7 +121,6 @@ default GraphQLInputObjectType onInputObjectType(AnnotationsWiringEnvironment en * of that DSL element * * @param environment the wiring element - * * @return a non null element based on the original one */ default GraphQLInputObjectField onInputObjectField(AnnotationsWiringEnvironment environment) { diff --git a/src/main/java/graphql/annotations/directives/AnnotationsWiringEnvironment.java b/src/main/java/graphql/annotations/directives/AnnotationsWiringEnvironment.java index 154e2ff2..4dcc6350 100644 --- a/src/main/java/graphql/annotations/directives/AnnotationsWiringEnvironment.java +++ b/src/main/java/graphql/annotations/directives/AnnotationsWiringEnvironment.java @@ -14,6 +14,7 @@ */ package graphql.annotations.directives; +import graphql.schema.GraphQLCodeRegistry; import graphql.schema.GraphQLDirective; import graphql.schema.GraphQLDirectiveContainer; @@ -27,4 +28,17 @@ public interface AnnotationsWiringEnvironment { * @return the directive that is being examined */ GraphQLDirective getDirective(); + + /** + * + * @return the parent name of the element + */ + String getParentName(); + + + /** + * + * @return the code registry builder + */ + GraphQLCodeRegistry.Builder getCodeRegistryBuilder(); } diff --git a/src/main/java/graphql/annotations/directives/AnnotationsWiringEnvironmentImpl.java b/src/main/java/graphql/annotations/directives/AnnotationsWiringEnvironmentImpl.java index b93a024a..b3fbb963 100644 --- a/src/main/java/graphql/annotations/directives/AnnotationsWiringEnvironmentImpl.java +++ b/src/main/java/graphql/annotations/directives/AnnotationsWiringEnvironmentImpl.java @@ -14,16 +14,22 @@ */ package graphql.annotations.directives; +import graphql.schema.GraphQLCodeRegistry; import graphql.schema.GraphQLDirective; import graphql.schema.GraphQLDirectiveContainer; public class AnnotationsWiringEnvironmentImpl implements AnnotationsWiringEnvironment { private final GraphQLDirectiveContainer element; private final GraphQLDirective directive; + private final String parentName; + private GraphQLCodeRegistry.Builder codeRegistryBuilder; - public AnnotationsWiringEnvironmentImpl(GraphQLDirectiveContainer element, GraphQLDirective directive) { + public AnnotationsWiringEnvironmentImpl(GraphQLDirectiveContainer element, GraphQLDirective directive, + String parentName, GraphQLCodeRegistry.Builder codeRegistryBuilder) { this.element = element; this.directive = directive; + this.parentName = parentName; + this.codeRegistryBuilder = codeRegistryBuilder; } @Override @@ -36,6 +42,16 @@ public GraphQLDirective getDirective() { return directive; } + @Override + public String getParentName() { + return parentName; + } + + @Override + public GraphQLCodeRegistry.Builder getCodeRegistryBuilder() { + return codeRegistryBuilder; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -44,6 +60,9 @@ public boolean equals(Object o) { AnnotationsWiringEnvironmentImpl that = (AnnotationsWiringEnvironmentImpl) o; if (element != null ? !element.equals(that.element) : that.element != null) return false; + if (parentName != null ? !parentName.equals(that.parentName) : that.parentName != null) return false; + if (codeRegistryBuilder != null ? !codeRegistryBuilder.equals(that.codeRegistryBuilder) : that.codeRegistryBuilder != null) + return false; return directive != null ? directive.equals(that.directive) : that.directive == null; } diff --git a/src/main/java/graphql/annotations/directives/DirectiveWirer.java b/src/main/java/graphql/annotations/directives/DirectiveWirer.java index d0f9d72c..0ae85e15 100644 --- a/src/main/java/graphql/annotations/directives/DirectiveWirer.java +++ b/src/main/java/graphql/annotations/directives/DirectiveWirer.java @@ -26,7 +26,9 @@ public class DirectiveWirer { @FunctionalInterface interface WiringFunction { - GraphQLDirectiveContainer apply(GraphQLDirective a, GraphQLDirectiveContainer b, AnnotationsDirectiveWiring wiring) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException; + GraphQLDirectiveContainer apply(GraphQLDirective a, GraphQLDirectiveContainer b, + AnnotationsDirectiveWiring wiring, GraphQLCodeRegistry.Builder codeRegistryBuilder, String parentName) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException; } private Map functionMap; @@ -37,11 +39,12 @@ public DirectiveWirer() { private void putInMap(Map map, Class clazz, String functionName, Introspection.DirectiveLocation... locations) { - map.put(clazz, (d, e, wiring) -> { + map.put(clazz, (d, e, wiring, codeRegistryBuilder, parentName) -> { assertLocation(d, e, locations); AnnotationsWiringEnvironmentImpl environment = - new AnnotationsWiringEnvironmentImpl(e, e.getDirective(d.getName())); - return (GraphQLDirectiveContainer) wiring.getClass().getMethod(functionName, AnnotationsWiringEnvironment.class).invoke(wiring, environment); + new AnnotationsWiringEnvironmentImpl(e, e.getDirective(d.getName()), parentName, codeRegistryBuilder); + return (GraphQLDirectiveContainer) wiring.getClass().getMethod(functionName, AnnotationsWiringEnvironment.class) + .invoke(wiring, environment); }); } @@ -61,14 +64,15 @@ private Map createFunctionsMap() { return functionMap; } - public GraphQLDirectiveContainer wire(GraphQLDirectiveContainer element, HashMap directiveWiringMap) { + public GraphQLDirectiveContainer wire(GraphQLDirectiveContainer element, HashMap directiveWiringMap + , GraphQLCodeRegistry.Builder codeRegistryBuilder, String parentName) { for (Map.Entry entry : directiveWiringMap.entrySet()) { GraphQLDirective graphQLDirective = entry.getKey(); AnnotationsDirectiveWiring wiring = entry.getValue(); Class aClass = element.getClass(); try { - element = functionMap.get(aClass).apply(graphQLDirective, element, wiring); + element = functionMap.get(aClass).apply(graphQLDirective, element, wiring, codeRegistryBuilder, parentName); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new GraphQLAnnotationsException(e.getMessage(), e); } diff --git a/src/main/java/graphql/annotations/processor/GraphQLAnnotations.java b/src/main/java/graphql/annotations/processor/GraphQLAnnotations.java index e441039b..3bf0d250 100644 --- a/src/main/java/graphql/annotations/processor/GraphQLAnnotations.java +++ b/src/main/java/graphql/annotations/processor/GraphQLAnnotations.java @@ -30,6 +30,7 @@ import graphql.annotations.processor.util.DataFetcherConstructor; import graphql.relay.Relay; import graphql.schema.GraphQLDirective; +import graphql.schema.GraphQLInterfaceType; import graphql.schema.GraphQLObjectType; import java.util.Arrays; @@ -94,12 +95,6 @@ public GraphQLAnnotations(TypeFunction defaultTypeFunction, GraphQLObjectHandler this.container = new ProcessingElementsContainer(defaultTypeFunction); } - public static GraphQLAnnotations instance = new GraphQLAnnotations(); - - public static GraphQLAnnotations getInstance() { - return instance; - } - public void setRelay(Relay relay) { this.container.setRelay(relay); } @@ -109,38 +104,46 @@ public String getTypeName(Class objectClass) { return toGraphqlName(name == null ? objectClass.getSimpleName() : name.value()); } - public static GraphQLObjectType object(Class object) throws GraphQLAnnotationsException { - GraphQLAnnotations instance = getInstance(); + public GraphQLInterfaceType generateInterface(Class object) throws GraphQLAnnotationsException { try { - return instance.graphQLObjectHandler.getObject(object, instance.getContainer()); + return this.graphQLObjectHandler.getGraphQLType(object, this.getContainer()); } catch (GraphQLAnnotationsException e) { - instance.getContainer().getProcessing().clear(); - instance.getTypeRegistry().clear(); + this.getContainer().getProcessing().clear(); + this.getTypeRegistry().clear(); throw e; } } - public static GraphQLObjectType object(Class object, GraphQLDirective... directives) throws GraphQLAnnotationsException { - GraphQLAnnotations instance = getInstance(); - Arrays.stream(directives).forEach(x -> instance.getContainer().getDirectiveRegistry().put(x.getName(), x)); - + public GraphQLObjectType object(Class object) throws GraphQLAnnotationsException { try { - return instance.graphQLObjectHandler.getObject(object, instance.getContainer()); + return this.graphQLObjectHandler.getGraphQLType(object, this.getContainer()); } catch (GraphQLAnnotationsException e) { - instance.getContainer().getProcessing().clear(); - instance.getTypeRegistry().clear(); + this.getContainer().getProcessing().clear(); + this.getTypeRegistry().clear(); throw e; } } - public static GraphQLDirective directive(Class object) throws GraphQLAnnotationsException { - GraphQLAnnotations instance = getInstance(); + @Deprecated + public GraphQLObjectType object(Class object, GraphQLDirective... directives) throws GraphQLAnnotationsException { + Arrays.stream(directives).forEach(directive -> this.getContainer().getDirectiveRegistry().put(directive.getName(), directive)); + try { + return this.graphQLObjectHandler.getGraphQLType(object, this.getContainer()); + } catch (GraphQLAnnotationsException e) { + this.getContainer().getProcessing().clear(); + this.getTypeRegistry().clear(); + throw e; + } + } + public GraphQLDirective directive(Class object) throws GraphQLAnnotationsException { try { - return instance.directiveCreator.getDirective(object); + GraphQLDirective directive = this.directiveCreator.getDirective(object); + this.getContainer().getDirectiveRegistry().put(directive.getName(), directive); + return directive; } catch (GraphQLAnnotationsException e) { - instance.getContainer().getProcessing().clear(); - instance.getTypeRegistry().clear(); + this.getContainer().getProcessing().clear(); + this.getTypeRegistry().clear(); throw e; } } @@ -149,12 +152,13 @@ public void registerTypeExtension(Class objectClass) { graphQLExtensionsHandler.registerTypeExtension(objectClass, container); } - public void registerType(TypeFunction typeFunction) { + public void registerTypeFunction(TypeFunction typeFunction) { ((DefaultTypeFunction) container.getDefaultTypeFunction()).register(typeFunction); } - public static void register(TypeFunction typeFunction) { - getInstance().registerType(typeFunction); + @Deprecated + public void register(TypeFunction typeFunction) { + this.registerTypeFunction(typeFunction); } public Map getTypeRegistry() { diff --git a/src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java b/src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java index 48614e53..d0a1bec3 100644 --- a/src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java +++ b/src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java @@ -20,6 +20,7 @@ import graphql.annotations.processor.typeFunctions.DefaultTypeFunction; import graphql.annotations.processor.typeFunctions.TypeFunction; import graphql.relay.Relay; +import graphql.schema.GraphQLCodeRegistry; import graphql.schema.GraphQLDirective; import java.util.HashMap; @@ -27,8 +28,9 @@ import java.util.Set; import java.util.Stack; -import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_SUFFIX; import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX; +import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_SUFFIX; +import static graphql.schema.GraphQLCodeRegistry.newCodeRegistry; public class ProcessingElementsContainer { @@ -37,6 +39,7 @@ public class ProcessingElementsContainer { private Map typeRegistry; private Map directiveRegistry; private Map, Set>> extensionsTypeRegistry; + private GraphQLCodeRegistry.Builder codeRegistryBuilder; private Stack processing; private String inputPrefix = DEFAULT_INPUT_PREFIX; private String inputSuffix = DEFAULT_INPUT_SUFFIX; @@ -49,21 +52,25 @@ public void setDirectiveRegistry(Map directiveRegistry this.directiveRegistry = directiveRegistry; } - public ProcessingElementsContainer(TypeFunction defaultTypeFunction, Relay relay, Map typeRegistry, Map directiveRegistry, Map, Set>> extensionsTypeRegistry, Stack processing) { + public ProcessingElementsContainer(TypeFunction defaultTypeFunction, Relay relay, Map typeRegistry, Map directiveRegistry, + Map, Set>> extensionsTypeRegistry, Stack processing, + GraphQLCodeRegistry.Builder codeRegistryBuilder) { this.defaultTypeFunction = defaultTypeFunction; this.relay = relay; this.typeRegistry = typeRegistry; this.directiveRegistry = directiveRegistry; this.extensionsTypeRegistry = extensionsTypeRegistry; this.processing = processing; + this.codeRegistryBuilder = codeRegistryBuilder; } public ProcessingElementsContainer(TypeFunction typeFunction) { - this(typeFunction, new Relay(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new Stack<>()); + this(typeFunction, new Relay(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new Stack<>(), newCodeRegistry()); } public ProcessingElementsContainer() { - this(new DefaultTypeFunction(new GraphQLInputProcessor(), new GraphQLOutputProcessor()), new Relay(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new Stack<>()); + this(new DefaultTypeFunction(new GraphQLInputProcessor(), new GraphQLOutputProcessor()), new Relay(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new Stack<>(), newCodeRegistry()); } public Relay getRelay() { @@ -121,4 +128,12 @@ public String getInputSuffix() { public void setInputSuffix(String inputSuffix) { this.inputSuffix = inputSuffix; } + + public void setCodeRegistryBuilder(GraphQLCodeRegistry.Builder builder) { + this.codeRegistryBuilder = builder; + } + + public GraphQLCodeRegistry.Builder getCodeRegistryBuilder() { + return this.codeRegistryBuilder; + } } diff --git a/src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLAnnotationsProcessor.java b/src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLAnnotationsProcessor.java index 3e729431..853a6849 100644 --- a/src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLAnnotationsProcessor.java +++ b/src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLAnnotationsProcessor.java @@ -38,5 +38,5 @@ public interface GraphQLAnnotationsProcessor { * * @param typeFunction The extension class to register */ - void registerType(TypeFunction typeFunction); + void registerTypeFunction(TypeFunction typeFunction); } diff --git a/src/main/java/graphql/annotations/processor/retrievers/GraphQLExtensionsHandler.java b/src/main/java/graphql/annotations/processor/retrievers/GraphQLExtensionsHandler.java index 012a6925..19363971 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/GraphQLExtensionsHandler.java +++ b/src/main/java/graphql/annotations/processor/retrievers/GraphQLExtensionsHandler.java @@ -18,8 +18,6 @@ import graphql.annotations.processor.ProcessingElementsContainer; import graphql.annotations.processor.exceptions.CannotCastMemberException; import graphql.annotations.processor.exceptions.GraphQLAnnotationsException; -import graphql.annotations.processor.searchAlgorithms.BreadthFirstSearch; -import graphql.annotations.processor.searchAlgorithms.ParentalSearch; import graphql.annotations.processor.searchAlgorithms.SearchAlgorithm; import graphql.schema.GraphQLFieldDefinition; import org.osgi.service.component.annotations.Component; @@ -53,7 +51,7 @@ public List getExtensionFields(Class object, List getExtensionFields(Class object, List objectClass, ProcessingElementsContai } } - @Reference(policy= ReferencePolicy.DYNAMIC, policyOption= ReferencePolicyOption.GREEDY) + @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY) public void setGraphQLObjectInfoRetriever(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever) { this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever; } @@ -102,7 +100,7 @@ public void unsetGraphQLObjectInfoRetriever(GraphQLObjectInfoRetriever graphQLOb } - @Reference(target = "(type=field)", policy=ReferencePolicy.DYNAMIC, policyOption= ReferencePolicyOption.GREEDY) + @Reference(target = "(type=field)", policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY) public void setFieldSearchAlgorithm(SearchAlgorithm fieldSearchAlgorithm) { this.fieldSearchAlgorithm = fieldSearchAlgorithm; } @@ -111,7 +109,7 @@ public void unsetFieldSearchAlgorithm(SearchAlgorithm fieldSearchAlgorithm) { this.fieldSearchAlgorithm = null; } - @Reference(target = "(type=method)", policy=ReferencePolicy.DYNAMIC, policyOption= ReferencePolicyOption.GREEDY) + @Reference(target = "(type=method)", policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY) public void setMethodSearchAlgorithm(SearchAlgorithm methodSearchAlgorithm) { this.methodSearchAlgorithm = methodSearchAlgorithm; } @@ -120,7 +118,7 @@ public void unsetMethodSearchAlgorithm(SearchAlgorithm methodSearchAlgorithm) { this.methodSearchAlgorithm = null; } - @Reference(policy=ReferencePolicy.DYNAMIC, policyOption= ReferencePolicyOption.GREEDY) + @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY) public void setFieldRetriever(GraphQLFieldRetriever fieldRetriever) { this.fieldRetriever = fieldRetriever; } diff --git a/src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java b/src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java index 46f61f4b..cc8dc4ac 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java +++ b/src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java @@ -15,7 +15,6 @@ package graphql.annotations.processor.retrievers; -import graphql.annotations.GraphQLFieldDefinitionWrapper; import graphql.annotations.annotationTypes.GraphQLRelayMutation; import graphql.annotations.connection.GraphQLConnection; import graphql.annotations.directives.DirectiveWirer; @@ -32,6 +31,7 @@ import graphql.annotations.processor.retrievers.fieldBuilders.method.MethodNameBuilder; import graphql.annotations.processor.retrievers.fieldBuilders.method.MethodTypeBuilder; import graphql.annotations.processor.typeFunctions.TypeFunction; +import graphql.annotations.processor.util.CodeRegistryUtil; import graphql.annotations.processor.util.ConnectionUtil; import graphql.annotations.processor.util.DataFetcherConstructor; import graphql.relay.Relay; @@ -50,6 +50,7 @@ import java.util.stream.Collectors; import static graphql.annotations.processor.util.ReflectionKit.newInstance; +import static graphql.schema.FieldCoordinates.coordinates; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLInputObjectField.newInputObjectField; @@ -68,16 +69,17 @@ public GraphQLFieldRetriever() { this(new DataFetcherConstructor()); } - public GraphQLFieldDefinition getField(Method method, ProcessingElementsContainer container) throws GraphQLAnnotationsException { + public GraphQLFieldDefinition getField(String parentName, Method method, ProcessingElementsContainer container) throws GraphQLAnnotationsException { GraphQLFieldDefinition.Builder builder = newFieldDefinition(); TypeFunction typeFunction = getTypeFunction(method, container); - builder.name(new MethodNameBuilder(method).alwaysPrettify(alwaysPrettify).build()); + String fieldName = new MethodNameBuilder(method).alwaysPrettify(alwaysPrettify).build(); + builder.name(fieldName); GraphQLOutputType outputType = (GraphQLOutputType) new MethodTypeBuilder(method, typeFunction, container, false).build(); boolean isConnection = ConnectionUtil.isConnection(method, outputType); if (isConnection) { outputType = getGraphQLConnection(method, outputType, ConnectionUtil.getRelay(method, container), container.getTypeRegistry()); - builder.argument(ConnectionUtil.getRelay(method, container).getConnectionFieldArguments()); + builder.arguments(ConnectionUtil.getRelay(method, container).getConnectionFieldArguments()); } builder.type(outputType); DirectivesBuilder directivesBuilder = new DirectivesBuilder(method, container); @@ -86,55 +88,80 @@ public GraphQLFieldDefinition getField(Method method, ProcessingElementsContaine GraphQLFieldDefinition relayFieldDefinition = handleRelayArguments(method, container, builder, outputType, args); builder.description(new DescriptionBuilder(method).build()) .deprecate(new DeprecateBuilder(method).build()) - .dataFetcher(new MethodDataFetcherBuilder(method, outputType, typeFunction, container, relayFieldDefinition, args, dataFetcherConstructor, isConnection).build()); + .build(); + + DataFetcher dataFetcher = new MethodDataFetcherBuilder(method, outputType, typeFunction, container, relayFieldDefinition, args, dataFetcherConstructor, isConnection).build(); + container.getCodeRegistryBuilder().dataFetcher(coordinates(parentName, fieldName), dataFetcher); - return new GraphQLFieldDefinitionWrapper((GraphQLFieldDefinition) new DirectiveWirer().wire(builder.build(), new DirectiveWiringMapRetriever().getDirectiveWiringMap(method, container))); + return (GraphQLFieldDefinition) new DirectiveWirer().wire(builder.build(), + new DirectiveWiringMapRetriever().getDirectiveWiringMap(method, container), + container.getCodeRegistryBuilder(), parentName); } - public GraphQLFieldDefinition getField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException { + public GraphQLFieldDefinition getField(String parentName, Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException { GraphQLFieldDefinition.Builder builder = newFieldDefinition(); - builder.name(new FieldNameBuilder(field).alwaysPrettify(alwaysPrettify).build()); + String fieldName = new FieldNameBuilder(field).alwaysPrettify(alwaysPrettify).build(); + builder.name(fieldName); TypeFunction typeFunction = getTypeFunction(field, container); GraphQLType outputType = typeFunction.buildType(field.getType(), field.getAnnotatedType(), container); boolean isConnection = ConnectionUtil.isConnection(field, outputType); if (isConnection) { outputType = getGraphQLConnection(field, outputType, ConnectionUtil.getRelay(field, container), container.getTypeRegistry()); - builder.argument(ConnectionUtil.getRelay(field, container).getConnectionFieldArguments()); + builder.arguments(ConnectionUtil.getRelay(field, container).getConnectionFieldArguments()); } + DataFetcher dataFetcher = new FieldDataFetcherBuilder(field, dataFetcherConstructor, outputType, typeFunction, container, isConnection).build(); builder.type((GraphQLOutputType) outputType).description(new DescriptionBuilder(field).build()) - .deprecate(new DeprecateBuilder(field).build()) - .dataFetcher(new FieldDataFetcherBuilder(field, dataFetcherConstructor, outputType, typeFunction, container, isConnection).build()); + .deprecate(new DeprecateBuilder(field).build()); + + container.getCodeRegistryBuilder().dataFetcher(coordinates(parentName, fieldName), dataFetcher); GraphQLDirective[] graphQLDirectives = new DirectivesBuilder(field, container).build(); builder.withDirectives(graphQLDirectives); - return new GraphQLFieldDefinitionWrapper((GraphQLFieldDefinition) new DirectiveWirer().wire(builder.build(), new DirectiveWiringMapRetriever().getDirectiveWiringMap(field, container))); + return (GraphQLFieldDefinition) new DirectiveWirer().wire(builder.build(), + new DirectiveWiringMapRetriever().getDirectiveWiringMap(field, container), + container.getCodeRegistryBuilder(), parentName); } - public GraphQLInputObjectField getInputField(Method method, ProcessingElementsContainer container) throws GraphQLAnnotationsException { + public GraphQLInputObjectField getInputField(Method method, ProcessingElementsContainer container, String parentName) throws GraphQLAnnotationsException { GraphQLInputObjectField.Builder builder = newInputObjectField(); builder.name(new MethodNameBuilder(method).alwaysPrettify(alwaysPrettify).build()); TypeFunction typeFunction = getTypeFunction(method, container); GraphQLInputType inputType = (GraphQLInputType) new MethodTypeBuilder(method, typeFunction, container, true).build(); - return builder.type(inputType).description(new DescriptionBuilder(method).build()).build(); + builder.withDirectives(new DirectivesBuilder(method, container).build()); + return (GraphQLInputObjectField) new DirectiveWirer().wire(builder.type(inputType) + .description(new DescriptionBuilder(method).build()).build(), + new DirectiveWiringMapRetriever().getDirectiveWiringMap(method, container), container.getCodeRegistryBuilder(), parentName + ); } - public GraphQLInputObjectField getInputField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException { + public GraphQLInputObjectField getInputField(Field field, ProcessingElementsContainer container, String parentName) throws GraphQLAnnotationsException { GraphQLInputObjectField.Builder builder = newInputObjectField(); builder.name(new FieldNameBuilder(field).alwaysPrettify(alwaysPrettify).build()); TypeFunction typeFunction = getTypeFunction(field, container); GraphQLType graphQLType = typeFunction.buildType(true, field.getType(), field.getAnnotatedType(), container); - return builder.type((GraphQLInputType) graphQLType).description(new DescriptionBuilder(field).build()).build(); + builder.withDirectives(new DirectivesBuilder(field, container).build()); + return (GraphQLInputObjectField) new DirectiveWirer().wire(builder.type((GraphQLInputType) graphQLType) + .description(new DescriptionBuilder(field).build()).build(), + new DirectiveWiringMapRetriever().getDirectiveWiringMap(field, container), container.getCodeRegistryBuilder(), parentName); } private GraphQLFieldDefinition handleRelayArguments(Method method, ProcessingElementsContainer container, GraphQLFieldDefinition.Builder builder, GraphQLOutputType outputType, List args) { GraphQLFieldDefinition relayFieldDefinition = null; if (method.isAnnotationPresent(GraphQLRelayMutation.class)) { relayFieldDefinition = buildRelayMutation(method, container, builder, outputType, args); + + // Getting the data fetcher from the old field type and putting it as the new type + String newParentType = relayFieldDefinition.getType().getName(); + relayFieldDefinition.getType().getChildren().forEach(field -> { + DataFetcher dataFetcher = CodeRegistryUtil.getDataFetcher(container.getCodeRegistryBuilder(), outputType.getName(), (GraphQLFieldDefinition) field); + container.getCodeRegistryBuilder().dataFetcher(coordinates(newParentType, field.getName()), dataFetcher); + }); + } else { - builder.argument(args); + builder.arguments(args); } return relayFieldDefinition; } @@ -164,7 +191,7 @@ private GraphQLFieldDefinition buildRelayMutation(Method method, ProcessingEleme args.stream(). map(t -> newInputObjectField().name(t.getName()).type(t.getType()).description(t.getDescription()).build()). collect(Collectors.toList()), fieldDefinitions, new StaticDataFetcher(null)); - builder.argument(relayFieldDefinition.getArguments()).type(relayFieldDefinition.getType()); + builder.arguments(relayFieldDefinition.getArguments()).type(relayFieldDefinition.getType()); return relayFieldDefinition; } diff --git a/src/main/java/graphql/annotations/processor/retrievers/GraphQLObjectHandler.java b/src/main/java/graphql/annotations/processor/retrievers/GraphQLObjectHandler.java index 96acd484..081cda8c 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/GraphQLObjectHandler.java +++ b/src/main/java/graphql/annotations/processor/retrievers/GraphQLObjectHandler.java @@ -17,7 +17,6 @@ import graphql.annotations.processor.ProcessingElementsContainer; import graphql.annotations.processor.exceptions.CannotCastMemberException; import graphql.annotations.processor.exceptions.GraphQLAnnotationsException; -import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLOutputType; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -29,12 +28,14 @@ public class GraphQLObjectHandler { private GraphQLTypeRetriever typeRetriever; - public GraphQLObjectType getObject(Class object, ProcessingElementsContainer container) throws GraphQLAnnotationsException, CannotCastMemberException { + public T getGraphQLType(Class object, ProcessingElementsContainer container) throws GraphQLAnnotationsException, CannotCastMemberException { GraphQLOutputType type = (GraphQLOutputType) typeRetriever.getGraphQLType(object, container, false); - if (type instanceof GraphQLObjectType) { - return (GraphQLObjectType) type; - } else { - throw new IllegalArgumentException("Object resolve to a " + type.getClass().getSimpleName()); + try{ + return (T)type; + + } + catch (Exception e){ + throw new IllegalArgumentException("Cannot cast type " + type.getClass().getSimpleName()); } } @@ -42,7 +43,7 @@ public GraphQLTypeRetriever getTypeRetriever() { return typeRetriever; } - @Reference(policy= ReferencePolicy.DYNAMIC, policyOption= ReferencePolicyOption.GREEDY) + @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY) public void setTypeRetriever(GraphQLTypeRetriever typeRetriever) { this.typeRetriever = typeRetriever; } diff --git a/src/main/java/graphql/annotations/processor/retrievers/GraphQLTypeRetriever.java b/src/main/java/graphql/annotations/processor/retrievers/GraphQLTypeRetriever.java index d4c9069b..94628f85 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/GraphQLTypeRetriever.java +++ b/src/main/java/graphql/annotations/processor/retrievers/GraphQLTypeRetriever.java @@ -46,7 +46,7 @@ public class GraphQLTypeRetriever { * * @param object the object class to examine* * @param container a class that hold several members that are required in order to build schema - * @param isInput true if the type is an input type, false otherwise + * @param isInput true if the type is an input type, false otherwise * @return a {@link GraphQLType} that represents that object class * @throws graphql.annotations.processor.exceptions.GraphQLAnnotationsException if the object class cannot be examined * @throws graphql.annotations.processor.exceptions.CannotCastMemberException if the object class cannot be examined @@ -90,7 +90,9 @@ public GraphQLType getGraphQLType(Class object, ProcessingElementsContainer c DirectiveWirer directiveWirer = new DirectiveWirer(); // wire the type with the directives and change the original type - type = directiveWirer.wire((GraphQLDirectiveContainer) type, new DirectiveWiringMapRetriever().getDirectiveWiringMap(object, container)); + type = directiveWirer.wire((GraphQLDirectiveContainer) type, + new DirectiveWiringMapRetriever().getDirectiveWiringMap(object, container), + container.getCodeRegistryBuilder(), null); container.getTypeRegistry().put(type.getName(), type); container.getProcessing().pop(); diff --git a/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/ArgumentBuilder.java b/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/ArgumentBuilder.java index 5cd97b57..461851e4 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/ArgumentBuilder.java +++ b/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/ArgumentBuilder.java @@ -81,7 +81,9 @@ private GraphQLArgument getArgument(Parameter parameter, graphql.schema.GraphQLI argumentBuilder.name(toGraphqlName(parameter.getName())); } argumentBuilder.withDirectives(new DirectivesBuilder(parameter, container).build()); - return (GraphQLArgument) new DirectiveWirer().wire(argumentBuilder.build(), new DirectiveWiringMapRetriever().getDirectiveWiringMap(parameter, container)); + return (GraphQLArgument) new DirectiveWirer().wire(argumentBuilder.build(), + new DirectiveWiringMapRetriever().getDirectiveWiringMap(parameter, container), container.getCodeRegistryBuilder(), + inputType.getName()); } } diff --git a/src/main/java/graphql/annotations/processor/typeBuilders/InputObjectBuilder.java b/src/main/java/graphql/annotations/processor/typeBuilders/InputObjectBuilder.java index 3e20eec9..553922d2 100644 --- a/src/main/java/graphql/annotations/processor/typeBuilders/InputObjectBuilder.java +++ b/src/main/java/graphql/annotations/processor/typeBuilders/InputObjectBuilder.java @@ -55,7 +55,8 @@ public InputObjectBuilder(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, public GraphQLInputObjectType.Builder getInputObjectBuilder(Class object, ProcessingElementsContainer container) throws GraphQLAnnotationsException { GraphQLInputObjectType.Builder builder = GraphQLInputObjectType.newInputObject(); - builder.name(container.getInputPrefix() + graphQLObjectInfoRetriever.getTypeName(object) + container.getInputSuffix()); + String name = container.getInputPrefix() + graphQLObjectInfoRetriever.getTypeName(object) + container.getInputSuffix(); + builder.name(name); GraphQLDescription description = object.getAnnotation(GraphQLDescription.class); if (description != null) { builder.description(description.value()); @@ -68,7 +69,7 @@ public GraphQLInputObjectType.Builder getInputObjectBuilder(Class object, Pro continue; } if (methodSearchAlgorithm.isFound(method)) { - GraphQLInputObjectField gqlField = graphQLFieldRetriever.getInputField(method,container); + GraphQLInputObjectField gqlField = graphQLFieldRetriever.getInputField(method, container, name); definedFields.add(gqlField.getName()); builder.field(gqlField); } @@ -79,7 +80,7 @@ public GraphQLInputObjectType.Builder getInputObjectBuilder(Class object, Pro continue; } if (fieldSearchAlgorithm.isFound(field)) { - GraphQLInputObjectField gqlField = graphQLFieldRetriever.getInputField(field,container); + GraphQLInputObjectField gqlField = graphQLFieldRetriever.getInputField(field, container, name); definedFields.add(gqlField.getName()); builder.field(gqlField); } diff --git a/src/main/java/graphql/annotations/processor/typeBuilders/InterfaceBuilder.java b/src/main/java/graphql/annotations/processor/typeBuilders/InterfaceBuilder.java index 0fda433d..699e09ea 100644 --- a/src/main/java/graphql/annotations/processor/typeBuilders/InterfaceBuilder.java +++ b/src/main/java/graphql/annotations/processor/typeBuilders/InterfaceBuilder.java @@ -18,10 +18,10 @@ import graphql.annotations.annotationTypes.GraphQLField; import graphql.annotations.annotationTypes.GraphQLTypeResolver; import graphql.annotations.processor.ProcessingElementsContainer; +import graphql.annotations.processor.exceptions.CannotCastMemberException; import graphql.annotations.processor.exceptions.GraphQLAnnotationsException; import graphql.annotations.processor.retrievers.GraphQLExtensionsHandler; import graphql.annotations.processor.retrievers.GraphQLFieldRetriever; -import graphql.annotations.processor.exceptions.CannotCastMemberException; import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever; import graphql.schema.GraphQLFieldDefinition; import graphql.schema.GraphQLInterfaceType; @@ -33,6 +33,7 @@ import static graphql.annotations.processor.util.ReflectionKit.newInstance; import static graphql.schema.GraphQLInterfaceType.newInterface; + /** * Copyright 2016 Yurii Rashkovskii * @@ -55,7 +56,7 @@ public class InterfaceBuilder { public InterfaceBuilder(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLFieldRetriever graphQLFieldRetriever, GraphQLExtensionsHandler extensionsHandler) { this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever; - this.graphQLFieldRetriever=graphQLFieldRetriever; + this.graphQLFieldRetriever = graphQLFieldRetriever; this.extensionsHandler = extensionsHandler; } @@ -66,7 +67,8 @@ public GraphQLInterfaceType.Builder getInterfaceBuilder(Class iface, Processi } GraphQLInterfaceType.Builder builder = newInterface(); - builder.name(graphQLObjectInfoRetriever.getTypeName(iface)); + String typeName = graphQLObjectInfoRetriever.getTypeName(iface); + builder.name(typeName); GraphQLDescription description = iface.getAnnotation(GraphQLDescription.class); if (description != null) { builder.description(description.value()); @@ -76,15 +78,15 @@ public GraphQLInterfaceType.Builder getInterfaceBuilder(Class iface, Processi boolean valid = !Modifier.isStatic(method.getModifiers()) && method.getAnnotation(GraphQLField.class) != null; if (valid) { - GraphQLFieldDefinition gqlField = graphQLFieldRetriever.getField(method,container); + GraphQLFieldDefinition gqlField = graphQLFieldRetriever.getField(typeName, method, container); definedFields.add(gqlField.getName()); builder.field(gqlField); } } - builder.fields(extensionsHandler.getExtensionFields(iface, definedFields,container)); + builder.fields(extensionsHandler.getExtensionFields(iface, definedFields, container)); GraphQLTypeResolver typeResolver = iface.getAnnotation(GraphQLTypeResolver.class); - builder.typeResolver(newInstance(typeResolver.value())); + container.getCodeRegistryBuilder().typeResolver(typeName, newInstance(typeResolver.value())); return builder; } } diff --git a/src/main/java/graphql/annotations/processor/typeBuilders/OutputObjectBuilder.java b/src/main/java/graphql/annotations/processor/typeBuilders/OutputObjectBuilder.java index a430d7c4..44c04d90 100644 --- a/src/main/java/graphql/annotations/processor/typeBuilders/OutputObjectBuilder.java +++ b/src/main/java/graphql/annotations/processor/typeBuilders/OutputObjectBuilder.java @@ -66,7 +66,8 @@ public OutputObjectBuilder(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever public GraphQLObjectType.Builder getOutputObjectBuilder(Class object, ProcessingElementsContainer container) throws GraphQLAnnotationsException { GraphQLObjectType.Builder builder = newObject(); - builder.name(graphQLObjectInfoRetriever.getTypeName(object)); + String typeName = graphQLObjectInfoRetriever.getTypeName(object); + builder.name(typeName); GraphQLDescription description = object.getAnnotation(GraphQLDescription.class); if (description != null) { builder.description(description.value()); @@ -77,7 +78,7 @@ public GraphQLObjectType.Builder getOutputObjectBuilder(Class object, Process continue; } if (methodSearchAlgorithm.isFound(method)) { - GraphQLFieldDefinition gqlField = graphQLFieldRetriever.getField(method, container); + GraphQLFieldDefinition gqlField = graphQLFieldRetriever.getField(typeName, method, container); definedFields.add(gqlField.getName()); builder.field(gqlField); } @@ -88,7 +89,7 @@ public GraphQLObjectType.Builder getOutputObjectBuilder(Class object, Process continue; } if (fieldSearchAlgorithm.isFound(field)) { - GraphQLFieldDefinition gqlField = graphQLFieldRetriever.getField(field, container); + GraphQLFieldDefinition gqlField = graphQLFieldRetriever.getField(typeName, field, container); definedFields.add(gqlField.getName()); builder.field(gqlField); } diff --git a/src/main/java/graphql/annotations/processor/typeBuilders/UnionBuilder.java b/src/main/java/graphql/annotations/processor/typeBuilders/UnionBuilder.java index 22e1c1a9..d918d0ab 100644 --- a/src/main/java/graphql/annotations/processor/typeBuilders/UnionBuilder.java +++ b/src/main/java/graphql/annotations/processor/typeBuilders/UnionBuilder.java @@ -58,7 +58,8 @@ public Builder getUnionBuilder(Class iface, ProcessingElementsContainer conta Builder builder = newUnionType(); GraphQLUnion unionAnnotation = iface.getAnnotation(GraphQLUnion.class); - builder.name(graphQLObjectInfoRetriever.getTypeName(iface)); + String typeName = graphQLObjectInfoRetriever.getTypeName(iface); + builder.name(typeName); GraphQLDescription description = iface.getAnnotation(GraphQLDescription.class); if (description != null) { builder.description(description.value()); @@ -78,8 +79,7 @@ public Builder getUnionBuilder(Class iface, ProcessingElementsContainer conta .forEach(builder::possibleType); TypeResolver typeResolver = getTypeResolver(container, unionAnnotation); - - builder.typeResolver(typeResolver); + container.getCodeRegistryBuilder().typeResolver(typeName, typeResolver); return builder; } @@ -90,10 +90,9 @@ private TypeResolver getTypeResolver(ProcessingElementsContainer container, Grap return typeResolverConstructorOptional .map(constructor -> { - if(constructor.getParameterCount() == 0) { + if (constructor.getParameterCount() == 0) { return (TypeResolver) constructNewInstance(constructor); - } - else { + } else { return (TypeResolver) constructNewInstance(constructor, unionAnnotation.possibleTypes(), container); } }) diff --git a/src/main/java/graphql/annotations/processor/util/CodeRegistryUtil.java b/src/main/java/graphql/annotations/processor/util/CodeRegistryUtil.java new file mode 100644 index 00000000..f9e70aca --- /dev/null +++ b/src/main/java/graphql/annotations/processor/util/CodeRegistryUtil.java @@ -0,0 +1,51 @@ +/** + * Copyright 2016 Yurii Rashkovskii + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + */ +package graphql.annotations.processor.util; + +import graphql.annotations.directives.AnnotationsWiringEnvironment; +import graphql.schema.*; + +import java.util.function.BiFunction; + +import static graphql.schema.GraphQLObjectType.newObject; + +public class CodeRegistryUtil { + /** + * This util method helps you wrap your datafetcher with some lambda code + * + * @param fieldDefinition The field you want to wrap its datafetcher + * @param environment the environment object of the Wiring process + * @param mapFunction the lambda expression to wrap with + */ + public static void wrapDataFetcher(GraphQLFieldDefinition fieldDefinition, AnnotationsWiringEnvironment environment, + BiFunction mapFunction) { + DataFetcher originalDataFetcher = getDataFetcher(environment.getCodeRegistryBuilder(), environment.getParentName(), fieldDefinition); + DataFetcher wrappedDataFetcher = DataFetcherFactories.wrapDataFetcher(originalDataFetcher, mapFunction); + environment.getCodeRegistryBuilder() + .dataFetcher(FieldCoordinates.coordinates(environment.getParentName(), fieldDefinition.getName()), wrappedDataFetcher); + } + + /** + * this util method helps you retrieve the data fetcher from the code registry if you do not have the whole parent object (only parent name) + * + * @param codeRegistryBuilder the code registry builder + * @param parentName the parent name + * @param fieldDefinition the field definition which the data fetcher is linked to + * @return the data fetcher + */ + public static DataFetcher getDataFetcher(GraphQLCodeRegistry.Builder codeRegistryBuilder, String parentName, GraphQLFieldDefinition fieldDefinition) { + return codeRegistryBuilder.getDataFetcher(newObject().name(parentName).build(), fieldDefinition); + } +} diff --git a/src/main/java/graphql/annotations/strategies/EnhancedExecutionStrategy.java b/src/main/java/graphql/annotations/strategies/EnhancedExecutionStrategy.java index b9ad19f3..dc1c63d1 100644 --- a/src/main/java/graphql/annotations/strategies/EnhancedExecutionStrategy.java +++ b/src/main/java/graphql/annotations/strategies/EnhancedExecutionStrategy.java @@ -31,8 +31,8 @@ public class EnhancedExecutionStrategy extends AsyncSerialExecutionStrategy { @Override protected CompletableFuture resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters) { - GraphQLObjectType parentType = (GraphQLObjectType) parameters.getExecutionStepInfo().getType(); - GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, parameters.getField().get(0)); + GraphQLObjectType parentType = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType(); + GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, parameters.getField().getSingleField()); if (fieldDef == null) return null; if (fieldDef.getName().contentEquals(CLIENT_MUTATION_ID)) { diff --git a/src/test/java/graphql/annotations/AnnotationsSchemaCreatorTest.java b/src/test/java/graphql/annotations/AnnotationsSchemaCreatorTest.java new file mode 100644 index 00000000..bc061eb2 --- /dev/null +++ b/src/test/java/graphql/annotations/AnnotationsSchemaCreatorTest.java @@ -0,0 +1,166 @@ +/** + * Copyright 2016 Yurii Rashkovskii + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + */ +package graphql.annotations; + +import graphql.annotations.AnnotationsSchemaCreator; +import graphql.annotations.annotationTypes.GraphQLDescription; +import graphql.annotations.annotationTypes.GraphQLField; +import graphql.annotations.annotationTypes.GraphQLName; +import graphql.annotations.directives.creation.DirectiveLocations; +import graphql.annotations.processor.GraphQLAnnotations; +import graphql.introspection.Introspection; +import graphql.schema.GraphQLDirective; +import graphql.schema.GraphQLObjectType; +import graphql.schema.GraphQLSchema; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.HashSet; +import java.util.Set; + +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +public class AnnotationsSchemaCreatorTest { + private AnnotationsSchemaCreator.Builder builder; + private GraphQLAnnotations graphQLAnnotations; + + @BeforeMethod + public void setUp() { + graphQLAnnotations = new GraphQLAnnotations(); + builder = newAnnotationsSchema().setAnnotationsProcessor(graphQLAnnotations); + } + + @Test(expectedExceptions = AssertionError.class) + public void build_QueryIsNotProvided_ExceptionIsThrown() { + // act + builder.build(); + } + + @GraphQLDescription("query obj") + public static class QueryTest { + @GraphQLField + public int getNum() { + return 5; + } + } + + @Test + public void build_QueryIsProvided_SchemaIsCreatedWithQuery() { + // arrange + act + GraphQLSchema schema = builder.query(QueryTest.class).build(); + + // assert + GraphQLObjectType queryType = schema.getQueryType(); + assertThat(queryType.getDescription(), is("query obj")); + assertThat(queryType.getFieldDefinition("getNum"), notNullValue()); + assertThat(queryType.getFieldDefinitions().size(), is(1)); + } + + public static class MutationTest { + @GraphQLField + public int mutate() { + return 4; + } + } + + @Test + public void build_Mutation_SchemaIsCreatedWithMutation() { + // arrange + act + GraphQLSchema schema = builder.query(QueryTest.class).mutation(MutationTest.class).build(); + GraphQLObjectType mutationType = schema.getMutationType(); + + /// assert + assertThat(mutationType.getFieldDefinition("mutate"), notNullValue()); + assertThat(mutationType.getFieldDefinitions().size(), is(1)); + } + + public static class SubscriptionTest { + @GraphQLField + public int subscribe() { + return 4; + } + } + + @Test + public void build_Subscription_SchemaIsCreatedWithSubscription() { + // arrange + act + GraphQLSchema schema = builder.query(QueryTest.class).subscription(SubscriptionTest.class).build(); + GraphQLObjectType subscriptionType = schema.getSubscriptionType(); + + // assert + assertThat(subscriptionType.getFieldDefinition("subscribe"), notNullValue()); + assertThat(subscriptionType.getFieldDefinitions().size(), is(1)); + } + + @GraphQLName("testDirective") + @DirectiveLocations({Introspection.DirectiveLocation.FIELD_DEFINITION}) + public static class DirectiveDefinitionTest { + private boolean isActive = true; + } + + @Test + public void build_Directive_SchemaIsCreatedWithDirective() { + // arrange + act + GraphQLSchema schema = builder.query(QueryTest.class).directive(DirectiveDefinitionTest.class).build(); + + // assert + GraphQLDirective testDirective = schema.getDirective("testDirective"); + assertThat(testDirective, notNullValue()); + assertThat(testDirective.getArguments().size(), is(1)); + assertThat(testDirective.getArgument("isActive"), notNullValue()); + } + + @GraphQLName("secondDirective") + @DirectiveLocations(Introspection.DirectiveLocation.FIELD) + public static class SecondDirective { + + } + + @Test + public void build_MultipleDirectives_SchemaIsCreatedWithDirectives() { + // arrange + act + Set> directives = new HashSet<>(); + directives.add(DirectiveDefinitionTest.class); + directives.add(SecondDirective.class); + GraphQLDirective directiveTest = graphQLAnnotations.directive(DirectiveDefinitionTest.class); + GraphQLDirective secondDirective = graphQLAnnotations.directive(SecondDirective.class); + GraphQLSchema schema = builder.query(QueryTest.class).directives(directives).build(); + + // assert + assertThat(schema.getDirective("secondDirective"), notNullValue()); + assertThat(schema.getDirective("testDirective"), notNullValue()); + } + + @GraphQLName("additional") + public static class AdditionalTypeTest { + public int getI() { + return 4; + } + } + + @Test + public void build_AdditionalType_SchemaIsCreatedWithAdditionalType() { + // arrange + act + GraphQLSchema schema = builder.query(QueryTest.class).additionalType(AdditionalTypeTest.class).build(); + GraphQLObjectType additionalType = graphQLAnnotations.object(AdditionalTypeTest.class); + + // assert + assertThat(schema.getType("additional"), notNullValue()); + assertThat(schema.getType("additional").toString(), is(additionalType.toString())); + } +} diff --git a/src/test/java/graphql/annotations/GraphQLBatchedTest.java b/src/test/java/graphql/annotations/GraphQLBatchedTest.java index a61c3c1b..213b6f18 100644 --- a/src/test/java/graphql/annotations/GraphQLBatchedTest.java +++ b/src/test/java/graphql/annotations/GraphQLBatchedTest.java @@ -31,16 +31,18 @@ import java.util.Map; import static graphql.Scalars.GraphQLString; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; @SuppressWarnings("unchecked") public class GraphQLBatchedTest { + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } public static class SimpleBatchedField { @@ -60,11 +62,10 @@ public List fields() { @Test public void batchedDataFetcher() throws Throwable { - GraphQLObjectType nestedObject = GraphQLAnnotations.object(SimpleBatchedField.class); + GraphQLObjectType nestedObject = this.graphQLAnnotations.object(SimpleBatchedField.class); assertEquals(nestedObject.getFieldDefinition("a").getType(), GraphQLString); - GraphQLObjectType object = GraphQLAnnotations.object(TestBatchedObject.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestBatchedObject.class).build(); GraphQL graphql = GraphQL.newGraphQL(schema).queryExecutionStrategy(new BatchedExecutionStrategy()).build(); ExecutionResult result = graphql.execute("{ fields { a } }", new TestBatchedObject()); List errors = result.getErrors(); @@ -88,7 +89,7 @@ public List a() { @Test(expectedExceptions = IllegalArgumentException.class) public void noStaticField() { - GraphQLObjectType object = GraphQLAnnotations.object(NoStaticBatchedField.class); + GraphQLObjectType object = this.graphQLAnnotations.object(NoStaticBatchedField.class); } public static class NoListBatchedField { @@ -101,7 +102,7 @@ public String a() { @Test(expectedExceptions = IllegalArgumentException.class) public void noListField() { - GraphQLObjectType object = GraphQLAnnotations.object(NoStaticBatchedField.class); + GraphQLObjectType object = this.graphQLAnnotations.object(NoStaticBatchedField.class); } public static class NoParameterizedBatchedField { @@ -114,6 +115,6 @@ public List a() { @Test(expectedExceptions = IllegalArgumentException.class) public void noParameterizedReturnField() { - GraphQLObjectType object = GraphQLAnnotations.object(NoStaticBatchedField.class); + GraphQLObjectType object = this.graphQLAnnotations.object(NoStaticBatchedField.class); } } diff --git a/src/test/java/graphql/annotations/GraphQLDataFetcherTest.java b/src/test/java/graphql/annotations/GraphQLDataFetcherTest.java index 1cc0626d..233a58df 100644 --- a/src/test/java/graphql/annotations/GraphQLDataFetcherTest.java +++ b/src/test/java/graphql/annotations/GraphQLDataFetcherTest.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -20,27 +20,31 @@ import graphql.annotations.annotationTypes.GraphQLField; import graphql.annotations.annotationTypes.GraphQLName; import graphql.annotations.processor.GraphQLAnnotations; -import graphql.schema.*; +import graphql.schema.DataFetcher; +import graphql.schema.DataFetchingEnvironment; +import graphql.schema.GraphQLSchema; +import graphql.schema.PropertyDataFetcher; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.util.HashMap; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.*; public class GraphQLDataFetcherTest { + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } @Test public void shouldUsePreferredConstructor() { // Given - final GraphQLObjectType object = GraphQLAnnotations.object(GraphQLDataFetcherTest.TestGraphQLQuery.class); - final GraphQLSchema schema = newSchema().query(object).build(); + final GraphQLSchema schema = newAnnotationsSchema().query(GraphQLDataFetcherTest.TestGraphQLQuery.class).build(); GraphQL graphql = GraphQL.newGraphQL(schema).build(); // When @@ -56,8 +60,7 @@ public void shouldUsePreferredConstructor() { @Test public void shouldUseProvidedSoloArgumentForDataFetcherDeclaredInMethod() { // Given - final GraphQLObjectType object = GraphQLAnnotations.object(TestMethodWithDataFetcherGraphQLQuery.class); - final GraphQLSchema schema = newSchema().query(object).build(); + final GraphQLSchema schema = newAnnotationsSchema().query(TestMethodWithDataFetcherGraphQLQuery.class).build(); final GraphQL graphql = GraphQL.newGraphQL(schema).build(); // When @@ -66,14 +69,13 @@ public void shouldUseProvidedSoloArgumentForDataFetcherDeclaredInMethod() { // Then final HashMap data = result.getData(); assertNotNull(data); - assertFalse((Boolean)data.get("great")); + assertFalse((Boolean) data.get("great")); } @Test public void shouldUseTargetAndArgumentsForDataFetcherDeclaredInMethod() { // Given - final GraphQLObjectType object = GraphQLAnnotations.object(TestMethodWithDataFetcherGraphQLQuery.class); - final GraphQLSchema schema = newSchema().query(object).build(); + final GraphQLSchema schema = newAnnotationsSchema().query(TestMethodWithDataFetcherGraphQLQuery.class).build(); final GraphQL graphql = GraphQL.newGraphQL(schema).build(); // When @@ -82,7 +84,7 @@ public void shouldUseTargetAndArgumentsForDataFetcherDeclaredInMethod() { // Then final HashMap data = result.getData(); assertNotNull(data); - assertTrue(((HashMap)data.get("sample")).get("isBad")); + assertTrue(((HashMap) data.get("sample")).get("isBad")); } @GraphQLName("Query") @@ -90,7 +92,7 @@ public static class TestGraphQLQuery { @GraphQLField @GraphQLDataFetcher(SampleDataFetcher.class) public TestSample sample() { // Note that GraphQL uses TestSample to build the graph - return null; + return null; } } @@ -98,11 +100,15 @@ public TestSample sample() { // Note that GraphQL uses TestSample to build the g public static class TestMethodWithDataFetcherGraphQLQuery { @GraphQLField @GraphQLDataFetcher(value = SampleOneArgDataFetcher.class, args = "true") - public Boolean great() { return false; } + public Boolean great() { + return false; + } @GraphQLField @GraphQLDataFetcher(SampleDataFetcher.class) - public TestSampleMethod sample() { return null; } + public TestSampleMethod sample() { + return null; + } } public static class TestSample { @@ -120,7 +126,9 @@ public static class TestSampleMethod { @GraphQLField @GraphQLDataFetcher(value = SampleMultiArgDataFetcher.class, firstArgIsTargetName = true, args = {"true"}) - public Boolean isBad() { return false; } // Defaults to FieldDataFetcher + public Boolean isBad() { + return false; + } // Defaults to FieldDataFetcher } @@ -135,16 +143,12 @@ public static class SampleOneArgDataFetcher implements DataFetcher { private boolean flip = false; public SampleOneArgDataFetcher(String flip) { - this.flip = Boolean.valueOf(flip); + this.flip = Boolean.valueOf(flip); } @Override public Object get(DataFetchingEnvironment environment) { - if ( flip ) { - return !flip; - } else { - return flip; - } + return !flip; } } @@ -160,7 +164,7 @@ public SampleMultiArgDataFetcher(String target, String flip) { public Object get(DataFetchingEnvironment environment) { final Object result = super.get(environment); if (flip) { - return !(Boolean)result; + return !(Boolean) result; } else { return result; } diff --git a/src/test/java/graphql/annotations/GraphQLDirectiveCreationTest.java b/src/test/java/graphql/annotations/GraphQLDirectiveCreationTest.java index 37b43443..9378d61e 100644 --- a/src/test/java/graphql/annotations/GraphQLDirectiveCreationTest.java +++ b/src/test/java/graphql/annotations/GraphQLDirectiveCreationTest.java @@ -21,6 +21,7 @@ import graphql.introspection.Introspection; import graphql.schema.GraphQLArgument; import graphql.schema.GraphQLDirective; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLBoolean; @@ -30,6 +31,13 @@ public class GraphQLDirectiveCreationTest { + private GraphQLAnnotations graphQLAnnotations; + + @BeforeMethod + public void setUp() { + this.graphQLAnnotations = new GraphQLAnnotations(); + } + @GraphQLName("upper") @GraphQLDescription("makes string upper case") @DirectiveLocations({Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.INTERFACE}) @@ -45,7 +53,7 @@ public static class UpperDirective { @Test public void test_directive_creation() { // Act - GraphQLDirective directive = GraphQLAnnotations.directive(UpperDirective.class); + GraphQLDirective directive = this.graphQLAnnotations.directive(UpperDirective.class); // Assert assertEquals(directive.getName(), "upper"); diff --git a/src/test/java/graphql/annotations/GraphQLDirectivesTest.java b/src/test/java/graphql/annotations/GraphQLDirectivesTest.java index 71c6cbfe..2f29e4cc 100644 --- a/src/test/java/graphql/annotations/GraphQLDirectivesTest.java +++ b/src/test/java/graphql/annotations/GraphQLDirectivesTest.java @@ -12,20 +12,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and */ -package graphql.annotations; /** - * Copyright 2016 Yurii Rashkovskii - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - */ +package graphql.annotations; import graphql.ExecutionResult; import graphql.GraphQL; @@ -38,9 +25,9 @@ import graphql.annotations.directives.creation.DirectiveLocations; import graphql.annotations.processor.GraphQLAnnotations; import graphql.annotations.processor.exceptions.GraphQLAnnotationsException; +import graphql.annotations.processor.util.CodeRegistryUtil; import graphql.introspection.Introspection; import graphql.schema.*; -import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -50,50 +37,67 @@ import static graphql.Scalars.GraphQLString; import static graphql.schema.GraphQLDirective.newDirective; import static graphql.schema.GraphQLSchema.newSchema; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; +import static org.testng.Assert.*; public class GraphQLDirectivesTest { - @BeforeClass - public static void setUp() throws Exception { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } + private GraphQLAnnotations graphQLAnnotations; @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - GraphQLAnnotations.getInstance().getContainer().getDirectiveRegistry().clear(); + public void setUp() { + this.graphQLAnnotations = new GraphQLAnnotations(); } - public static class UpperWiring implements AnnotationsDirectiveWiring { @Override public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) { GraphQLFieldDefinition field = (GraphQLFieldDefinition) environment.getElement(); boolean isActive = (boolean) environment.getDirective().getArgument("isActive").getValue(); - DataFetcher dataFetcher = DataFetcherFactories.wrapDataFetcher(field.getDataFetcher(), (((dataFetchingEnvironment, value) -> { + CodeRegistryUtil.wrapDataFetcher(field, environment, (((dataFetchingEnvironment, value) -> { if (value instanceof String && isActive) { return ((String) value).toUpperCase(); } return value; }))); - return field.transform(builder -> builder.dataFetcher(dataFetcher)); + + return field; } } public static class SuffixWiring implements AnnotationsDirectiveWiring { + @Override + public GraphQLInputObjectField onInputObjectField(AnnotationsWiringEnvironment environment) { + GraphQLInputObjectField field = (GraphQLInputObjectField) environment.getElement(); + String suffix = (String) environment.getDirective().getArgument("suffix").getValue(); + return field.transform(builder -> builder.name(field.getName() + suffix)); + } + @Override public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) { GraphQLFieldDefinition field = (GraphQLFieldDefinition) environment.getElement(); String suffix = (String) environment.getDirective().getArgument("suffix").getValue(); - DataFetcher dataFetcher = DataFetcherFactories.wrapDataFetcher(field.getDataFetcher(), (((dataFetchingEnvironment, value) -> { + CodeRegistryUtil.wrapDataFetcher(field, environment, (dataFetchingEnvironment, value) -> { if (value instanceof String) { return value + suffix; } return value; - }))); - return field.transform(builder -> builder.dataFetcher(dataFetcher)); + }); + + return field; + } + + @Override + public GraphQLArgument onArgument(AnnotationsWiringEnvironment environment) { + GraphQLArgument element = (GraphQLArgument) environment.getElement(); + String suffix = (String) environment.getDirective().getArgument("suffix").getValue(); + return element.transform(builder -> builder.name(element.getName() + suffix)); + } + + @Override + public GraphQLInputObjectType onInputObjectType(AnnotationsWiringEnvironment environment) { + GraphQLInputObjectType element = (GraphQLInputObjectType) environment.getElement(); + String suffix = (String) environment.getDirective().getArgument("suffix").getValue(); + return element; } } @@ -128,26 +132,85 @@ public static String name() { } } + public static class Query4 { + @GraphQLField + public static String nameWithArgument(@GraphQLDirectives({@Directive(name = "suffix", + wiringClass = SuffixWiring.class, argumentsValues = {"coolSuffixForArg"})}) + @GraphQLName("extensionArg") String extensionArg) { + return "yarin" + extensionArg; + } + } + + public static class InputObject { + @GraphQLField + @GraphQLDirectives({@Directive(name = "suffix", wiringClass = SuffixWiring.class, argumentsValues = {"coolSuffix"})}) + private String a; + + @GraphQLField + private int b; + + public InputObject(@GraphQLName("a") String a, @GraphQLName("b") int b) { + this.a = a; + this.b = b; + } + } + + public static class Query5 { + @GraphQLField + public static String nameWithInputObject(@GraphQLName("inputObject") InputObject input) { + return "yarin"; + } + } + + @Test + public void queryNameWithInputObject_directivesProvidedToRegistry_wiringOfInputObjectIsActivated() { + GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString)) + .validLocations(Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION, Introspection.DirectiveLocation.FIELD_DEFINITION).build(); + + this.graphQLAnnotations.getContainer().getDirectiveRegistry().put(suffixDirective.getName(), suffixDirective); + GraphQLObjectType object = this.graphQLAnnotations.object(Query5.class); + GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build(); + GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build(); + GraphQLFieldDefinition nameWithInputObject = schema.getQueryType().getFieldDefinition("nameWithInputObject"); + GraphQLInputObjectField field = ((GraphQLInputObjectType) nameWithInputObject.getArgument("inputObject").getType()).getField("acoolSuffix"); + assertNotNull(field); + } + + @Test + public void queryNameWithArgument_directivesProvidedToRegistry_wiringOfArgumentIsActivated() { + GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString)) + .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION).build(); + this.graphQLAnnotations.getContainer().getDirectiveRegistry().put(suffixDirective.getName(), suffixDirective); + GraphQLObjectType object = this.graphQLAnnotations.object(Query4.class); + GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build(); + GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build(); + + ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { nameWithArgument(extensionArgcoolSuffixForArg: \"ext\") }"); + assertTrue(result.getErrors().isEmpty()); + } @Test(expectedExceptions = GraphQLAnnotationsException.class) public void queryName_noDirectivesProvidedToRegistry_exceptionIsThrown() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(Query.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLObjectType object = this.graphQLAnnotations.object(Query.class); + GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build(); + GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build(); GraphQL.newGraphQL(schema).build().execute("query { name }"); } @GraphQLName("upperCase") @DirectiveLocations(Introspection.DirectiveLocation.FIELD_DEFINITION) - public static class UpperCase{ + public static class UpperCase { boolean isActive; } @Test public void queryName_directivesProvidedToRegistry_wiringIsActivated() throws Exception { - GraphQLDirective upperCase = GraphQLAnnotations.directive(UpperCase.class); - GraphQLObjectType object = GraphQLAnnotations.object(Query.class, upperCase); - GraphQLSchema schema = newSchema().query(object).build(); + this.graphQLAnnotations.directive(UpperCase.class); + GraphQLObjectType object = this.graphQLAnnotations.object(Query.class); + GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build(); + + GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { name }"); assertTrue(result.getErrors().isEmpty()); @@ -158,8 +221,10 @@ public void queryName_directivesProvidedToRegistry_wiringIsActivated() throws Ex public void queryNameWithFalse_directivesProvidedToRegistry_wiringIsActivated() throws Exception { GraphQLDirective upperCase = newDirective().name("upperCase").argument(builder -> builder.name("isActive").type(GraphQLBoolean)) .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build(); - GraphQLObjectType object = GraphQLAnnotations.object(Query.class, upperCase); - GraphQLSchema schema = newSchema().query(object).build(); + this.graphQLAnnotations.getContainer().getDirectiveRegistry().put(upperCase.getName(), upperCase); + GraphQLObjectType object = this.graphQLAnnotations.object(Query.class); + GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build(); + GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { nameWithFalse }"); assertTrue(result.getErrors().isEmpty()); @@ -170,8 +235,10 @@ public void queryNameWithFalse_directivesProvidedToRegistry_wiringIsActivated() public void queryNameWithNoArgs_directivesProvidedToRegistry_wiringIsActivated() throws Exception { GraphQLDirective upperCase = newDirective().name("upperCase").argument(builder -> builder.name("isActive").type(GraphQLBoolean).defaultValue(true)) .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build(); - GraphQLObjectType object = GraphQLAnnotations.object(Query2.class, upperCase); - GraphQLSchema schema = newSchema().query(object).build(); + this.graphQLAnnotations.getContainer().getDirectiveRegistry().put(upperCase.getName(), upperCase); + GraphQLObjectType object = this.graphQLAnnotations.object(Query2.class); + GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build(); + GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { nameWithNoArgs }"); assertTrue(result.getErrors().isEmpty()); @@ -182,8 +249,11 @@ public void queryNameWithNoArgs_directivesProvidedToRegistry_wiringIsActivated() public void queryNameWithNoArgs_noDefaultValue_exceptionIsThrown() throws Exception { GraphQLDirective upperCase = newDirective().name("upperCase").argument(builder -> builder.name("isActive").type(GraphQLBoolean)) .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build(); - GraphQLObjectType object = GraphQLAnnotations.object(Query2.class, upperCase); - GraphQLSchema schema = newSchema().query(object).build(); + this.graphQLAnnotations.getContainer().getDirectiveRegistry().put(upperCase.getName(), upperCase); + GraphQLObjectType object = this.graphQLAnnotations.object(Query2.class); + GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build(); + + GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build(); GraphQL.newGraphQL(schema).build().execute("query { nameWithNoArgs }"); } @@ -193,9 +263,13 @@ public void queryName_chainedDirectives_wiringIsActivatedInCorrectOrder() throws GraphQLDirective upperCase = newDirective().name("upperCase").argument(builder -> builder.name("isActive").type(GraphQLBoolean).defaultValue(true)) .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build(); GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString)) - .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build(); - GraphQLObjectType object = GraphQLAnnotations.object(Query3.class, upperCase, suffixDirective); - GraphQLSchema schema = newSchema().query(object).build(); + .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION).build(); + this.graphQLAnnotations.getContainer().getDirectiveRegistry().put(upperCase.getName(), upperCase); + this.graphQLAnnotations.getContainer().getDirectiveRegistry().put(suffixDirective.getName(), suffixDirective); + GraphQLObjectType object = this.graphQLAnnotations.object(Query3.class); + GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build(); + + GraphQLSchema schema = newSchema().query(object).codeRegistry(codeRegistry).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { name }"); assertTrue(result.getErrors().isEmpty()); diff --git a/src/test/java/graphql/annotations/GraphQLEnumTest.java b/src/test/java/graphql/annotations/GraphQLEnumTest.java index ee927e9a..c29477bb 100644 --- a/src/test/java/graphql/annotations/GraphQLEnumTest.java +++ b/src/test/java/graphql/annotations/GraphQLEnumTest.java @@ -18,21 +18,13 @@ import graphql.GraphQL; import graphql.annotations.annotationTypes.GraphQLField; import graphql.annotations.annotationTypes.GraphQLName; -import graphql.annotations.processor.GraphQLAnnotations; -import graphql.schema.GraphQLObjectType; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.assertEquals; public class GraphQLEnumTest { - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - public enum Foo { ONE, TWO @@ -69,8 +61,7 @@ public static User user(@GraphQLName("param") Foo param) { @Test public void test() throws IllegalAccessException, NoSuchMethodException, InstantiationException { - GraphQLObjectType queryObject = GraphQLAnnotations.object(Query.class); - GraphQL graphql = GraphQL.newGraphQL(newSchema().query(queryObject).build()).build(); + GraphQL graphql = GraphQL.newGraphQL(newAnnotationsSchema().query(Query.class).build()).build(); ExecutionResult result = graphql.execute("{ defaultUser{ getName } }"); assertEquals(result.getData().toString(), "{defaultUser={getName=ONE}}"); @@ -78,8 +69,7 @@ public void test() throws IllegalAccessException, NoSuchMethodException, Instant @Test public void testAsInput() throws IllegalAccessException, NoSuchMethodException, InstantiationException { - GraphQLObjectType queryObject = GraphQLAnnotations.object(Query.class); - GraphQL graphql = GraphQL.newGraphQL(newSchema().query(queryObject).build()).build(); + GraphQL graphql = GraphQL.newGraphQL(newAnnotationsSchema().query(Query.class).build()).build(); ExecutionResult result = graphql.execute("{ user(param:TWO){ getName } }"); assertEquals(result.getData().toString(), "{user={getName=TWO}}"); diff --git a/src/test/java/graphql/annotations/GraphQLExtensionsTest.java b/src/test/java/graphql/annotations/GraphQLExtensionsTest.java index 4025ac03..8eedc418 100644 --- a/src/test/java/graphql/annotations/GraphQLExtensionsTest.java +++ b/src/test/java/graphql/annotations/GraphQLExtensionsTest.java @@ -28,7 +28,7 @@ import java.util.Map; import static graphql.Scalars.GraphQLString; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.*; public class GraphQLExtensionsTest { @@ -98,7 +98,7 @@ public void fields() { GraphQLAnnotations instance = new GraphQLAnnotations(); instance.registerTypeExtension(TestObjectExtension.class); GraphQLObjectHandler graphQLObjectHandler = instance.getObjectHandler(); - GraphQLObjectType object = graphQLObjectHandler.getObject(GraphQLExtensionsTest.TestObject.class, instance.getContainer()); + GraphQLObjectType object = graphQLObjectHandler.getGraphQLType(GraphQLExtensionsTest.TestObject.class, instance.getContainer()); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 5); @@ -114,13 +114,7 @@ public void fields() { @Test public void values() { - GraphQLAnnotations instance = new GraphQLAnnotations(); - instance.registerTypeExtension(TestObjectExtension.class); - GraphQLObjectHandler graphQLObjectHandler = instance.getObjectHandler(); - GraphQLObjectType object = graphQLObjectHandler.getObject(GraphQLExtensionsTest.TestObject.class, instance.getContainer()); - - GraphQLSchema schema = newSchema().query(object).build(); - GraphQLSchema schemaInherited = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).typeExtension(TestObjectExtension.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field field2 field3 field4 field5}", new GraphQLExtensionsTest.TestObject()); Map data = result.getData(); @@ -136,7 +130,7 @@ public void testDuplicateField() { GraphQLAnnotations instance = new GraphQLAnnotations(); GraphQLObjectHandler graphQLObjectHandler = instance.getObjectHandler(); instance.registerTypeExtension(TestObjectExtensionInvalid.class); - GraphQLAnnotationsException e = expectThrows(GraphQLAnnotationsException.class, () -> graphQLObjectHandler.getObject(TestObject.class,instance.getContainer())); + GraphQLAnnotationsException e = expectThrows(GraphQLAnnotationsException.class, () -> graphQLObjectHandler.getGraphQLType(TestObject.class, instance.getContainer())); assertTrue(e.getMessage().startsWith("Duplicate field")); } } diff --git a/src/test/java/graphql/annotations/GraphQLFragmentTest.java b/src/test/java/graphql/annotations/GraphQLFragmentTest.java index 6ab5416c..df8c901e 100644 --- a/src/test/java/graphql/annotations/GraphQLFragmentTest.java +++ b/src/test/java/graphql/annotations/GraphQLFragmentTest.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -20,8 +20,6 @@ import graphql.annotations.annotationTypes.GraphQLField; import graphql.annotations.annotationTypes.GraphQLTypeResolver; import graphql.annotations.processor.GraphQLAnnotations; -import graphql.annotations.processor.retrievers.GraphQLInterfaceRetriever; -import graphql.schema.GraphQLInterfaceType; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import graphql.schema.TypeResolver; @@ -30,14 +28,17 @@ import java.util.*; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.AssertJUnit.assertEquals; public class GraphQLFragmentTest { + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } static Map registry; @@ -49,24 +50,17 @@ public void init() { public void testInterfaceInlineFragment() throws Exception { // Given registry = new HashMap<>(); - GraphQLInterfaceRetriever graphQLInterfaceRetriever=GraphQLAnnotations.getInstance().getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); - - - GraphQLObjectType rootType = GraphQLAnnotations.object(RootObject.class); - GraphQLObjectType objectType2 = GraphQLAnnotations.object(MyObject2.class); + GraphQLObjectType objectType2 = this.graphQLAnnotations.object(MyObject2.class); registry.put("MyObject2", objectType2); - GraphQLObjectType objectType = GraphQLAnnotations.object(MyObject.class); + GraphQLObjectType objectType = this.graphQLAnnotations.object(MyObject.class); registry.put("MyObject", objectType); - GraphQLInterfaceType iface = (GraphQLInterfaceType) graphQLInterfaceRetriever.getInterface(MyInterface.class,GraphQLAnnotations.getInstance().getContainer()); - GraphQLSchema schema = GraphQLSchema.newSchema() - .query(rootType) - .build(new HashSet<>(Arrays.asList(iface, objectType))); + GraphQLSchema schema = newAnnotationsSchema().query(RootObject.class).additionalType(MyObject.class).additionalType(MyInterface.class).build(); GraphQL graphQL2 = GraphQL.newGraphQL(schema).build(); @@ -112,12 +106,12 @@ public String getB() { } @GraphQLTypeResolver(value = MyTypeResolver.class) - public static interface MyInterface { + public interface MyInterface { @GraphQLField - public String getA(); + String getA(); @GraphQLField - public String getB(); + String getB(); } public static class MyTypeResolver implements TypeResolver { diff --git a/src/test/java/graphql/annotations/GraphQLInputTest.java b/src/test/java/graphql/annotations/GraphQLInputTest.java index 6dac1ef7..bf918ebc 100644 --- a/src/test/java/graphql/annotations/GraphQLInputTest.java +++ b/src/test/java/graphql/annotations/GraphQLInputTest.java @@ -26,13 +26,14 @@ import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import graphql.schema.TypeResolver; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static graphql.schema.GraphQLSchema.newSchema; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; @@ -40,12 +41,21 @@ @SuppressWarnings("unchecked") public class GraphQLInputTest { + private GraphQLAnnotations graphQLAnnotations; + private static GraphQLObjectType testObjectType; + + @BeforeMethod + public void setUp() { + this.graphQLAnnotations = new GraphQLAnnotations(); + testObjectType = this.graphQLAnnotations.object(TestObject.class); + } + public static class Resolver implements TypeResolver { @Override public GraphQLObjectType getType(TypeResolutionEnvironment env) { try { - return GraphQLAnnotations.object(TestObject.class); + return testObjectType; } catch (GraphQLAnnotationsException e) { return null; } @@ -177,8 +187,8 @@ public TestObject iface() { @Test public void query() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)) - .additionalType(GraphQLAnnotations.object(TestObject.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class) + .additionalType(TestObject.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ object { value(input:{key:\"test\"}) } }", new Query()); @@ -188,7 +198,7 @@ public void query() { @Test public void queryMultipleDefinitions() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryMultipleDefinitions.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query(QueryMultipleDefinitions.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ something(code: {firstField:\"a\",secondField:\"b\"}) somethingElse(code: {firstField:\"c\",secondField:\"d\"}) }", new QueryMultipleDefinitions()); @@ -199,7 +209,7 @@ public void queryMultipleDefinitions() { @Test public void queryWithRecursion() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryRecursion.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query(QueryRecursion.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ object { value(input:{key:\"test\"}) } }", new QueryRecursion()); @@ -213,7 +223,7 @@ public void queryWithRecursion() { @Test public void queryWithList() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryList.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query(QueryList.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ object { value(input:[[[{key:\"test\", complex:[{subKey:\"subtest\"},{subKey:\"subtest2\"}]}]]]) } }", new QueryList()); @@ -222,7 +232,7 @@ public void queryWithList() { @Test public void queryWithInterface() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryIface.class)).build(Collections.singleton(GraphQLAnnotations.object(TestObject.class))); + GraphQLSchema schema = newAnnotationsSchema().query(QueryIface.class).additionalType(TestObject.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ iface { value(input:{key:\"test\"}) } }", new QueryIface()); @@ -285,7 +295,7 @@ public Skill getSkill() { @Test public void testInputAndOutputWithSameName() { // arrange + act - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryInputAndOutput.class)).build(); + GraphQLSchema schema = newSchema().query(this.graphQLAnnotations.object(QueryInputAndOutput.class)).build(); // assert assertEquals(schema.getQueryType().getFieldDefinition("getHero").getType().getName(), "hero"); assertEquals(schema.getQueryType().getFieldDefinition("getString").getArgument("input").getType().getName(), "Inputhero"); @@ -296,7 +306,7 @@ public void testInputAndOutputWithSameName() { @Test public void testInputAndOutputSameClass() { // arrange + act - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryInputAndOutput2.class)).build(); + GraphQLSchema schema = newSchema().query(this.graphQLAnnotations.object(QueryInputAndOutput2.class)).build(); // assert assertEquals(schema.getQueryType().getFieldDefinition("getSkill").getType().getName(), "Skill"); assertEquals(schema.getQueryType().getFieldDefinition("getA").getArgument("skill").getType().getName(), "InputSkill"); @@ -354,7 +364,7 @@ public String getB(@GraphQLName("input") BIn input) { @Test public void testInputAndOutputWithSameNameWithChildrenWithSameName() { // arrange + act - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QuerySameNameWithChildren.class)).build(); + GraphQLSchema schema = newSchema().query(this.graphQLAnnotations.object(QuerySameNameWithChildren.class)).build(); // assert assertEquals(schema.getQueryType().getFieldDefinition("getAout").getType().getName(), "A"); assertEquals(schema.getQueryType().getFieldDefinition("getAout").getType().getClass(), GraphQLObjectType.class); diff --git a/src/test/java/graphql/annotations/GraphQLInterfaceTest.java b/src/test/java/graphql/annotations/GraphQLInterfaceTest.java index 6cff2ea6..f461fc9f 100644 --- a/src/test/java/graphql/annotations/GraphQLInterfaceTest.java +++ b/src/test/java/graphql/annotations/GraphQLInterfaceTest.java @@ -24,32 +24,25 @@ import graphql.annotations.processor.GraphQLAnnotations; import graphql.annotations.processor.exceptions.GraphQLAnnotationsException; import graphql.annotations.processor.retrievers.GraphQLInterfaceRetriever; -import graphql.schema.DataFetcher; -import graphql.schema.DataFetchingEnvironment; -import graphql.schema.GraphQLFieldDefinition; -import graphql.schema.GraphQLInterfaceType; -import graphql.schema.GraphQLObjectType; -import graphql.schema.GraphQLOutputType; -import graphql.schema.GraphQLSchema; -import graphql.schema.GraphQLUnionType; -import graphql.schema.TypeResolver; +import graphql.schema.*; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.util.List; import java.util.Map; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; @SuppressWarnings("unchecked") public class GraphQLInterfaceTest { + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } interface NoResolverIface { @@ -59,9 +52,9 @@ interface NoResolverIface { @Test public void noResolver() { - GraphQLInterfaceRetriever graphQLInterfaceRetriever = GraphQLAnnotations.getInstance().getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); + GraphQLInterfaceRetriever graphQLInterfaceRetriever = this.graphQLAnnotations.getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); - GraphQLObjectType object = (GraphQLObjectType) graphQLInterfaceRetriever.getInterface(NoResolverIface.class, GraphQLAnnotations.getInstance().getContainer()); + GraphQLObjectType object = (GraphQLObjectType) graphQLInterfaceRetriever.getInterface(NoResolverIface.class, this.graphQLAnnotations.getContainer()); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 1); assertEquals(fields.get(0).getName(), "value"); @@ -76,7 +69,7 @@ public static Resolver getInstance() { @Override public GraphQLObjectType getType(TypeResolutionEnvironment env) { try { - return GraphQLAnnotations.object(TestObject.class); + return new GraphQLAnnotations().object(TestObject.class); } catch (GraphQLAnnotationsException e) { return null; } @@ -118,8 +111,8 @@ public String value() { @Test public void test() { - GraphQLInterfaceRetriever graphQLInterfaceRetriever = GraphQLAnnotations.getInstance().getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); - GraphQLInterfaceType iface = (GraphQLInterfaceType) graphQLInterfaceRetriever.getInterface(TestIface.class, GraphQLAnnotations.getInstance().getContainer()); + GraphQLInterfaceRetriever graphQLInterfaceRetriever = this.graphQLAnnotations.getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); + GraphQLInterfaceType iface = (GraphQLInterfaceType) graphQLInterfaceRetriever.getInterface(TestIface.class, this.graphQLAnnotations.getContainer()); List fields = iface.getFieldDefinitions(); assertEquals(fields.size(), 1); assertEquals(fields.get(0).getName(), "value"); @@ -127,15 +120,15 @@ public void test() { @Test public void testUnion() { - GraphQLInterfaceRetriever graphQLInterfaceRetriever = GraphQLAnnotations.getInstance().getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); - GraphQLUnionType unionType = (GraphQLUnionType) graphQLInterfaceRetriever.getInterface(TestUnion.class, GraphQLAnnotations.getInstance().getContainer()); + GraphQLInterfaceRetriever graphQLInterfaceRetriever = this.graphQLAnnotations.getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); + GraphQLUnionType unionType = (GraphQLUnionType) graphQLInterfaceRetriever.getInterface(TestUnion.class, this.graphQLAnnotations.getContainer()); assertEquals(unionType.getTypes().size(), 1); assertEquals(unionType.getTypes().get(0).getName(), "TestObject1"); } @Test public void testInterfaces() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); List ifaces = object.getInterfaces(); assertEquals(ifaces.size(), 1); assertEquals(ifaces.get(0).getName(), "TestIface"); @@ -166,8 +159,7 @@ public UnionQuery(TestUnion union) { @Test public void query() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)) - .additionalType(GraphQLAnnotations.object(TestObject.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class).additionalType(TestObject.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ iface { value } }"); @@ -177,7 +169,7 @@ public void query() { @Test public void queryUnion() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(UnionQuery.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query(UnionQuery.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ union { ... on TestObject1 { value } } }", new UnionQuery(new TestObject1())); diff --git a/src/test/java/graphql/annotations/GraphQLIterableAndArrayTest.java b/src/test/java/graphql/annotations/GraphQLIterableAndArrayTest.java index f1891bb2..6cb8bb04 100644 --- a/src/test/java/graphql/annotations/GraphQLIterableAndArrayTest.java +++ b/src/test/java/graphql/annotations/GraphQLIterableAndArrayTest.java @@ -21,7 +21,6 @@ import graphql.annotations.processor.GraphQLAnnotations; import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -31,14 +30,17 @@ import java.util.LinkedHashMap; import java.util.List; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; public class GraphQLIterableAndArrayTest { + + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } static class TestMappedObject { @@ -103,8 +105,7 @@ public TestObjectDB[][] get(DataFetchingEnvironment environment) { @Test public void queryWithArray() { - GraphQLObjectType object = GraphQLAnnotations.object(IterableAndArrayTestQuery.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(IterableAndArrayTestQuery.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{array {name foo}}"); assertTrue(result.getErrors().isEmpty()); @@ -114,8 +115,7 @@ public void queryWithArray() { @Test public void queryWithList() { - GraphQLObjectType object = GraphQLAnnotations.object(IterableAndArrayTestQuery.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(IterableAndArrayTestQuery.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{list {name foo}}"); assertTrue(result.getErrors().isEmpty()); @@ -125,8 +125,7 @@ public void queryWithList() { @Test public void queryWithArrayWithinAnArray() { - GraphQLObjectType object = GraphQLAnnotations.object(IterableAndArrayTestQuery.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(IterableAndArrayTestQuery.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{arrayWithinArray {name foo}}"); assertTrue(result.getErrors().isEmpty()); diff --git a/src/test/java/graphql/annotations/GraphQLObjectTest.java b/src/test/java/graphql/annotations/GraphQLObjectTest.java index 511d5a88..1c74bf56 100644 --- a/src/test/java/graphql/annotations/GraphQLObjectTest.java +++ b/src/test/java/graphql/annotations/GraphQLObjectTest.java @@ -27,6 +27,7 @@ import graphql.annotations.processor.searchAlgorithms.ParentalSearch; import graphql.annotations.processor.typeBuilders.InputObjectBuilder; import graphql.annotations.processor.typeFunctions.TypeFunction; +import graphql.annotations.processor.util.CodeRegistryUtil; import graphql.schema.*; import graphql.schema.GraphQLType; import graphql.schema.idl.SchemaParser; @@ -40,8 +41,9 @@ import java.util.function.Supplier; import static graphql.Scalars.GraphQLString; -import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_SUFFIX; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX; +import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_SUFFIX; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLSchema.newSchema; import static org.testng.Assert.*; @@ -49,9 +51,11 @@ @SuppressWarnings("unchecked") public class GraphQLObjectTest { + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } public static class DefaultAValue implements Supplier { @@ -199,7 +203,7 @@ public String getY() { @GraphQLField @GraphQLPrettify @GraphQLName("daniel") - public String setM(){ + public String setM() { return "Asdf"; } @@ -208,7 +212,7 @@ public String setM(){ @Test public void objectCreation_nameIsCorrect() { // Act - GraphQLObjectType object = GraphQLAnnotations.object(NameTest.class); + GraphQLObjectType object = this.graphQLAnnotations.object(NameTest.class); // Assert assertNotNull(object.getFieldDefinition("awesome")); @@ -221,8 +225,7 @@ public void objectCreation_nameIsCorrect() { @Test public void fetchTestMappedObject_assertNameIsMappedFromDBObject() { - GraphQLObjectType object = GraphQLAnnotations.object(TestQuery.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestQuery.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{object {name aaa}}"); assertTrue(result.getErrors().isEmpty()); @@ -232,7 +235,7 @@ public void fetchTestMappedObject_assertNameIsMappedFromDBObject() { @Test public void namedFields() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObjectNamedArgs.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObjectNamedArgs.class); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 1); @@ -245,20 +248,20 @@ public void namedFields() { @Test public void metainformation() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); assertEquals(object.getName(), "TestObject"); assertEquals(object.getDescription(), "TestObject object"); } @Test public void objectClass() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); assertTrue(object instanceof GraphQLObjectType); } @Test public void testSchema() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); String schema = new SchemaPrinter().print(object); assertTrue(schema.contains("type TestObject {")); TypeDefinitionRegistry reg = new SchemaParser().parse(schema); @@ -268,7 +271,7 @@ public void testSchema() { @Test public void fields() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 8); @@ -303,8 +306,10 @@ public void fields() { assertEquals(fields.get(5).getName(), "privateTest"); assertEquals(fields.get(6).getName(), "publicTest"); - assertEquals(fields.get(5).getDataFetcher().getClass(), PropertyDataFetcher.class); - assertEquals(fields.get(6).getDataFetcher().getClass(), PropertyDataFetcher.class); + DataFetcher dataFetcher1 = CodeRegistryUtil.getDataFetcher(this.graphQLAnnotations.getContainer().getCodeRegistryBuilder(), "TestObject", fields.get(5)); + DataFetcher dataFetcher2 = CodeRegistryUtil.getDataFetcher(this.graphQLAnnotations.getContainer().getCodeRegistryBuilder(), "TestObject", fields.get(6)); + assertEquals(dataFetcher1.getClass(), PropertyDataFetcher.class); + assertEquals(dataFetcher2.getClass(), PropertyDataFetcher.class); assertEquals(fields.get(7).getName(), "z_nonOptionalString"); assertTrue(fields.get(7).getType() instanceof graphql.schema.GraphQLNonNull); @@ -320,12 +325,12 @@ public String field() { @Test public void methodInheritance() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); - GraphQLObjectType objectInherited = GraphQLAnnotations.object(TestObjectInherited.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); + GraphQLObjectType objectInherited = this.graphQLAnnotations.object(TestObjectInherited.class); assertEquals(object.getFieldDefinitions().size(), objectInherited.getFieldDefinitions().size()); - GraphQLSchema schema = newSchema().query(object).build(); - GraphQLSchema schemaInherited = newSchema().query(objectInherited).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).build(); + GraphQLSchema schemaInherited = newAnnotationsSchema().query(TestObjectInherited.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field0}", new TestObject()); assertEquals(((Map) result.getData()).get("field0"), "test"); @@ -361,9 +366,7 @@ public Long id() { @Test public void methodInheritanceWithGenerics() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObjectBridgMethod.class); - - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObjectBridgMethod.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{id}", new TestObjectBridgMethod()); assertEquals(((Map) result.getData()).get("id"), 1L); @@ -381,7 +384,7 @@ public static class IfaceImpl implements Iface { @Test public void interfaceInheritance() { - GraphQLObjectType object = GraphQLAnnotations.object(IfaceImpl.class); + GraphQLObjectType object = this.graphQLAnnotations.object(IfaceImpl.class); assertEquals(object.getFieldDefinitions().size(), 1); assertEquals(object.getFieldDefinition("field").getType(), GraphQLString); @@ -401,7 +404,7 @@ public String setAnotherValue(String s) { @Test public void accessors() { - GraphQLObjectType object = GraphQLAnnotations.object(TestAccessors.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestAccessors.class); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 2); fields.sort(Comparator.comparing(GraphQLFieldDefinition::getName)); @@ -413,7 +416,7 @@ public void accessors() { @Test public void defaults() { - GraphQLObjectType object = GraphQLAnnotations.object(TestDefaults.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestDefaults.class); assertEquals(object.getName(), "TestDefaults"); assertNull(object.getDescription()); } @@ -464,7 +467,7 @@ public void setBooleanField(boolean booleanField) { @Test public void field() { - GraphQLObjectType object = GraphQLAnnotations.object(TestField.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestField.class); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 1); assertEquals(fields.get(0).getName(), "field1"); @@ -481,7 +484,7 @@ public String getValue() { @Test public void onMethod() { - GraphQLObjectType object = GraphQLAnnotations.object(OnMethodTest.class); + GraphQLObjectType object = this.graphQLAnnotations.object(OnMethodTest.class); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 1); assertEquals(fields.get(0).getName(), "getValue"); @@ -511,8 +514,7 @@ public String someField() { @Test public void dataFetcher() { - GraphQLObjectType object = GraphQLAnnotations.object(TestDataFetcher.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestDataFetcher.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field someField}", new TestObject()); assertTrue(result.getErrors().isEmpty()); @@ -522,8 +524,7 @@ public void dataFetcher() { @Test public void query() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field0}", new TestObject()); assertTrue(result.getErrors().isEmpty()); @@ -541,8 +542,7 @@ public void query() { @Test public void queryField() { - GraphQLObjectType object = GraphQLAnnotations.object(TestField.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestField.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field1}", new TestField()); assertTrue(result.getErrors().isEmpty()); @@ -551,8 +551,7 @@ public void queryField() { @Test public void queryPrivateField() { - GraphQLObjectType object = GraphQLAnnotations.object(PrivateTestField.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(PrivateTestField.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field1, field2, booleanField}", new PrivateTestField()); assertTrue(result.getErrors().isEmpty()); @@ -564,8 +563,7 @@ public void queryPrivateField() { @Test public void defaultArg() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{fieldWithArgs(a: \"test\")}", new TestObject()); assertTrue(result.getErrors().isEmpty()); @@ -590,7 +588,7 @@ public static class Class2 { @Test public void recursiveTypes() { GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations(); - GraphQLObjectType object = graphQLAnnotations.getObjectHandler().getObject(Class1.class, graphQLAnnotations.getContainer()); + GraphQLObjectType object = graphQLAnnotations.getObjectHandler().getGraphQLType(Class1.class, graphQLAnnotations.getContainer()); GraphQLSchema schema = newSchema().query(object).build(); Class1 class1 = new Class1(); @@ -624,8 +622,8 @@ public UUID id() { @Test public void customType() { - GraphQLAnnotations.register(new UUIDTypeFunction()); - GraphQLObjectType object = GraphQLAnnotations.object(TestCustomType.class); + this.graphQLAnnotations.registerTypeFunction(new UUIDTypeFunction()); + GraphQLObjectType object = this.graphQLAnnotations.object(TestCustomType.class); assertEquals(object.getFieldDefinition("id").getType(), GraphQLString); } @@ -639,7 +637,7 @@ public UUID id() { @Test public void customTypeFunction() { - GraphQLObjectType object = GraphQLAnnotations.object(TestCustomTypeFunction.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestCustomTypeFunction.class); assertEquals(object.getFieldDefinition("id").getType(), GraphQLString); } @@ -693,12 +691,12 @@ public static class InputObject { @Test public void inputObjectArgument() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObjectInput.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObjectInput.class); GraphQLArgument argument = object.getFieldDefinition("test").getArgument("arg"); assertTrue(argument.getType() instanceof GraphQLInputObjectType); assertEquals(argument.getName(), "arg"); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObjectInput.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{ test( other:0,arg: { a:\"ok\", b:2 }) }", new TestObjectInput()); assertTrue(result.getErrors().isEmpty()); Map v = (Map) result.getData(); @@ -707,12 +705,12 @@ public void inputObjectArgument() { @Test public void complexInputObjectArgument() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObjectInput.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObjectInput.class); GraphQLArgument argument = object.getFieldDefinition("test2").getArgument("arg"); assertTrue(argument.getType() instanceof GraphQLInputObjectType); assertEquals(argument.getName(), "arg"); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObjectInput.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{ test2(arg: {inputs:[{ a:\"ok\", b:2 }]}, other:0) }", new TestObjectInput()); assertTrue(result.getErrors().isEmpty()); Map v = result.getData(); @@ -724,7 +722,7 @@ public void inputObject() { GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever(); GraphQLInputObjectType type = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever), new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()). - getInputObjectBuilder(InputObject.class, GraphQLAnnotations.getInstance().getContainer()).build(); + getInputObjectBuilder(InputObject.class, this.graphQLAnnotations.getContainer()).build(); assertEquals(type.getName(), DEFAULT_INPUT_PREFIX + InputObject.class.getSimpleName(), "Type name prefix did not match expected value"); assertEquals(type.getFields().size(), InputObject.class.getDeclaredFields().length); @@ -733,12 +731,12 @@ public void inputObject() { @Test public void inputObjectCustomPrefixes() { GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever(); - ProcessingElementsContainer container = GraphQLAnnotations.getInstance().getContainer(); + ProcessingElementsContainer container = this.graphQLAnnotations.getContainer(); container.setInputPrefix(""); container.setInputSuffix("Input"); GraphQLInputObjectType type = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever), new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()). - getInputObjectBuilder(InputObject.class, GraphQLAnnotations.getInstance().getContainer()).build(); + getInputObjectBuilder(InputObject.class, this.graphQLAnnotations.getContainer()).build(); assertEquals(type.getName(), "" + InputObject.class.getSimpleName() + "Input", "Type name prefix did not match expected value"); assertEquals(type.getFields().size(), InputObject.class.getDeclaredFields().length); @@ -792,7 +790,7 @@ public String toString() { @Test public void queryOptional() { - GraphQLObjectType object = GraphQLAnnotations.object(OptionalTest.class); + GraphQLObjectType object = this.graphQLAnnotations.object(OptionalTest.class); GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); @@ -805,11 +803,11 @@ public void queryOptional() { @Test public void optionalInput() { - GraphQLObjectType object = GraphQLAnnotations.object(OptionalTest.class); + GraphQLObjectType object = this.graphQLAnnotations.object(OptionalTest.class); GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever(); GraphQLInputObjectType inputObject = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever), new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()). - getInputObjectBuilder(OptionalTest.class, GraphQLAnnotations.getInstance().getContainer()).build(); + getInputObjectBuilder(OptionalTest.class, this.graphQLAnnotations.getContainer()).build(); GraphQLObjectType mutation = GraphQLObjectType.newObject().name("mut").field(newFieldDefinition().name("test").type(object). argument(GraphQLArgument.newArgument().type(inputObject).name("input").build()).dataFetcher(environment -> { @@ -847,7 +845,7 @@ public String toString() { @Test public void queryEnum() { - GraphQLObjectType object = GraphQLAnnotations.object(EnumTest.class); + GraphQLObjectType object = this.graphQLAnnotations.object(EnumTest.class); GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); @@ -866,7 +864,7 @@ public String first(List l) { @Test public void parametrizedArg() { - GraphQLObjectType object = GraphQLAnnotations.object(ParametrizedArgsTest.class); + GraphQLObjectType object = this.graphQLAnnotations.object(ParametrizedArgsTest.class); GraphQLInputType t = object.getFieldDefinition("first").getArguments().get(0).getType(); assertTrue(t instanceof GraphQLList); assertEquals(((GraphQLList) t).getWrappedType(), Scalars.GraphQLString); @@ -892,7 +890,7 @@ public String off() { @Test public void inheritGraphQLField() { - GraphQLObjectType object = GraphQLAnnotations.object(InheritGraphQLFieldTest.class); + GraphQLObjectType object = this.graphQLAnnotations.object(InheritGraphQLFieldTest.class); assertNotNull(object.getFieldDefinition("on")); assertNull(object.getFieldDefinition("off")); assertNotNull(object.getFieldDefinition("inheritedOn")); diff --git a/src/test/java/graphql/annotations/GraphQLSimpleSchemaTest.java b/src/test/java/graphql/annotations/GraphQLSimpleSchemaTest.java index 7ab7dc67..e36ed669 100644 --- a/src/test/java/graphql/annotations/GraphQLSimpleSchemaTest.java +++ b/src/test/java/graphql/annotations/GraphQLSimpleSchemaTest.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -18,23 +18,12 @@ import graphql.GraphQL; import graphql.annotations.annotationTypes.GraphQLField; import graphql.annotations.annotationTypes.GraphQLInvokeDetached; -import graphql.annotations.processor.GraphQLAnnotations; -import graphql.annotations.processor.retrievers.GraphQLObjectHandler; -import graphql.schema.GraphQLObjectType; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.assertEquals; public class GraphQLSimpleSchemaTest { - - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - - public static class User { private String name; @@ -73,10 +62,7 @@ public User defaultUser2() { @Test public void detachedCall() { - GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations(); - GraphQLObjectHandler graphQLObjectHandler = graphQLAnnotations.getObjectHandler(); - GraphQLObjectType queryObject = graphQLObjectHandler.getObject(Query.class,graphQLAnnotations.getContainer()); - GraphQL graphql = GraphQL.newGraphQL(newSchema().query(queryObject).build()).build(); + GraphQL graphql = GraphQL.newGraphQL(newAnnotationsSchema().query(Query.class).build()).build(); ExecutionResult result = graphql.execute("{ defaultUser{ name } }"); String actual = result.getData().toString(); @@ -85,10 +71,7 @@ public void detachedCall() { @Test public void staticCall() { - GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations(); - GraphQLObjectHandler graphQLObjectHandler = graphQLAnnotations.getObjectHandler(); - GraphQLObjectType queryObject = graphQLObjectHandler.getObject(Query.class,graphQLAnnotations.getContainer()); - GraphQL graphql = GraphQL.newGraphQL(newSchema().query(queryObject).build()).build(); + GraphQL graphql = GraphQL.newGraphQL(newAnnotationsSchema().query(Query.class).build()).build(); ExecutionResult result = graphql.execute("{ defaultUser2{ name } }"); String actual = result.getData().toString(); diff --git a/src/test/java/graphql/annotations/GraphQLUnionTest.java b/src/test/java/graphql/annotations/GraphQLUnionTest.java index 29e60e29..5d5adf56 100644 --- a/src/test/java/graphql/annotations/GraphQLUnionTest.java +++ b/src/test/java/graphql/annotations/GraphQLUnionTest.java @@ -29,7 +29,7 @@ import java.util.List; import java.util.Map; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -39,18 +39,20 @@ @SuppressWarnings({"WeakerAccess", "unchecked", "AssertEqualsBetweenInconvertibleTypesTestNG"}) public class GraphQLUnionTest { + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + graphQLAnnotations = new GraphQLAnnotations(); } @Test public void getGraphQLType_typeIsUnion_returnsUnionType() throws Exception { //Arrange - GraphQLInterfaceRetriever graphQLInterfaceRetriever = GraphQLAnnotations.getInstance().getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); + GraphQLInterfaceRetriever graphQLInterfaceRetriever = this.graphQLAnnotations.getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); //Act - GraphQLOutputType unionType = graphQLInterfaceRetriever.getInterface(Hardware.class, GraphQLAnnotations.getInstance().getContainer()); + GraphQLOutputType unionType = graphQLInterfaceRetriever.getInterface(Hardware.class, this.graphQLAnnotations.getContainer()); //Assert assertThat(unionType, instanceOf(GraphQLUnionType.class)); @@ -59,11 +61,11 @@ public void getGraphQLType_typeIsUnion_returnsUnionType() throws Exception { @Test public void getResolver_resolverIsDefaultOne_returnsUnionTypeResolver() throws Exception { //Arrange - GraphQLInterfaceRetriever graphQLInterfaceRetriever = GraphQLAnnotations.getInstance().getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); + GraphQLInterfaceRetriever graphQLInterfaceRetriever = this.graphQLAnnotations.getObjectHandler().getTypeRetriever().getGraphQLInterfaceRetriever(); //Act - GraphQLUnionType unionType = (GraphQLUnionType) graphQLInterfaceRetriever.getInterface(Hardware.class, GraphQLAnnotations.getInstance().getContainer()); - TypeResolver typeResolver = unionType.getTypeResolver(); + GraphQLUnionType unionType = (GraphQLUnionType) graphQLInterfaceRetriever.getInterface(Hardware.class, this.graphQLAnnotations.getContainer()); + TypeResolver typeResolver = this.graphQLAnnotations.getContainer().getCodeRegistryBuilder().getTypeResolver(unionType); //Assert assertThat(typeResolver, instanceOf(UnionTypeResolver.class)); @@ -72,7 +74,7 @@ public void getResolver_resolverIsDefaultOne_returnsUnionTypeResolver() throws E @Test public void unionType_buildSchema_unionIsAFieldOfQuery() throws Exception { //Act - GraphQLObjectType object = GraphQLAnnotations.object(Query.class); + GraphQLObjectType object = this.graphQLAnnotations.object(Query.class); List unions = object.getFieldDefinitions(); //Assert @@ -81,7 +83,7 @@ public void unionType_buildSchema_unionIsAFieldOfQuery() throws Exception { @Test public void unionQuery_returnTypeIsComputer_getComputer() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query((Query.class)).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); String query = "{ getHardwareComputer{ ... on Computer {name}, ... on Screen{resolution}} }"; @@ -92,7 +94,7 @@ public void unionQuery_returnTypeIsComputer_getComputer() { @Test public void unionQuery_returnTypeIsScreen_getScreen() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query((Query.class)).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); String query = "{ getHardwareScreen{ ... on Computer {name}, ... on Screen{resolution}} }"; @@ -103,7 +105,7 @@ public void unionQuery_returnTypeIsScreen_getScreen() { @Test public void unionQueryWithCustomTypeResolver_askForDog_getDog() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query((Query.class)).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); String query = "{ getPet(kindOfPet:\"dog\"){ ... on Cat {mew}, ... on Dog{waf}} }"; @@ -114,7 +116,7 @@ public void unionQueryWithCustomTypeResolver_askForDog_getDog() { @Test public void unionQueryWithCustomTypeResolver_askForCat_getCat() { - GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); String query = "{ getPet(kindOfPet:\"cat\"){ ... on Cat {mew}, ... on Dog{waf}} }"; @@ -166,23 +168,28 @@ class Query { @GraphQLField @GraphQLDataFetcher(PetDataFetcher.class) - public Pet getPet(@GraphQLName("kindOfPet") @GraphQLNonNull String kindOfPet){return null;} + public Pet getPet(@GraphQLName("kindOfPet") @GraphQLNonNull String kindOfPet) { + return null; + } } + static class Computer implements Hardware { @GraphQLField String name; + public Computer(String name) { this.name = name; } } + @GraphQLUnion(typeResolver = PetResolver.class, possibleTypes = {Cat.class, Dog.class}) interface Pet { } - static class Cat implements Pet{ + static class Cat implements Pet { @GraphQLField String mew; @@ -191,7 +198,7 @@ public Cat(String mew) { } } - static class Dog implements Pet{ + static class Dog implements Pet { @GraphQLField String waf; @@ -204,10 +211,9 @@ public static class PetResolver implements TypeResolver { @Override public GraphQLObjectType getType(TypeResolutionEnvironment env) { Object object = env.getObject(); - if(object instanceof Dog) { + if (object instanceof Dog) { return env.getSchema().getObjectType("Dog"); - } - else { + } else { return env.getSchema().getObjectType("Cat"); } } @@ -217,10 +223,9 @@ public static class PetDataFetcher implements DataFetcher { @Override public Pet get(DataFetchingEnvironment environment) { String nameOfPet = environment.getArgument("kindOfPet"); - if(nameOfPet.toLowerCase().equals("dog")) { + if (nameOfPet.toLowerCase().equals("dog")) { return new Dog("waf"); - } - else { + } else { return new Cat("mew"); } } diff --git a/src/test/java/graphql/annotations/MethodDataFetcherTest.java b/src/test/java/graphql/annotations/MethodDataFetcherTest.java index 77e5add3..2684348f 100644 --- a/src/test/java/graphql/annotations/MethodDataFetcherTest.java +++ b/src/test/java/graphql/annotations/MethodDataFetcherTest.java @@ -18,26 +18,29 @@ import graphql.ExecutionResult; import graphql.GraphQL; import graphql.annotations.annotationTypes.*; -import graphql.annotations.annotationTypes.GraphQLType; import graphql.annotations.dataFetchers.MethodDataFetcher; import graphql.annotations.processor.GraphQLAnnotations; -import graphql.schema.*; +import graphql.schema.DataFetcher; +import graphql.schema.DataFetchingEnvironment; +import graphql.schema.GraphQLSchema; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.ArrayList; import java.util.HashMap; import java.util.Map; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; +import static graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment; import static org.testng.Assert.*; @SuppressWarnings("unchecked") public class MethodDataFetcherTest { + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } public static class StaticApi { @@ -48,9 +51,8 @@ public static String name() { } @Test - public void query_staticMethod_valueIsDeterminedByMethod(){ - GraphQLObjectType object = GraphQLAnnotations.object(StaticApi.class); - GraphQLSchema schema = newSchema().query(object).build(); + public void query_staticMethod_valueIsDeterminedByMethod() { + GraphQLSchema schema = newAnnotationsSchema().query(StaticApi.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { name }").root(new StaticApi())); assertTrue(result.getErrors().isEmpty()); @@ -75,8 +77,7 @@ public Api1 queryField() { @Test public void query_onlyApiClass_valueIsDeterminedByField() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(Query1.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query1.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { queryField { name } }").root(new Query1())); assertTrue(result.getErrors().isEmpty()); @@ -103,8 +104,7 @@ public Api2 queryField() { @Test public void query_onlyApiClass_valueIsDeterminedByMethod() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(Query2.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query2.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { queryField { name } }").root(new Query2())); assertTrue(result.getErrors().isEmpty()); @@ -147,8 +147,7 @@ public static class Query3 { @Test public void query_apiAndDbClass_valueIsDeterminedByDBField() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(Query3.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query3.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { queryField { nameX } }").root(new Query3())); assertTrue(result.getErrors().isEmpty()); @@ -195,8 +194,7 @@ public static class Query4 { @Test public void query_apiAndDbClass_valueIsDeterminedByGetPrefixDBMethod() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(Query4.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query4.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { queryField { nameX } }").root(new Query4())); assertTrue(result.getErrors().isEmpty()); @@ -243,8 +241,7 @@ public static class Query6 { @Test public void query_apiAndDbClass_valueIsDeterminedByIsPrefixDBMethod() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(Query6.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query6.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { queryField { nameX } }").root(new Query6())); assertTrue(result.getErrors().isEmpty()); @@ -295,8 +292,7 @@ public static class Query7 { @Test public void query_apiAndDbClass_valueIsDeterminedByDBMethod() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(Query7.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query7.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { queryField { nameX } }").root(new Query7())); assertTrue(result.getErrors().isEmpty()); @@ -339,8 +335,7 @@ public static class Query5 { @Test public void query_apiAndDbClassAndApiIsInvokeDetached_valueIsDeterminedByApiMethod() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(Query5.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query5.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { queryField { name } }").root(new Query5())); assertTrue(result.getErrors().isEmpty()); @@ -362,10 +357,7 @@ public String method() throws TestException { public void exceptionRethrowing() { try { MethodDataFetcher methodDataFetcher = new MethodDataFetcher(getClass().getMethod("method"), null, null); - methodDataFetcher.get(new DataFetchingEnvironmentImpl(this, new HashMap<>(), - null, null, null, new ArrayList<>(), - null, null, null, null, - null, null, null, null)); + methodDataFetcher.get(newDataFetchingEnvironment().source(this).arguments(new HashMap<>()).build()); } catch (NoSuchMethodException e) { e.printStackTrace(); } @@ -456,8 +448,7 @@ public ApiType get(DataFetchingEnvironment environment) { @Test public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByEntity() { - GraphQLObjectType object = GraphQLAnnotations.object(Query.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { a } }"); assertTrue(result.getErrors().isEmpty()); @@ -467,8 +458,7 @@ public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetached_valueIsDetermi @Test public void queryingOneCanonizedFieldNotAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByEntity() { - GraphQLObjectType object = GraphQLAnnotations.object(Query.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { canonizedType { m } } }"); assertTrue(result.getErrors().isEmpty()); @@ -477,8 +467,7 @@ public void queryingOneCanonizedFieldNotAnnotatedWithGraphQLInvokeDetached_value @Test public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetachedAndNameIsPrettified_valueIsDeterminedByEntity() { - GraphQLObjectType object = GraphQLAnnotations.object(Query.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { x } }"); assertTrue(result.getErrors().isEmpty()); @@ -487,8 +476,7 @@ public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetachedAndNameIsPretti @Test public void queryingOneFieldAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByApiEntity() { - GraphQLObjectType object = GraphQLAnnotations.object(Query.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { b } }"); assertTrue(result.getErrors().isEmpty()); @@ -497,8 +485,7 @@ public void queryingOneFieldAnnotatedWithGraphQLInvokeDetached_valueIsDetermined @Test public void queryingFieldsFromApiEntityFetcher_valueIsDeterminedByApiEntity() { - GraphQLObjectType object = GraphQLAnnotations.object(Query.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { apiField { a b } }"); assertTrue(result.getErrors().isEmpty()); @@ -508,8 +495,7 @@ public void queryingFieldsFromApiEntityFetcher_valueIsDeterminedByApiEntity() { @Test public void queryingFieldsFromNoApiEntityFetcher_noMatchingFieldInEntity_throwException() { - GraphQLObjectType object = GraphQLAnnotations.object(Query.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(Query.class).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { c } }"); assertFalse(result.getErrors().isEmpty()); diff --git a/src/test/java/graphql/annotations/RelayTest.java b/src/test/java/graphql/annotations/RelayTest.java index c8c0601d..85874ab8 100644 --- a/src/test/java/graphql/annotations/RelayTest.java +++ b/src/test/java/graphql/annotations/RelayTest.java @@ -23,27 +23,37 @@ import graphql.schema.*; import graphql.schema.GraphQLNonNull; import graphql.schema.GraphQLType; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.util.HashMap; import java.util.Map; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.*; @SuppressWarnings("unchecked") public class RelayTest { + private GraphQLAnnotations graphQLAnnotations; + + private static GraphQLObjectType resultObject; + + @BeforeClass + public void setUp() { + resultObject = new GraphQLAnnotations().object(Result.class); + } + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } public static class ResultTypeResolver implements TypeResolver { - @Override public GraphQLObjectType getType(TypeResolutionEnvironment env) { - return GraphQLAnnotations.object(Result.class); + return resultObject; } } @@ -53,7 +63,7 @@ public interface IResult { int getI(); } - public static class Result implements IResult{ + public static class Result implements IResult { private final int i; public Result(int i) { @@ -66,22 +76,28 @@ public int getI() { } public static class WrongReturnType { - @GraphQLField @GraphQLRelayMutation + @GraphQLField + @GraphQLRelayMutation public int doSomething() { return 0; } } public static class TestObject { - @GraphQLField @GraphQLRelayMutation + @GraphQLField + @GraphQLRelayMutation public Result doSomething() { return new Result(0); } - @GraphQLField @GraphQLRelayMutation + + @GraphQLField + @GraphQLRelayMutation public Result doSomethingElse(@GraphQLName("a") @GraphQLDescription("A") int a, @GraphQLName("b") int b) { return new Result(a - b); } - @GraphQLField @GraphQLRelayMutation + + @GraphQLField + @GraphQLRelayMutation public IResult doSomethingI() { return new Result(0); } @@ -90,12 +106,12 @@ public IResult doSomethingI() { @Test(expectedExceptions = RuntimeException.class) public void notAnObjectType() { - GraphQLObjectType object = GraphQLAnnotations.object(WrongReturnType.class); + GraphQLObjectType object = this.graphQLAnnotations.object(WrongReturnType.class); } @Test public void noArgMutation() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); GraphQLFieldDefinition doSomething = object.getFieldDefinition("doSomething"); @@ -113,7 +129,7 @@ public void noArgMutation() { assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); - GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).mutation(TestObject.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); @@ -129,13 +145,13 @@ public void noArgMutation() { @Test public void interfaceReturningMutation() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); GraphQLFieldDefinition doSomething = object.getFieldDefinition("doSomethingI"); assertNotNull(doSomething); - GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).mutation(TestObject.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); @@ -152,7 +168,7 @@ public void interfaceReturningMutation() { @Test public void argMutation() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); GraphQLFieldDefinition doSomethingElse = object.getFieldDefinition("doSomethingElse"); @@ -174,7 +190,7 @@ public void argMutation() { assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); - GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).mutation(TestObject.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); @@ -190,7 +206,7 @@ public void argMutation() { @Test public void argVariableMutation() { - GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class); GraphQLFieldDefinition doSomethingElse = object.getFieldDefinition("doSomethingElse"); @@ -211,7 +227,7 @@ public void argVariableMutation() { assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); - GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).mutation(TestObject.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); diff --git a/src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java b/src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java index 58bafed4..94283ac7 100644 --- a/src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java +++ b/src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java @@ -38,6 +38,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static graphql.annotations.processor.util.RelayKit.EMPTY_CONNECTION; import static graphql.schema.GraphQLSchema.newSchema; import static java.util.Collections.emptyList; @@ -46,9 +47,11 @@ @SuppressWarnings("unchecked") public class GraphQLConnectionTest { + private GraphQLAnnotations graphQLAnnotations; + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } public static class Obj { @@ -127,17 +130,17 @@ public String getCursor(Obj entity) { @Test(expectedExceptions = GraphQLConnectionException.class) public void fieldList() { - GraphQLObjectType object = GraphQLAnnotations.object(TestConnectionOnField.class); + GraphQLObjectType object = this.graphQLAnnotations.object(TestConnectionOnField.class); GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ objs(first: 1) { edges { cursor node { id, val } } } }", new TestListField(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world")))); } + @Test public void methodList() { - GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestConnections.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ getObjs(first: 1) { edges { cursor node { id, val } } } }", @@ -152,8 +155,8 @@ public void methodList() { @Test public void customRelayMethodList() { try { - GraphQLAnnotations.getInstance().setRelay(new CustomRelay()); - GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class); + this.graphQLAnnotations.setRelay(new CustomRelay()); + GraphQLObjectType object = this.graphQLAnnotations.object(TestConnections.class); GraphQLSchema schema = newSchema().query(object).build(); graphql.schema.GraphQLObjectType f = (GraphQLObjectType) schema.getType("ObjConnection"); @@ -164,7 +167,7 @@ public void customRelayMethodList() { GraphQLObjectType pageInfo = (GraphQLObjectType) schema.getType("PageInfo"); assertTrue(pageInfo.getFieldDefinition("additionalInfo") != null); } finally { - GraphQLAnnotations.getInstance().setRelay(new Relay()); + this.graphQLAnnotations.setRelay(new Relay()); } } @@ -179,8 +182,7 @@ public void testResult(String name, ExecutionResult result) { @Test public void methodStream() { - GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestConnections.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ getObjStream(first: 1) { edges { cursor node { id, val } } } }", @@ -193,8 +195,7 @@ public void methodStream() { @Test public void methodNonNull() { - GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestConnections.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ getNonNullObjs(first: 1) { edges { cursor node { id, val } } } }", @@ -207,8 +208,7 @@ public void methodNonNull() { @Test public void methodNull() { - GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestConnections.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ getNullObj(first: 1) { edges { cursor node { id, val } } } }", @@ -223,8 +223,7 @@ public void methodNull() { @Test public void emptyListData() { - GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestConnections.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ getObjStreamWithParam(first: 1, filter:\"hel\") { edges { cursor node { id, val } } } }", @@ -239,8 +238,7 @@ public void emptyListData() { @Test public void methodListWithParam() { - GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestConnections.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ getObjStreamWithParam(first: 2, filter:\"hel\") { edges { cursor node { id, val } } } }", @@ -291,8 +289,7 @@ public String getCursor(Obj entity) { @Test public void customConnection() { - GraphQLObjectType object = GraphQLAnnotations.object(TestCustomConnection.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(TestCustomConnection.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); ExecutionResult result = graphQL.execute("{ getObjs(first: 1) { edges { cursor node { id, val } } } }", @@ -308,8 +305,7 @@ public void customConnection() { @Test public void duplicateConnection() { try { - GraphQLObjectType object = GraphQLAnnotations.object(DuplicateTest.class); - newSchema().query(object).build(); + newAnnotationsSchema().query(DuplicateTest.class).build(); } catch (GraphQLAnnotationsException e) { fail("Schema cannot be created", e); } diff --git a/src/test/java/graphql/annotations/connection/GraphQLEnhancedConnectionTest.java b/src/test/java/graphql/annotations/connection/GraphQLEnhancedConnectionTest.java index e10c47a7..53a748ce 100644 --- a/src/test/java/graphql/annotations/connection/GraphQLEnhancedConnectionTest.java +++ b/src/test/java/graphql/annotations/connection/GraphQLEnhancedConnectionTest.java @@ -23,38 +23,27 @@ import graphql.annotations.processor.GraphQLAnnotations; import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; -import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.testng.Assert.assertEquals; @SuppressWarnings("ALL") public class GraphQLEnhancedConnectionTest { - private static GraphQL graphQL; + private GraphQL graphQL; + private GraphQLAnnotations graphQLAnnotations; - @BeforeClass - public static void setUp() throws Exception { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - GraphQLObjectType object = GraphQLAnnotations.object(TestListField.class); - GraphQLSchema schema = newSchema().query(object).build(); - - graphQL = GraphQL.newGraphQL(schema).build(); - } - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); + public void setUp() throws Exception { + this.graphQLAnnotations = new GraphQLAnnotations(); + GraphQLSchema schema = newAnnotationsSchema().query(TestListField.class).build(); + graphQL = GraphQL.newGraphQL(schema).build(); } public static class Obj { @@ -139,14 +128,14 @@ public List get(DataFetchingEnvironment environment) { @Test(expectedExceptions = GraphQLConnectionException.class) public void ConnectionFieldDoesntReturnPaginatedData_tryToBuildSchema_getException() throws Exception { //Act + Assert - GraphQLAnnotations.object(NotValidConnectionField.class); + this.graphQLAnnotations.object(NotValidConnectionField.class); } @Test public void validDatafetcher_queryForCursors_getValidCursors() throws Exception { //Arrange ExecutionInput executionInput = new ExecutionInput("{ objs(first:2) { edges { cursor } } }", - null, "CONTEXT", null, null); + null, "CONTEXT", null, new HashMap<>()); //Act ExecutionResult result = graphQL.execute(executionInput); Map>>>> data = result.getData(); @@ -161,7 +150,7 @@ public void validDatafetcher_queryForCursors_getValidCursors() throws Exception public void fetchConnectionAsync() throws Exception { //Arrange ExecutionInput executionInput = new ExecutionInput("{ objsAsync(first:2) { edges { cursor } } }", - null, "CONTEXT", null, null); + null, "CONTEXT", null, new HashMap<>()); //Act ExecutionResult result = graphQL.execute(executionInput); Map>>>> data = result.getData(); @@ -176,7 +165,7 @@ public void fetchConnectionAsync() throws Exception { public void validDatafetcher_queryForValues_returnsValidValues() throws Exception { //Arrange ExecutionInput executionInput = new ExecutionInput("{ objs(first:2) { edges { cursor node { id, val } } } }", - null, "CONTEXT", null, null); + null, "CONTEXT", null, new HashMap<>()); //Act ExecutionResult result = graphQL.execute(executionInput); @@ -193,7 +182,7 @@ public void validDatafetcher_queryForHasPreviousPage_returnsFalse() throws Excep //Arrange ExecutionInput executionInput = new ExecutionInput("{ objs(first:2) { pageInfo { hasPreviousPage } } }", - null, "CONTEXT", null, null); + null, "CONTEXT", null, new HashMap<>()); //Act ExecutionResult result = graphQL.execute(executionInput); @@ -208,7 +197,7 @@ public void validDatafetcher_queryForHasNextPage_returnsTrue() throws Exception //Arrange ExecutionInput executionInput = new ExecutionInput("{ objs(first:2) { pageInfo { hasNextPage } } }", - null, "CONTEXT", null, null); + null, "CONTEXT", null, new HashMap<>()); //Act ExecutionResult result = graphQL.execute(executionInput); diff --git a/src/test/java/graphql/annotations/connection/GraphQLSimpleConnectionTest.java b/src/test/java/graphql/annotations/connection/GraphQLSimpleConnectionTest.java index cde98583..43008ec8 100644 --- a/src/test/java/graphql/annotations/connection/GraphQLSimpleConnectionTest.java +++ b/src/test/java/graphql/annotations/connection/GraphQLSimpleConnectionTest.java @@ -23,7 +23,6 @@ import graphql.annotations.processor.GraphQLAnnotations; import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -32,23 +31,24 @@ import java.util.HashMap; import java.util.List; -import static graphql.schema.GraphQLSchema.newSchema; +import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.testng.Assert.assertEquals; public class GraphQLSimpleConnectionTest { + private GraphQLAnnotations graphQLAnnotations; + + @BeforeMethod public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - GraphQLAnnotations.getInstance().getContainer().getProcessing().clear(); + this.graphQLAnnotations = new GraphQLAnnotations(); } @Test public void simpleConnection_buildSchema_TypeOfSimpleConnectionIsGraphQLList() throws Exception { - GraphQLObjectType object = GraphQLAnnotations.object(MainConnection.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(MainConnection.class).build(); String objsTypeName = schema.getQueryType().getFieldDefinition("objs").getType().getName(); @@ -57,18 +57,17 @@ public void simpleConnection_buildSchema_TypeOfSimpleConnectionIsGraphQLList() t @Test(expectedExceptions = GraphQLConnectionException.class) public void simpleConnection_fieldDoesNotHaveDataFetcherAnnotation_throwsError() { - GraphQLAnnotations.object(TestConnectionOnField.class); + this.graphQLAnnotations.object(TestConnectionOnField.class); } @Test(expectedExceptions = GraphQLConnectionException.class) public void simpleConnection_returnTypeIsNotValid_throwsError() { - GraphQLAnnotations.object(TestConnectionNotGoodReturnType.class); + this.graphQLAnnotations.object(TestConnectionNotGoodReturnType.class); } @Test public void simpleConnection_queryForOverAll_getCorrectAnswer() { - GraphQLObjectType object = GraphQLAnnotations.object(MainConnection.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(MainConnection.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); @@ -81,8 +80,7 @@ public void simpleConnection_queryForOverAll_getCorrectAnswer() { @Test public void simpleConnection_queryForTwoObject_getTwoObject() { - GraphQLObjectType object = GraphQLAnnotations.object(MainConnection.class); - GraphQLSchema schema = newSchema().query(object).build(); + GraphQLSchema schema = newAnnotationsSchema().query(MainConnection.class).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); diff --git a/src/test/java/graphql/annotations/directives/DirectiveWirerTest.java b/src/test/java/graphql/annotations/directives/DirectiveWirerTest.java index 0fbed26c..46b42862 100644 --- a/src/test/java/graphql/annotations/directives/DirectiveWirerTest.java +++ b/src/test/java/graphql/annotations/directives/DirectiveWirerTest.java @@ -29,10 +29,13 @@ public class DirectiveWirerTest { private DirectiveWirer directiveWirer; + private String parentName = "parent"; + private GraphQLCodeRegistry.Builder builder; @BeforeMethod public void setUp() throws Exception { directiveWirer = new DirectiveWirer(); + builder = mock(GraphQLCodeRegistry.Builder.class); } // GraphQLFieldDefinition @@ -46,8 +49,10 @@ public void wireFieldDefinition_validLocations_correctMethodIsCalled() throws Ex GraphQLFieldDefinition directiveContainer = GraphQLFieldDefinition.newFieldDefinition().name("bla") .type(GraphQLString).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); - AnnotationsWiringEnvironmentImpl lowerCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("lowerCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); + AnnotationsWiringEnvironmentImpl lowerCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("lowerCase"), parentName, builder); when(upperWiring.onField(upperCaseEnv)).thenReturn(directiveContainer); when(lowerWiring.onField(lowerCaseEnv)).thenReturn(directiveContainer); @@ -59,7 +64,7 @@ public void wireFieldDefinition_validLocations_correctMethodIsCalled() throws Ex map.put(lowerCase, lowerWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -75,7 +80,8 @@ public void wireFieldDefinition_invalidLocations_exceptionIsThrown() throws Exce GraphQLFieldDefinition directiveContainer = GraphQLFieldDefinition.newFieldDefinition().name("bla") .type(GraphQLString).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onField(upperCaseEnv)).thenReturn(directiveContainer); @@ -84,7 +90,7 @@ public void wireFieldDefinition_invalidLocations_exceptionIsThrown() throws Exce map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLObjectType @@ -96,7 +102,8 @@ public void wireGraphQLObjectType_validLocations_correctMethodIsCalled() throws GraphQLObjectType directiveContainer = GraphQLObjectType.newObject().name("asdf").build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onObject(upperCaseEnv)).thenReturn(directiveContainer); @@ -105,7 +112,7 @@ public void wireGraphQLObjectType_validLocations_correctMethodIsCalled() throws map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -119,7 +126,8 @@ public void wireGraphQLObjectType_invalidLocations_exceptionIsThrown() throws Ex GraphQLObjectType directiveContainer = GraphQLObjectType.newObject().name("asdf00").build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onObject(upperCaseEnv)).thenReturn(directiveContainer); @@ -128,7 +136,7 @@ public void wireGraphQLObjectType_invalidLocations_exceptionIsThrown() throws Ex map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLArgument @@ -140,7 +148,8 @@ public void wireGraphQLArgument_validLocations_correctMethodIsCalled() throws Ex GraphQLArgument directiveContainer = GraphQLArgument.newArgument().name("asdf").type(GraphQLString).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onArgument(upperCaseEnv)).thenReturn(directiveContainer); @@ -149,7 +158,7 @@ public void wireGraphQLArgument_validLocations_correctMethodIsCalled() throws Ex map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -163,7 +172,8 @@ public void wireGraphQLArgument_invalidLocations_exceptionIsThrown() throws Exce GraphQLArgument directiveContainer = GraphQLArgument.newArgument().name("asdf0").type(GraphQLString).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onArgument(upperCaseEnv)).thenReturn(directiveContainer); @@ -172,7 +182,7 @@ public void wireGraphQLArgument_invalidLocations_exceptionIsThrown() throws Exce map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLInterfaceType @@ -189,7 +199,8 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) { } }).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onInterface(upperCaseEnv)).thenReturn(directiveContainer); @@ -198,7 +209,7 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) { map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -217,7 +228,8 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) { } }).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onInterface(upperCaseEnv)).thenReturn(directiveContainer); @@ -226,7 +238,7 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) { map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLUnionType @@ -239,7 +251,8 @@ public void wireGraphQLUnionType_validLocations_correctMethodIsCalled() throws E GraphQLUnionType directiveContainer = GraphQLUnionType.newUnionType().name("asdf") .possibleType(GraphQLObjectType.newObject().name("Asdfaaaa").build()).typeResolver(env -> null).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onUnion(upperCaseEnv)).thenReturn(directiveContainer); @@ -248,7 +261,7 @@ public void wireGraphQLUnionType_validLocations_correctMethodIsCalled() throws E map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -264,7 +277,8 @@ public void wireGraphQLUnionType_invalidLocations_exceptionIsThrown() throws Exc .possibleType(GraphQLObjectType.newObject().name("Asdfaaaa").build()).typeResolver(env -> null).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onUnion(upperCaseEnv)).thenReturn(directiveContainer); @@ -273,7 +287,7 @@ public void wireGraphQLUnionType_invalidLocations_exceptionIsThrown() throws Exc map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLEnumType @@ -285,7 +299,8 @@ public void wireGraphQLEnumType_validLocations_correctMethodIsCalled() throws Ex GraphQLEnumType directiveContainer = GraphQLEnumType.newEnum().name("asdf").value("asdfasdf").build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onEnum(upperCaseEnv)).thenReturn(directiveContainer); @@ -294,7 +309,7 @@ public void wireGraphQLEnumType_validLocations_correctMethodIsCalled() throws Ex map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -308,7 +323,8 @@ public void wireGraphQLEnumType_invalidLocations_exceptionIsThrown() throws Exce GraphQLEnumType directiveContainer = GraphQLEnumType.newEnum().name("asdf").value("asdfasdf").build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onEnum(upperCaseEnv)).thenReturn(directiveContainer); @@ -317,7 +333,7 @@ public void wireGraphQLEnumType_invalidLocations_exceptionIsThrown() throws Exce map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLEnumValueDefinition @@ -329,7 +345,8 @@ public void wireGraphQLEnumValueDefinition_validLocations_correctMethodIsCalled( GraphQLEnumValueDefinition directiveContainer = GraphQLEnumValueDefinition.newEnumValueDefinition().name("asdf").build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onEnumValue(upperCaseEnv)).thenReturn(directiveContainer); @@ -338,7 +355,7 @@ public void wireGraphQLEnumValueDefinition_validLocations_correctMethodIsCalled( map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -352,7 +369,8 @@ public void wireGraphQLEnumValueDefinition_invalidLocations_exceptionIsThrown() GraphQLEnumValueDefinition directiveContainer = GraphQLEnumValueDefinition.newEnumValueDefinition().name("asdf").build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onEnumValue(upperCaseEnv)).thenReturn(directiveContainer); @@ -361,7 +379,7 @@ public void wireGraphQLEnumValueDefinition_invalidLocations_exceptionIsThrown() map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLScalarType @@ -373,7 +391,8 @@ public void wireGraphQLScalarType_validLocations_correctMethodIsCalled() throws GraphQLScalarType directiveContainer = GraphQLString; - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onScalar(upperCaseEnv)).thenReturn(directiveContainer); @@ -382,7 +401,7 @@ public void wireGraphQLScalarType_validLocations_correctMethodIsCalled() throws map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -396,7 +415,8 @@ public void wireGraphQLScalarType_invalidLocations_exceptionIsThrown() throws Ex GraphQLScalarType directiveContainer = GraphQLString; - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onScalar(upperCaseEnv)).thenReturn(directiveContainer); @@ -405,7 +425,7 @@ public void wireGraphQLScalarType_invalidLocations_exceptionIsThrown() throws Ex map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLInputObjectType @@ -418,7 +438,8 @@ public void wireGraphQLInputObjectType_validLocations_correctMethodIsCalled() th GraphQLInputObjectType directiveContainer = GraphQLInputObjectType.newInputObject().name("asdf") .build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onInputObjectType(upperCaseEnv)).thenReturn(directiveContainer); @@ -427,7 +448,7 @@ public void wireGraphQLInputObjectType_validLocations_correctMethodIsCalled() th map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -442,7 +463,8 @@ public void wireGraphQLInputObjectType_invalidLocations_exceptionIsThrown() thro GraphQLInputObjectType directiveContainer = GraphQLInputObjectType.newInputObject().name("asdf") .build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onInputObjectType(upperCaseEnv)).thenReturn(directiveContainer); @@ -451,7 +473,7 @@ public void wireGraphQLInputObjectType_invalidLocations_exceptionIsThrown() thro map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } // GraphQLInputObjectField @@ -464,7 +486,8 @@ public void wireGraphQLInputObjectField_validLocations_correctMethodIsCalled() t GraphQLInputObjectField directiveContainer = GraphQLInputObjectField.newInputObjectField().name("asdf") .type(GraphQLInputObjectType.newInputObject().name("dfdf").build()).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl( + directiveContainer, directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onInputObjectField(upperCaseEnv)).thenReturn(directiveContainer); @@ -473,7 +496,7 @@ public void wireGraphQLInputObjectField_validLocations_correctMethodIsCalled() t map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); // Assert @@ -488,7 +511,8 @@ public void wireGraphQLInputObjectField_invalidLocations_exceptionIsThrown() thr GraphQLInputObjectField directiveContainer = GraphQLInputObjectField.newInputObjectField().name("asdf") .type(GraphQLInputObjectType.newInputObject().name("dfdf").build()).build(); - AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); + AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, + directiveContainer.getDirective("upperCase"), parentName, builder); when(upperWiring.onInputObjectField(upperCaseEnv)).thenReturn(directiveContainer); @@ -497,7 +521,7 @@ public void wireGraphQLInputObjectField_invalidLocations_exceptionIsThrown() thr map.put(upperCase, upperWiring); // Act - directiveWirer.wire(directiveContainer, map); + directiveWirer.wire(directiveContainer, map, builder, parentName); } } diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/BigDecimalFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/BigDecimalFunctionTests.java index 18f5c48d..40816ea8 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/BigDecimalFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/BigDecimalFunctionTests.java @@ -14,8 +14,6 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.math.BigDecimal; @@ -25,11 +23,6 @@ import static org.testng.Assert.assertEquals; public class BigDecimalFunctionTests { - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_bigDecimalType_returnsGraphQLBigDecimal() { //arrange diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/BigIntegerFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/BigIntegerFunctionTests.java index bacdfffd..8293ca5b 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/BigIntegerFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/BigIntegerFunctionTests.java @@ -14,8 +14,6 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.math.BigInteger; @@ -25,11 +23,6 @@ import static org.testng.Assert.assertEquals; public class BigIntegerFunctionTests { - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_bigIntegerType_returnsGraphQLBigInteger() { //arrange diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/BooleanFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/BooleanFunctionTests.java index 23aa76f7..47033b93 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/BooleanFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/BooleanFunctionTests.java @@ -14,21 +14,13 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLBoolean; -import static org.testng.Assert.assertEquals; import static graphql.annotations.processor.typeFunctions.DefaultTypeFunctionTestHelper.testedDefaultTypeFunction; +import static org.testng.Assert.assertEquals; public class BooleanFunctionTests { - - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_booleanType_returnsGraphQLBoolean() { DefaultTypeFunction instance = testedDefaultTypeFunction(); diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/ByteFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/ByteFunctionTests.java index 101c56d4..4a733331 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/ByteFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/ByteFunctionTests.java @@ -14,8 +14,6 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLByte; @@ -23,11 +21,6 @@ import static org.testng.Assert.assertEquals; public class ByteFunctionTests { - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_byteType_returnsGraphQLByte() { //arrange diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/CharFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/CharFunctionTests.java index 08664d46..7d057037 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/CharFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/CharFunctionTests.java @@ -14,8 +14,6 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLChar; @@ -23,11 +21,6 @@ import static org.testng.Assert.assertEquals; public class CharFunctionTests { - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_characterType_returnsGraphQLChar() { //arrange diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/DefaultTypeFunctionTest.java b/src/test/java/graphql/annotations/processor/typeFunctions/DefaultTypeFunctionTest.java index e27a6a53..5f9ac57d 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/DefaultTypeFunctionTest.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/DefaultTypeFunctionTest.java @@ -21,8 +21,6 @@ import graphql.annotations.processor.GraphQLAnnotations; import graphql.annotations.processor.ProcessingElementsContainer; import graphql.schema.*; -import graphql.schema.GraphQLType; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.util.*; @@ -33,12 +31,6 @@ import static org.testng.Assert.*; public class DefaultTypeFunctionTest { - - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - private enum A { @GraphQLName("someA") @GraphQLDescription("a") A, B } @@ -67,10 +59,10 @@ public void enumeration() { List values = ((GraphQLEnumType) enumeration).getValues(); assertEquals(values.stream(). map(GraphQLEnumValueDefinition::getName).collect(Collectors.toList()), - Arrays.asList("someA", "B")); + Arrays.asList("B", "someA")); assertEquals(values.stream(). map(GraphQLEnumValueDefinition::getDescription).collect(Collectors.toList()), - Arrays.asList("a", "B")); + Arrays.asList("B", "a")); } @@ -199,7 +191,6 @@ public void recursiveTypes() throws Exception { assertNotNull(class1class2); assertTrue(((GraphQLObjectType) class1class2.getType()).getFieldDefinition("class1").getType() instanceof GraphQLTypeReference); assertTrue(((GraphQLObjectType) class1class2.getType()).getFieldDefinition("class2").getType() instanceof GraphQLTypeReference); - GraphQLAnnotations.instance = new GraphQLAnnotations(); } private ProcessingElementsContainer testedProcessingElementsContainer() { diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/FloatFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/FloatFunctionTests.java index 2a419ad6..ca32cc97 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/FloatFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/FloatFunctionTests.java @@ -14,21 +14,13 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLFloat; -import static org.testng.Assert.assertEquals; import static graphql.annotations.processor.typeFunctions.DefaultTypeFunctionTestHelper.testedDefaultTypeFunction; +import static org.testng.Assert.assertEquals; public class FloatFunctionTests { - - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_floatType_returnsGraphQLFloat() { DefaultTypeFunction instance = testedDefaultTypeFunction(); diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/IDFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/IDFunctionTests.java index 659f6cdd..09e707e3 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/IDFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/IDFunctionTests.java @@ -14,26 +14,19 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; import graphql.annotations.annotationTypes.GraphQLID; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.lang.reflect.Field; import java.lang.reflect.Method; import static graphql.Scalars.GraphQLID; -import static org.testng.Assert.assertEquals; import static graphql.annotations.processor.typeFunctions.DefaultTypeFunctionTestHelper.testedDefaultTypeFunction; +import static org.testng.Assert.assertEquals; public class IDFunctionTests { - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - public @graphql.annotations.annotationTypes.GraphQLID String idStringMethod() { return "asd"; } @@ -57,7 +50,7 @@ public void buildType_stringMethodAnnotatedWithGraphQLID_returnsGraphQLID() thro Method idStringMethod = IDFunctionTests.class.getMethod("idStringMethod"); // Act+Assert - assertEquals(instance.buildType(idStringMethod.getReturnType(), idStringMethod.getAnnotatedReturnType(),null), GraphQLID); + assertEquals(instance.buildType(idStringMethod.getReturnType(), idStringMethod.getAnnotatedReturnType(), null), GraphQLID); } @Test @@ -67,7 +60,7 @@ public void buildType_integerMethodAnnotatedWithGraphQLID_returnsGraphQLID() thr Method idIntegerMethod = IDFunctionTests.class.getMethod("idIntegerMethod"); // Act+Assert - assertEquals(instance.buildType(idIntegerMethod.getReturnType(), idIntegerMethod.getAnnotatedReturnType(),null), GraphQLID); + assertEquals(instance.buildType(idIntegerMethod.getReturnType(), idIntegerMethod.getAnnotatedReturnType(), null), GraphQLID); } @Test @@ -77,7 +70,7 @@ public void buildType_intMethodAnnotatedWithGraphQLID_returnsGraphQLID() throws Method idIntMethod = IDFunctionTests.class.getMethod("idIntMethod"); // Act+Assert - assertEquals(instance.buildType(idIntMethod.getReturnType(), idIntMethod.getAnnotatedReturnType(),null), GraphQLID); + assertEquals(instance.buildType(idIntMethod.getReturnType(), idIntMethod.getAnnotatedReturnType(), null), GraphQLID); } @Test @@ -87,7 +80,7 @@ public void buildType_stringFieldAnnotatedWithGraphQLID_returnsGraphQLID() throw Field idStringField = IDFunctionTests.class.getField("idStringField"); // Act+Assert - assertEquals(instance.buildType(idStringField.getType(), idStringField.getAnnotatedType(),null), GraphQLID); + assertEquals(instance.buildType(idStringField.getType(), idStringField.getAnnotatedType(), null), GraphQLID); } @Test @@ -97,7 +90,7 @@ public void buildType_integerFieldAnnotatedWithGraphQLID_returnsGraphQLID() thro Field idIntegerField = IDFunctionTests.class.getField("idIntegerField"); // Act+Assert - assertEquals(instance.buildType(idIntegerField.getType(), idIntegerField.getAnnotatedType(),null), GraphQLID); + assertEquals(instance.buildType(idIntegerField.getType(), idIntegerField.getAnnotatedType(), null), GraphQLID); } @Test @@ -107,7 +100,7 @@ public void buildType_intFieldAnnotatedWithGraphQLID_returnsGraphQLID() throws N Field idIntField = IDFunctionTests.class.getField("idIntField"); // Act+Assert - assertEquals(instance.buildType(idIntField.getType(), idIntField.getAnnotatedType(),null), GraphQLID); + assertEquals(instance.buildType(idIntField.getType(), idIntField.getAnnotatedType(), null), GraphQLID); } diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/IntegerFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/IntegerFunctionTests.java index 50cfeb04..6c90cc89 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/IntegerFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/IntegerFunctionTests.java @@ -14,21 +14,13 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLInt; -import static org.testng.Assert.assertEquals; import static graphql.annotations.processor.typeFunctions.DefaultTypeFunctionTestHelper.testedDefaultTypeFunction; +import static org.testng.Assert.assertEquals; public class IntegerFunctionTests { - - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_integerType_returnsGraphQLInt() { DefaultTypeFunction instance = testedDefaultTypeFunction(); diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/LongFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/LongFunctionTests.java index 9468a93b..a06822e1 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/LongFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/LongFunctionTests.java @@ -15,21 +15,13 @@ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLLong; -import static org.testng.Assert.assertEquals; import static graphql.annotations.processor.typeFunctions.DefaultTypeFunctionTestHelper.testedDefaultTypeFunction; +import static org.testng.Assert.assertEquals; public class LongFunctionTests { - - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_longType_returnsGraphQLLong() { DefaultTypeFunction instance = testedDefaultTypeFunction(); diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/ShortFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/ShortFunctionTests.java index e62efd59..9e38b59f 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/ShortFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/ShortFunctionTests.java @@ -14,8 +14,6 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLShort; @@ -23,11 +21,6 @@ import static org.testng.Assert.assertEquals; public class ShortFunctionTests { - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_shortType_returnsGraphQLShort() { //arrange diff --git a/src/test/java/graphql/annotations/processor/typeFunctions/StringFunctionTests.java b/src/test/java/graphql/annotations/processor/typeFunctions/StringFunctionTests.java index 76b4ca2b..d83ac10c 100644 --- a/src/test/java/graphql/annotations/processor/typeFunctions/StringFunctionTests.java +++ b/src/test/java/graphql/annotations/processor/typeFunctions/StringFunctionTests.java @@ -14,21 +14,13 @@ */ package graphql.annotations.processor.typeFunctions; -import graphql.annotations.processor.GraphQLAnnotations; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static graphql.Scalars.GraphQLString; -import static org.testng.Assert.assertEquals; import static graphql.annotations.processor.typeFunctions.DefaultTypeFunctionTestHelper.testedDefaultTypeFunction; +import static org.testng.Assert.assertEquals; public class StringFunctionTests { - - @BeforeMethod - public void init() { - GraphQLAnnotations.getInstance().getTypeRegistry().clear(); - } - @Test public void buildType_stringType_returnsGraphQLString() { DefaultTypeFunction instance = testedDefaultTypeFunction();