forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLScalarType.java
More file actions
126 lines (101 loc) · 3.79 KB
/
GraphQLScalarType.java
File metadata and controls
126 lines (101 loc) · 3.79 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package graphql.schema;
import graphql.Internal;
import graphql.PublicApi;
import graphql.language.ScalarTypeDefinition;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;
import static java.util.Collections.emptyList;
/**
* A scalar type is a leaf node in the graphql tree of types. This class allows you to define new scalar types.
*
* <blockquote>
* GraphQL provides a number of built‐in scalars, but type systems can add additional scalars with semantic meaning,
* for example, a GraphQL system could define a scalar called Time which, while serialized as a string, promises to
* conform to ISO‐8601. When querying a field of type Time, you can then rely on the ability to parse the result with an ISO‐8601 parser and use a client‐specific primitive for time.
*
* From the spec : http://facebook.github.io/graphql/#sec-Scalars
* </blockquote>
*
* graphql-java ships with a set of predefined scalar types via {@link graphql.Scalars}
*
* @see graphql.Scalars
*/
public class GraphQLScalarType implements GraphQLType, GraphQLInputType, GraphQLOutputType, GraphQLUnmodifiedType, GraphQLNullableType, GraphQLDirectiveContainer {
private final String name;
private final String description;
private final Coercing coercing;
private final ScalarTypeDefinition definition;
private final List<GraphQLDirective> directives;
@Internal
public GraphQLScalarType(String name, String description, Coercing coercing) {
this(name, description, coercing, emptyList(), null);
}
@Internal
public GraphQLScalarType(String name, String description, Coercing coercing, List<GraphQLDirective> directives, ScalarTypeDefinition definition) {
assertValidName(name);
assertNotNull(coercing, "coercing can't be null");
assertNotNull(directives, "directives can't be null");
this.name = name;
this.description = description;
this.coercing = coercing;
this.definition = definition;
this.directives = directives;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public Coercing getCoercing() {
return coercing;
}
public ScalarTypeDefinition getDefinition() {
return definition;
}
@Override
public List<GraphQLDirective> getDirectives() {
return new ArrayList<>(directives);
}
@Override
public String toString() {
return "GraphQLScalarType{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
", coercing=" + coercing +
'}';
}
public static Builder newScalar() {
return new Builder();
}
@PublicApi
public static class Builder {
private String name;
private String description;
private Coercing coercing;
private ScalarTypeDefinition definition;
private final List<GraphQLDirective> directives = new ArrayList<>();
public Builder name(String name) {
this.name = name;
return this;
}
public Builder description(String description) {
this.description = description;
return this;
}
public Builder definition(ScalarTypeDefinition definition) {
this.definition = definition;
return this;
}
public Builder withDirectives(GraphQLDirective... directives) {
Collections.addAll(this.directives, directives);
return this;
}
public GraphQLScalarType build() {
return new GraphQLScalarType(name, description, coercing, directives, definition);
}
}
}