/
_Any.java
76 lines (67 loc) · 2.69 KB
/
_Any.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.apollographql.federation.graphqljava;
import graphql.Assert;
import graphql.language.ArrayValue;
import graphql.language.BooleanValue;
import graphql.language.EnumValue;
import graphql.language.FloatValue;
import graphql.language.IntValue;
import graphql.language.NullValue;
import graphql.language.ObjectField;
import graphql.language.ObjectValue;
import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import org.jetbrains.annotations.Nullable;
import java.util.stream.Collectors;
import static graphql.schema.GraphQLScalarType.newScalar;
public final class _Any {
public static final String typeName = "_Any";
static final Coercing defaultCoercing = new Coercing() {
@Override
public Object serialize(Object dataFetcherResult) throws CoercingSerializeException {
return dataFetcherResult;
}
@Override
public Object parseValue(Object input) throws CoercingParseValueException {
return input;
}
@Nullable
@Override
public Object parseLiteral(Object input) throws CoercingParseLiteralException {
if (input instanceof NullValue) {
return null;
} else if (input instanceof FloatValue) {
return ((FloatValue) input).getValue();
} else if (input instanceof StringValue) {
return ((StringValue) input).getValue();
} else if (input instanceof IntValue) {
return ((IntValue) input).getValue();
} else if (input instanceof BooleanValue) {
return ((BooleanValue) input).isValue();
} else if (input instanceof EnumValue) {
return ((EnumValue) input).getName();
} else if (input instanceof ArrayValue) {
return ((ArrayValue) input).getValues()
.stream()
.map(this::parseLiteral)
.collect(Collectors.toList());
} else if (input instanceof ObjectValue) {
return ((ObjectValue) input).getObjectFields()
.stream()
.collect(Collectors.toMap(ObjectField::getName, f -> parseLiteral(f.getValue())));
}
return Assert.assertShouldNeverHappen();
}
};
private _Any() {
}
static GraphQLScalarType type(Coercing coercing) {
return newScalar()
.name(typeName)
.coercing(coercing)
.build();
}
}