-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathAliasedScalar.java
More file actions
108 lines (94 loc) · 3.49 KB
/
AliasedScalar.java
File metadata and controls
108 lines (94 loc) · 3.49 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package graphql.scalars.alias;
import graphql.Assert;
import graphql.GraphQLContext;
import graphql.Internal;
import graphql.execution.CoercedVariables;
import graphql.language.Value;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import java.util.Locale;
/**
* Access this via {@link graphql.scalars.ExtendedScalars#newAliasedScalar(String)}
*/
@Internal
public final class AliasedScalar {
private AliasedScalar() {
}
/**
* A builder for {@link graphql.scalars.alias.AliasedScalar}
*/
public static class Builder {
private String name;
private String description;
private GraphQLScalarType aliasedScalar;
/**
* Sets the name of the aliased scalar
*
* @param name the name of the aliased scalar
*
* @return this builder
*/
public Builder name(String name) {
this.name = name;
return this;
}
/**
* Sets the description of the aliased scalar
*
* @param description the description of the aliased scalar
*
* @return this builder
*/
public Builder description(String description) {
this.description = description;
return this;
}
/**
* Sets the scalar that is to be aliased
*
* @param aliasedScalar the scalar that is to be aliased
*
* @return this builder
*/
public Builder aliasedScalar(GraphQLScalarType aliasedScalar) {
this.aliasedScalar = aliasedScalar;
return this;
}
/**
* @return the built {@link AliasedScalar}
*/
public GraphQLScalarType build() {
Assert.assertNotNull(name);
return aliasedScalarImpl(name, description, aliasedScalar);
}
}
private static GraphQLScalarType aliasedScalarImpl(String name, String description, GraphQLScalarType aliasedScalar) {
Assert.assertNotNull(aliasedScalar);
Coercing<Object, Object> coercing = new Coercing<>() {
@Override
public Object serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
return aliasedScalar.getCoercing().serialize(input, graphQLContext, locale);
}
@Override
public Object parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
return aliasedScalar.getCoercing().parseValue(input, graphQLContext, locale);
}
@Override
public Object parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
return aliasedScalar.getCoercing().parseLiteral(input, variables, graphQLContext, locale);
}
@Override
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
return aliasedScalar.getCoercing().valueToLiteral(input, graphQLContext, locale);
}
};
return GraphQLScalarType.newScalar()
.name(name)
.description(description)
.coercing(coercing)
.build();
}
}