|
1 | 1 | package graphql.schema.idl; |
2 | 2 |
|
| 3 | +import graphql.Assert; |
| 4 | +import graphql.GraphQLContext; |
3 | 5 | import graphql.PublicApi; |
| 6 | +import graphql.execution.CoercedVariables; |
| 7 | +import graphql.language.ArrayValue; |
| 8 | +import graphql.language.BooleanValue; |
| 9 | +import graphql.language.EnumValue; |
| 10 | +import graphql.language.FloatValue; |
| 11 | +import graphql.language.IntValue; |
| 12 | +import graphql.language.NullValue; |
| 13 | +import graphql.language.ObjectField; |
| 14 | +import graphql.language.ObjectValue; |
| 15 | +import graphql.language.StringValue; |
| 16 | +import graphql.language.Value; |
| 17 | +import graphql.language.VariableReference; |
4 | 18 | import graphql.schema.Coercing; |
5 | 19 | import graphql.schema.CoercingParseLiteralException; |
6 | 20 | import graphql.schema.CoercingParseValueException; |
|
9 | 23 | import graphql.schema.GraphQLScalarType; |
10 | 24 | import graphql.schema.SingletonPropertyDataFetcher; |
11 | 25 | import graphql.schema.TypeResolver; |
| 26 | +import org.jspecify.annotations.NonNull; |
| 27 | +import org.jspecify.annotations.NullMarked; |
| 28 | +import org.jspecify.annotations.Nullable; |
12 | 29 |
|
| 30 | +import java.util.LinkedHashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Locale; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +/** |
| 37 | + * This is a {@link WiringFactory} which provides mocked types resolver |
| 38 | + * and scalars. It is useful for testing only, for example for creating schemas |
| 39 | + * that can be inspected but not executed on. |
| 40 | + * <p> |
| 41 | + * See {@link RuntimeWiring#MOCKED_WIRING} for example usage |
| 42 | + */ |
13 | 43 | @PublicApi |
| 44 | +@NullMarked |
| 45 | +@SuppressWarnings("rawtypes") |
14 | 46 | public class MockedWiringFactory implements WiringFactory { |
15 | 47 |
|
16 | 48 | @Override |
@@ -43,34 +75,79 @@ public boolean providesDataFetcher(FieldWiringEnvironment environment) { |
43 | 75 | } |
44 | 76 |
|
45 | 77 | @Override |
46 | | - public DataFetcher getDataFetcher(FieldWiringEnvironment environment) { |
| 78 | + public DataFetcher<?> getDataFetcher(FieldWiringEnvironment environment) { |
47 | 79 | return SingletonPropertyDataFetcher.singleton(); |
48 | 80 | } |
49 | 81 |
|
50 | 82 | @Override |
51 | 83 | public boolean providesScalar(ScalarWiringEnvironment environment) { |
52 | | - if (ScalarInfo.isGraphqlSpecifiedScalar(environment.getScalarTypeDefinition().getName())) { |
53 | | - return false; |
54 | | - } |
55 | | - return true; |
| 84 | + return !ScalarInfo.isGraphqlSpecifiedScalar(environment.getScalarTypeDefinition().getName()); |
56 | 85 | } |
57 | 86 |
|
58 | 87 | public GraphQLScalarType getScalar(ScalarWiringEnvironment environment) { |
59 | | - return GraphQLScalarType.newScalar().name(environment.getScalarTypeDefinition().getName()).coercing(new Coercing() { |
| 88 | + return GraphQLScalarType.newScalar().name(environment.getScalarTypeDefinition().getName()).coercing(new Coercing<>() { |
| 89 | + @Nullable |
60 | 90 | @Override |
61 | | - public Object serialize(Object dataFetcherResult) throws CoercingSerializeException { |
| 91 | + public Object serialize(@NonNull Object dataFetcherResult, @NonNull GraphQLContext graphQLContext, @NonNull Locale locale) throws CoercingSerializeException { |
62 | 92 | throw new UnsupportedOperationException("Not implemented...this is only a mocked wiring"); |
63 | 93 | } |
64 | 94 |
|
| 95 | + @Nullable |
65 | 96 | @Override |
66 | | - public Object parseValue(Object input) throws CoercingParseValueException { |
| 97 | + public Object parseValue(@NonNull Object input, @NonNull GraphQLContext graphQLContext, @NonNull Locale locale) throws CoercingParseValueException { |
67 | 98 | throw new UnsupportedOperationException("Not implemented...this is only a mocked wiring"); |
68 | 99 | } |
69 | 100 |
|
| 101 | + @Nullable |
70 | 102 | @Override |
71 | | - public Object parseLiteral(Object input) throws CoercingParseLiteralException { |
72 | | - throw new UnsupportedOperationException("Not implemented...this is only a mocked wiring"); |
| 103 | + public Object parseLiteral(@NonNull Value input, @NonNull CoercedVariables variables, @NonNull GraphQLContext graphQLContext, @NonNull Locale locale) throws CoercingParseLiteralException { |
| 104 | + return parseLiteralImpl(input, variables, graphQLContext, locale); |
73 | 105 | } |
| 106 | + |
| 107 | + @Nullable |
| 108 | + private Object parseLiteralImpl(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) { |
| 109 | + if (input instanceof NullValue) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + if (input instanceof FloatValue) { |
| 113 | + return ((FloatValue) input).getValue(); |
| 114 | + } |
| 115 | + if (input instanceof StringValue) { |
| 116 | + return ((StringValue) input).getValue(); |
| 117 | + } |
| 118 | + if (input instanceof IntValue) { |
| 119 | + return ((IntValue) input).getValue(); |
| 120 | + } |
| 121 | + if (input instanceof BooleanValue) { |
| 122 | + return ((BooleanValue) input).isValue(); |
| 123 | + } |
| 124 | + if (input instanceof EnumValue) { |
| 125 | + return ((EnumValue) input).getName(); |
| 126 | + } |
| 127 | + if (input instanceof VariableReference) { |
| 128 | + String varName = ((VariableReference) input).getName(); |
| 129 | + return variables.get(varName); |
| 130 | + } |
| 131 | + if (input instanceof ArrayValue) { |
| 132 | + List<Value> values = ((ArrayValue) input).getValues(); |
| 133 | + return values.stream() |
| 134 | + .map(v -> parseLiteral(v, variables, graphQLContext, locale)) |
| 135 | + .collect(Collectors.toList()); |
| 136 | + } |
| 137 | + if (input instanceof ObjectValue) { |
| 138 | + List<ObjectField> values = ((ObjectValue) input).getObjectFields(); |
| 139 | + Map<String, Object> parsedValues = new LinkedHashMap<>(); |
| 140 | + values.forEach(fld -> { |
| 141 | + Object parsedValue = parseLiteral(fld.getValue(), variables, graphQLContext, locale); |
| 142 | + if (parsedValue != null) { |
| 143 | + parsedValues.put(fld.getName(), parsedValue); |
| 144 | + } |
| 145 | + }); |
| 146 | + return parsedValues; |
| 147 | + } |
| 148 | + return Assert.assertShouldNeverHappen("We have covered all Value types"); |
| 149 | + } |
| 150 | + |
74 | 151 | }).build(); |
75 | 152 | } |
76 | 153 | } |
0 commit comments