|
| 1 | +package com.introproventures.graphql.jpa.query.autoconfigure; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import graphql.GraphQL; |
| 8 | +import graphql.Scalars; |
| 9 | +import graphql.schema.GraphQLFieldDefinition; |
| 10 | +import graphql.schema.GraphQLObjectType; |
| 11 | +import graphql.schema.GraphQLSchema; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 18 | +import org.springframework.stereotype.Component; |
| 19 | +import org.springframework.test.context.junit4.SpringRunner; |
| 20 | + |
| 21 | +@RunWith(SpringRunner.class) |
| 22 | +@SpringBootTest(webEnvironment=WebEnvironment.NONE) |
| 23 | +public class GraphQLSchemaAutoConfigurationTest { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private GraphQLSchema graphQLSchema; |
| 27 | + |
| 28 | + @SpringBootApplication |
| 29 | + static class Application { |
| 30 | + |
| 31 | + @Component |
| 32 | + static class MutationGraphQLSchemaConfigurer implements GraphQLSchemaConfigurer { |
| 33 | + |
| 34 | + @Override |
| 35 | + public void configure(GraphQLShemaRegistration registry) { |
| 36 | + GraphQLObjectType mutation = GraphQLObjectType.newObject() |
| 37 | + .name("mutation") |
| 38 | + .field(GraphQLFieldDefinition.newFieldDefinition() |
| 39 | + .name("greet") |
| 40 | + .type(Scalars.GraphQLString) |
| 41 | + .dataFetcher(environment -> { |
| 42 | + return "hello world"; |
| 43 | + })) |
| 44 | + .build(); |
| 45 | + |
| 46 | + GraphQLSchema graphQLSchema = GraphQLSchema.newSchema() |
| 47 | + .query(GraphQLObjectType.newObject().name("null")) |
| 48 | + .mutation(mutation) |
| 49 | + .build(); |
| 50 | + |
| 51 | + registry.register(graphQLSchema); |
| 52 | + } |
| 53 | + } |
| 54 | + @Component |
| 55 | + static class QueryGraphQLSchemaConfigurer implements GraphQLSchemaConfigurer { |
| 56 | + |
| 57 | + @Override |
| 58 | + public void configure(GraphQLShemaRegistration registry) { |
| 59 | + GraphQLObjectType query = GraphQLObjectType.newObject() |
| 60 | + .name("query") |
| 61 | + .field(GraphQLFieldDefinition.newFieldDefinition() |
| 62 | + .name("hello") |
| 63 | + .type(Scalars.GraphQLString) |
| 64 | + .dataFetcher(environment -> { |
| 65 | + return "world"; |
| 66 | + })) |
| 67 | + .build(); |
| 68 | + |
| 69 | + GraphQLSchema graphQLSchema = GraphQLSchema.newSchema() |
| 70 | + .query(query) |
| 71 | + .build(); |
| 72 | + |
| 73 | + registry.register(graphQLSchema); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void contextLoads() { |
| 80 | + // given |
| 81 | + GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); |
| 82 | + |
| 83 | + // when |
| 84 | + Map<String, Object> result = graphQL.execute("query {hello}").getData(); |
| 85 | + Map<String, Object> result2 = graphQL.execute("mutation {greet}").getData(); |
| 86 | + |
| 87 | + // then |
| 88 | + assertThat(result.toString()).isEqualTo("{hello=world}"); |
| 89 | + assertThat(result2.toString()).isEqualTo("{greet=hello world}"); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +} |
0 commit comments