From 9a99fd4c6cd37d989528b83c3b4f9e1f94c76302 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 19 Apr 2021 20:00:40 -0400 Subject: [PATCH] Improving the review example based on https://github.com/apollographql/federation-jvm/pull/103 --- .../java/com/example/demo/ReviewsApp.java | 1 + .../demo/federation/FederatedSchema.java | 15 ++--- .../GraphQLJavaToolsConfiguration.java | 62 +++++++++++++++++++ .../demo/query/ReviewsQueryResolver.java | 8 --- .../src/main/resources/schema/schema.graphqls | 5 +- 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 reviews/src/main/java/com/example/demo/federation/GraphQLJavaToolsConfiguration.java diff --git a/reviews/src/main/java/com/example/demo/ReviewsApp.java b/reviews/src/main/java/com/example/demo/ReviewsApp.java index 55fa596..70e525b 100644 --- a/reviews/src/main/java/com/example/demo/ReviewsApp.java +++ b/reviews/src/main/java/com/example/demo/ReviewsApp.java @@ -9,4 +9,5 @@ public class ReviewsApp { public static void main(String[] args) { SpringApplication.run(ReviewsApp.class, args); } + } \ No newline at end of file diff --git a/reviews/src/main/java/com/example/demo/federation/FederatedSchema.java b/reviews/src/main/java/com/example/demo/federation/FederatedSchema.java index 1c26e5b..17a1649 100644 --- a/reviews/src/main/java/com/example/demo/federation/FederatedSchema.java +++ b/reviews/src/main/java/com/example/demo/federation/FederatedSchema.java @@ -1,27 +1,24 @@ package com.example.demo.federation; -import com.apollographql.federation.graphqljava.Federation; import com.apollographql.federation.graphqljava._Entity; import com.example.demo.model.Show; -import graphql.kickstart.tools.SchemaParser; import graphql.schema.GraphQLSchema; +import com.apollographql.federation.graphqljava.SchemaTransformer; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; -import org.springframework.core.io.Resource; -import org.springframework.stereotype.Component; +import org.springframework.context.annotation.Configuration; -@Component +@Configuration public class FederatedSchema { @Bean - public GraphQLSchema federatedGraphQLSchema(SchemaParser schemaParser) + public GraphQLSchema federatedGraphQLSchema(SchemaTransformer schemaTransformer) throws IOException { - GraphQLSchema federatedSchema = Federation.transform(schemaParser.makeExecutableSchema(), true) + return schemaTransformer .fetchEntities(env -> env.>>getArgument(_Entity.argumentName) .stream() .map(reference -> { @@ -39,8 +36,6 @@ public GraphQLSchema federatedGraphQLSchema(SchemaParser schemaParser) return null; }) .build(); - - return federatedSchema; } } diff --git a/reviews/src/main/java/com/example/demo/federation/GraphQLJavaToolsConfiguration.java b/reviews/src/main/java/com/example/demo/federation/GraphQLJavaToolsConfiguration.java new file mode 100644 index 0000000..b486446 --- /dev/null +++ b/reviews/src/main/java/com/example/demo/federation/GraphQLJavaToolsConfiguration.java @@ -0,0 +1,62 @@ +package com.example.demo.federation; + +import com.apollographql.federation.graphqljava.Federation; +import com.apollographql.federation.graphqljava.SchemaTransformer; +import com.example.demo.model.Show; +import graphql.Scalars; +import graphql.kickstart.tools.SchemaObjects; +import graphql.kickstart.tools.SchemaParser; +import graphql.kickstart.tools.SchemaParserDictionary; +import graphql.kickstart.tools.SchemaParserOptions; +import graphql.schema.GraphQLFieldDefinition; +import graphql.schema.GraphQLObjectType; +import graphql.schema.GraphQLSchema; +import org.jetbrains.annotations.NotNull; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class GraphQLJavaToolsConfiguration { + + @Bean + public SchemaParserDictionary schemaParserDictionary() { + return new SchemaParserDictionary().add("Show", Show.class); + } + + @Bean + public BeanPostProcessor schemaParserOptionsBuilderPostProcessor() { + return new BeanPostProcessor() { + @Override + public Object postProcessAfterInitialization(@NotNull Object bean, @NotNull String beanName) throws BeansException { + return bean instanceof SchemaParserOptions.Builder + ? ((SchemaParserOptions.Builder) bean).includeUnusedTypes(true) : bean; + } + }; + } + + + @Bean + public SchemaTransformer schemaTransformer(SchemaParser schemaParser) { + final SchemaObjects schemaObjects = schemaParser.parseSchemaObjects(); + final boolean queryTypeIsEmpty = schemaObjects.getQuery().getFieldDefinitions().isEmpty(); + final GraphQLObjectType newQuery = queryTypeIsEmpty + ? schemaObjects.getQuery().transform(graphQLObjectTypeBuilder -> + graphQLObjectTypeBuilder.field(GraphQLFieldDefinition.newFieldDefinition() + .name("_dummy") + .type(Scalars.GraphQLString) + .build() + ) + ) + : schemaObjects.getQuery(); + final GraphQLSchema graphQLSchema = GraphQLSchema.newSchema() + .query(newQuery) + .mutation(schemaObjects.getMutation()) + .subscription(schemaObjects.getSubscription()) + .additionalTypes(schemaObjects.getDictionary()) + .codeRegistry(schemaObjects.getCodeRegistryBuilder().build()) + .build(); + return Federation.transform(graphQLSchema, queryTypeIsEmpty); + } +} \ No newline at end of file diff --git a/reviews/src/main/java/com/example/demo/query/ReviewsQueryResolver.java b/reviews/src/main/java/com/example/demo/query/ReviewsQueryResolver.java index c48288e..7bb3e47 100644 --- a/reviews/src/main/java/com/example/demo/query/ReviewsQueryResolver.java +++ b/reviews/src/main/java/com/example/demo/query/ReviewsQueryResolver.java @@ -11,12 +11,4 @@ @Component public class ReviewsQueryResolver implements GraphQLQueryResolver { - @Autowired - ReviewService reviewService; - - public Show get_workaround() { - throw new UnknownOperationException("Federation workaround - You need to reference types otherwise by default " - + "graphql-java-tools will remove them from the executable schema. This in turn causes " - + "Federation.transform to not build out the _entities query"); - } } diff --git a/reviews/src/main/resources/schema/schema.graphqls b/reviews/src/main/resources/schema/schema.graphqls index ea17cc5..1d4c366 100644 --- a/reviews/src/main/resources/schema/schema.graphqls +++ b/reviews/src/main/resources/schema/schema.graphqls @@ -7,7 +7,4 @@ type Review { starRating: Int } -type Query { - "In order for the FederatedSchema.java to build the _entities query, you must reference the federatedType within a root node" - _workaround : Show @deprecated(reason: "This is a workaround") -} \ No newline at end of file +type Query \ No newline at end of file