Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Improving the review example based on apollographql/federation-jvm#103
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wkcorp committed Apr 20, 2021
1 parent 780ebf8 commit 9a99fd4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
1 change: 1 addition & 0 deletions reviews/src/main/java/com/example/demo/ReviewsApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class ReviewsApp {
public static void main(String[] args) {
SpringApplication.run(ReviewsApp.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -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.<List<Map<String, Object>>>getArgument(_Entity.argumentName)
.stream()
.map(reference -> {
Expand All @@ -39,8 +36,6 @@ public GraphQLSchema federatedGraphQLSchema(SchemaParser schemaParser)
return null;
})
.build();

return federatedSchema;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
5 changes: 1 addition & 4 deletions reviews/src/main/resources/schema/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
type Query

0 comments on commit 9a99fd4

Please sign in to comment.