|
1 | 1 | package com.oembedler.moon.graphql.boot; |
2 | 2 |
|
3 | | -import com.coxautodev.graphql.tools.*; |
| 3 | +import com.coxautodev.graphql.tools.GraphQLResolver; |
| 4 | +import com.coxautodev.graphql.tools.PerFieldObjectMapperProvider; |
| 5 | +import com.coxautodev.graphql.tools.SchemaParser; |
| 6 | +import com.coxautodev.graphql.tools.SchemaParserBuilder; |
| 7 | +import com.coxautodev.graphql.tools.SchemaParserDictionary; |
| 8 | +import com.coxautodev.graphql.tools.SchemaParserOptions; |
| 9 | +import com.coxautodev.graphql.tools.TypeDefinitionFactory; |
4 | 10 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 11 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; |
6 | 12 | import com.fasterxml.jackson.module.kotlin.KotlinModule; |
7 | 13 | import graphql.schema.GraphQLScalarType; |
8 | 14 | import graphql.schema.GraphQLSchema; |
| 15 | +import graphql.schema.idl.SchemaDirectiveWiring; |
9 | 16 | import graphql.servlet.config.GraphQLSchemaProvider; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.List; |
10 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
11 | 20 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
12 | 21 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
|
19 | 28 | import org.springframework.context.annotation.Bean; |
20 | 29 | import org.springframework.context.annotation.Configuration; |
21 | 30 |
|
22 | | -import java.io.IOException; |
23 | | -import java.util.List; |
24 | | - |
25 | 31 | /** |
26 | 32 | * @author Andrew Potter |
27 | 33 | */ |
|
31 | 37 | @EnableConfigurationProperties(GraphQLToolsProperties.class) |
32 | 38 | public class GraphQLJavaToolsAutoConfiguration { |
33 | 39 |
|
34 | | - @Autowired(required = false) |
35 | | - private SchemaParserDictionary dictionary; |
| 40 | + @Autowired(required = false) |
| 41 | + private SchemaParserDictionary dictionary; |
36 | 42 |
|
37 | | - @Autowired(required = false) |
38 | | - private GraphQLScalarType[] scalars; |
| 43 | + @Autowired(required = false) |
| 44 | + private GraphQLScalarType[] scalars; |
39 | 45 |
|
40 | | - @Autowired(required = false) |
41 | | - private List<SchemaDirective> directives; |
| 46 | + @Autowired(required = false) |
| 47 | + private List<SchemaDirective> directives; |
42 | 48 |
|
43 | | - @Autowired(required = false) |
44 | | - private List<TypeDefinitionFactory> typeDefinitionFactories; |
| 49 | + @Autowired(required = false) |
| 50 | + private List<SchemaDirectiveWiring> directiveWirings; |
45 | 51 |
|
| 52 | + @Autowired(required = false) |
| 53 | + private List<TypeDefinitionFactory> typeDefinitionFactories; |
46 | 54 |
|
47 | | - @Autowired |
48 | | - private GraphQLToolsProperties props; |
| 55 | + @Autowired |
| 56 | + private GraphQLToolsProperties props; |
49 | 57 |
|
50 | | - @Bean |
51 | | - @ConditionalOnMissingBean |
52 | | - public SchemaStringProvider schemaStringProvider() { |
53 | | - return new ClasspathResourceSchemaStringProvider(props.getSchemaLocationPattern()); |
54 | | - } |
| 58 | + @Bean |
| 59 | + @ConditionalOnMissingBean |
| 60 | + public SchemaStringProvider schemaStringProvider() { |
| 61 | + return new ClasspathResourceSchemaStringProvider(props.getSchemaLocationPattern()); |
| 62 | + } |
55 | 63 |
|
56 | | - @Bean |
57 | | - @ConditionalOnMissingBean |
58 | | - @ConfigurationProperties("graphql.tools.schema-parser-options") |
59 | | - public SchemaParserOptions.Builder optionsBuilder( |
60 | | - PerFieldObjectMapperProvider perFieldObjectMapperProvider |
61 | | - ) { |
62 | | - SchemaParserOptions.Builder optionsBuilder = SchemaParserOptions.newOptions(); |
| 64 | + @Bean |
| 65 | + @ConditionalOnMissingBean |
| 66 | + @ConfigurationProperties("graphql.tools.schema-parser-options") |
| 67 | + public SchemaParserOptions.Builder optionsBuilder( |
| 68 | + PerFieldObjectMapperProvider perFieldObjectMapperProvider |
| 69 | + ) { |
| 70 | + SchemaParserOptions.Builder optionsBuilder = SchemaParserOptions.newOptions(); |
63 | 71 |
|
64 | | - if (perFieldObjectMapperProvider != null) { |
65 | | - optionsBuilder.objectMapperProvider(perFieldObjectMapperProvider); |
66 | | - } |
67 | | - optionsBuilder.introspectionEnabled(props.isIntrospectionEnabled()); |
68 | | - |
69 | | - if (typeDefinitionFactories != null) { |
70 | | - typeDefinitionFactories.forEach(optionsBuilder::typeDefinitionFactory); |
71 | | - } |
| 72 | + if (perFieldObjectMapperProvider != null) { |
| 73 | + optionsBuilder.objectMapperProvider(perFieldObjectMapperProvider); |
| 74 | + } |
| 75 | + optionsBuilder.introspectionEnabled(props.isIntrospectionEnabled()); |
72 | 76 |
|
73 | | - return optionsBuilder; |
| 77 | + if (typeDefinitionFactories != null) { |
| 78 | + typeDefinitionFactories.forEach(optionsBuilder::typeDefinitionFactory); |
74 | 79 | } |
75 | 80 |
|
76 | | - @Bean |
77 | | - @ConditionalOnBean({GraphQLResolver.class}) |
78 | | - @ConditionalOnMissingBean |
79 | | - public SchemaParser schemaParser( |
80 | | - List<GraphQLResolver<?>> resolvers, |
81 | | - SchemaStringProvider schemaStringProvider, |
82 | | - SchemaParserOptions.Builder optionsBuilder |
83 | | - ) throws IOException { |
84 | | - SchemaParserBuilder builder = dictionary != null ? new SchemaParserBuilder(dictionary) : new SchemaParserBuilder(); |
| 81 | + return optionsBuilder; |
| 82 | + } |
85 | 83 |
|
86 | | - List<String> schemaStrings = schemaStringProvider.schemaStrings(); |
87 | | - schemaStrings.forEach(builder::schemaString); |
| 84 | + @Bean |
| 85 | + @ConditionalOnBean({GraphQLResolver.class}) |
| 86 | + @ConditionalOnMissingBean |
| 87 | + public SchemaParser schemaParser( |
| 88 | + List<GraphQLResolver<?>> resolvers, |
| 89 | + SchemaStringProvider schemaStringProvider, |
| 90 | + SchemaParserOptions.Builder optionsBuilder |
| 91 | + ) throws IOException { |
| 92 | + SchemaParserBuilder builder = dictionary != null ? new SchemaParserBuilder(dictionary) : new SchemaParserBuilder(); |
88 | 93 |
|
89 | | - if (scalars != null) { |
90 | | - builder.scalars(scalars); |
91 | | - } |
| 94 | + List<String> schemaStrings = schemaStringProvider.schemaStrings(); |
| 95 | + schemaStrings.forEach(builder::schemaString); |
92 | 96 |
|
93 | | - builder.options(optionsBuilder.build()); |
| 97 | + if (scalars != null) { |
| 98 | + builder.scalars(scalars); |
| 99 | + } |
94 | 100 |
|
95 | | - if (directives != null) { |
96 | | - directives.forEach(it -> builder.directive(it.getName(), it.getDirective())); |
97 | | - } |
| 101 | + builder.options(optionsBuilder.build()); |
98 | 102 |
|
99 | | - return builder |
100 | | - .resolvers(resolvers) |
101 | | - .build(); |
| 103 | + if (directives != null) { |
| 104 | + directives.forEach(it -> builder.directive(it.getName(), it.getDirective())); |
102 | 105 | } |
103 | 106 |
|
104 | | - @Bean |
105 | | - @ConditionalOnMissingBean |
106 | | - @ConditionalOnProperty(value = "graphql.tools.use-default-objectmapper", havingValue = "true", matchIfMissing = true) |
107 | | - public PerFieldObjectMapperProvider perFieldObjectMapperProvider(ObjectMapper objectMapper) { |
108 | | - objectMapper |
109 | | - .registerModule(new Jdk8Module()) |
110 | | - .registerModule(new KotlinModule()); |
111 | | - return fieldDefinition -> objectMapper; |
| 107 | + if (directiveWirings != null) { |
| 108 | + directiveWirings.forEach(builder::directiveWiring); |
112 | 109 | } |
113 | 110 |
|
114 | | - |
115 | | - @Bean |
116 | | - @ConditionalOnBean(SchemaParser.class) |
117 | | - @ConditionalOnMissingBean({GraphQLSchema.class, GraphQLSchemaProvider.class}) |
118 | | - public GraphQLSchema graphQLSchema(SchemaParser schemaParser) { |
119 | | - return schemaParser.makeExecutableSchema(); |
120 | | - } |
| 111 | + return builder |
| 112 | + .resolvers(resolvers) |
| 113 | + .build(); |
| 114 | + } |
| 115 | + |
| 116 | + @Bean |
| 117 | + @ConditionalOnMissingBean |
| 118 | + @ConditionalOnProperty(value = "graphql.tools.use-default-objectmapper", havingValue = "true", matchIfMissing = true) |
| 119 | + public PerFieldObjectMapperProvider perFieldObjectMapperProvider(ObjectMapper objectMapper) { |
| 120 | + objectMapper |
| 121 | + .registerModule(new Jdk8Module()) |
| 122 | + .registerModule(new KotlinModule()); |
| 123 | + return fieldDefinition -> objectMapper; |
| 124 | + } |
| 125 | + |
| 126 | + @Bean |
| 127 | + @ConditionalOnBean(SchemaParser.class) |
| 128 | + @ConditionalOnMissingBean({GraphQLSchema.class, GraphQLSchemaProvider.class}) |
| 129 | + public GraphQLSchema graphQLSchema(SchemaParser schemaParser) { |
| 130 | + return schemaParser.makeExecutableSchema(); |
| 131 | + } |
121 | 132 | } |
0 commit comments