1
1
package graphql .schema .idl ;
2
2
3
3
import graphql .Internal ;
4
- import graphql .Scalars ;
5
4
import graphql .introspection .Introspection .DirectiveLocation ;
6
5
import graphql .language .Argument ;
7
6
import graphql .language .ArrayValue ;
8
- import graphql .language .BooleanValue ;
9
7
import graphql .language .Comment ;
10
8
import graphql .language .Description ;
11
9
import graphql .language .Directive ;
12
10
import graphql .language .DirectiveDefinition ;
13
11
import graphql .language .EnumValue ;
14
- import graphql .language .FloatValue ;
15
12
import graphql .language .InputValueDefinition ;
16
- import graphql .language .IntValue ;
17
13
import graphql .language .Node ;
18
14
import graphql .language .NullValue ;
19
15
import graphql .language .ObjectValue ;
39
35
import java .util .Optional ;
40
36
import java .util .Set ;
41
37
import java .util .function .Function ;
42
- import java .util .stream .Collectors ;
43
38
44
39
import static graphql .Assert .assertShouldNeverHappen ;
45
- import static graphql .Assert .assertTrue ;
46
40
import static graphql .introspection .Introspection .DirectiveLocation .ENUM_VALUE ;
47
41
import static graphql .introspection .Introspection .DirectiveLocation .FIELD_DEFINITION ;
48
42
import static graphql .introspection .Introspection .DirectiveLocation .valueOf ;
49
43
import static graphql .language .DirectiveLocation .newDirectiveLocation ;
50
- import static graphql .schema .GraphQLList .list ;
51
44
import static graphql .schema .GraphQLTypeUtil .isList ;
52
45
import static graphql .schema .GraphQLTypeUtil .simplePrint ;
53
46
import static graphql .schema .GraphQLTypeUtil .unwrapOne ;
@@ -173,74 +166,10 @@ public void addDeprecatedDirectiveDefinition(TypeDefinitionRegistry typeRegistry
173
166
typeRegistry .add (DEPRECATED_DIRECTIVE_DEFINITION );
174
167
}
175
168
176
- /**
177
- * We support the basic types as directive types
178
- *
179
- * @param value the value to use
180
- *
181
- * @return a graphql input type
182
- */
183
- public GraphQLInputType buildDirectiveInputType (Value value ) {
184
- if (value instanceof NullValue ) {
185
- return Scalars .GraphQLString ;
186
- }
187
- if (value instanceof FloatValue ) {
188
- return Scalars .GraphQLFloat ;
189
- }
190
- if (value instanceof StringValue ) {
191
- return Scalars .GraphQLString ;
192
- }
193
- if (value instanceof IntValue ) {
194
- return Scalars .GraphQLInt ;
195
- }
196
- if (value instanceof BooleanValue ) {
197
- return Scalars .GraphQLBoolean ;
198
- }
199
- if (value instanceof ArrayValue ) {
200
- ArrayValue arrayValue = (ArrayValue ) value ;
201
- return list (buildDirectiveInputType (getArrayValueWrappedType (arrayValue )));
202
- }
203
- return assertShouldNeverHappen ("Directive values of type '%s' are not supported yet" , value .getClass ().getSimpleName ());
204
- }
205
-
206
- private Value getArrayValueWrappedType (ArrayValue value ) {
207
- // empty array [] is equivalent to [null]
208
- if (value .getValues ().isEmpty ()) {
209
- return NullValue .Null ;
210
- }
211
-
212
- // get rid of null values
213
- List <Value > nonNullValueList = value .getValues ().stream ()
214
- .filter (v -> !(v instanceof NullValue ))
215
- .collect (toList ());
216
-
217
- // [null, null, ...] unwrapped is null
218
- if (nonNullValueList .isEmpty ()) {
219
- return NullValue .Null ;
220
- }
221
-
222
- // make sure the array isn't polymorphic
223
- Set <Class <? extends Value >> distinctTypes = nonNullValueList .stream ()
224
- .map (Value ::getClass )
225
- .distinct ()
226
- .collect (Collectors .toSet ());
227
-
228
- assertTrue (distinctTypes .size () == 1 ,
229
- "Arrays containing multiple types of values are not supported yet. Detected the following types [%s]" ,
230
- nonNullValueList .stream ()
231
- .map (Value ::getClass )
232
- .map (Class ::getSimpleName )
233
- .distinct ()
234
- .sorted ()
235
- .collect (Collectors .joining ("," )));
236
-
237
- // peek at first value, value exists and is assured to be non-null
238
- return nonNullValueList .get (0 );
239
- }
240
169
241
170
// builds directives from a type and its extensions
242
171
public GraphQLDirective buildDirective (Directive directive , Set <GraphQLDirective > directiveDefinitions , DirectiveLocation directiveLocation , GraphqlTypeComparatorRegistry comparatorRegistry ) {
243
- Optional < GraphQLDirective > directiveDefinition = directiveDefinitions . stream (). filter ( dd -> dd .getName ().equals (directive .getName ())).findFirst ();
172
+ GraphQLDirective directiveDefinition = FpKit . findOne ( directiveDefinitions , dd -> dd .getName ().equals (directive .getName ())).get ();
244
173
GraphQLDirective .Builder builder = GraphQLDirective .newDirective ()
245
174
.name (directive .getName ())
246
175
.description (buildDescription (directive , null ))
@@ -251,26 +180,20 @@ public GraphQLDirective buildDirective(Directive directive, Set<GraphQLDirective
251
180
.map (arg -> buildDirectiveArgument (arg , directiveDefinition ))
252
181
.collect (toList ());
253
182
254
- if (directiveDefinition .isPresent ()) {
255
- arguments = transferMissingArguments (arguments , directiveDefinition .get ());
256
- }
183
+ arguments = transferMissingArguments (arguments , directiveDefinition );
257
184
arguments .forEach (builder ::argument );
258
185
259
186
return builder .build ();
260
187
}
261
188
262
- private GraphQLArgument buildDirectiveArgument (Argument arg , Optional < GraphQLDirective > directiveDefinition ) {
263
- Optional < GraphQLArgument > directiveDefArgument = directiveDefinition .map ( dd -> dd . getArgument (arg .getName () ));
189
+ private GraphQLArgument buildDirectiveArgument (Argument arg , GraphQLDirective directiveDefinition ) {
190
+ GraphQLArgument directiveDefArgument = directiveDefinition .getArgument (arg .getName ());
264
191
GraphQLArgument .Builder builder = GraphQLArgument .newArgument ();
265
192
builder .name (arg .getName ());
266
193
GraphQLInputType inputType ;
267
194
Object defaultValue = null ;
268
- if (directiveDefArgument .isPresent ()) {
269
- inputType = directiveDefArgument .get ().getType ();
270
- defaultValue = directiveDefArgument .get ().getDefaultValue ();
271
- } else {
272
- inputType = buildDirectiveInputType (arg .getValue ());
273
- }
195
+ inputType = directiveDefArgument .getType ();
196
+ defaultValue = directiveDefArgument .getDefaultValue ();
274
197
builder .type (inputType );
275
198
builder .defaultValue (defaultValue );
276
199
0 commit comments