forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GarfieldSchema.java
193 lines (161 loc) · 5.54 KB
/
GarfieldSchema.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package graphql;
import graphql.schema.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static graphql.Scalars.GraphQLBoolean;
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLInterfaceType.newInterface;
import static graphql.schema.GraphQLObjectType.newObject;
import static graphql.schema.GraphQLUnionType.newUnionType;
public class GarfieldSchema {
public interface Named {
String getName();
}
public static class Dog implements Named {
private final String name;
private final boolean barks;
public Dog(String name, boolean barks) {
this.name = name;
this.barks = barks;
}
public boolean isBarks() {
return barks;
}
@Override
public String getName() {
return name;
}
}
public static class Cat implements Named {
private final String name;
private final boolean meows;
public Cat(String name, boolean meows) {
this.name = name;
this.meows = meows;
}
public boolean isMeows() {
return meows;
}
@Override
public String getName() {
return name;
}
}
public static class Person implements Named {
private final String name;
private List<Dog> dogs;
private List<Cat> cats;
private List<Named> friends;
public Person(String name) {
this(name, Collections.<Cat>emptyList(), Collections.<Dog>emptyList(), Collections.<Named>emptyList());
}
public Person(String name, List<Cat> cats, List<Dog> dogs, List<Named> friends) {
this.name = name;
this.dogs = dogs;
this.cats = cats;
this.friends = friends;
}
public List<Object> getPets() {
List<Object> pets = new ArrayList<Object>();
pets.addAll(cats);
pets.addAll(dogs);
return pets;
}
@Override
public String getName() {
return name;
}
public List<Named> getFriends() {
return friends;
}
}
public static Cat garfield = new Cat("Garfield", false);
public static Dog odie = new Dog("Odie", true);
public static Person liz = new Person("Liz");
public static Person john = new Person("John", Arrays.asList(garfield), Arrays.asList(odie), Arrays.asList(liz, odie));
public static GraphQLInterfaceType NamedType = newInterface()
.name("Named")
.field(newFieldDefinition()
.name("name")
.type(GraphQLString)
.build())
.typeResolver(new TypeResolver() {
@Override
public GraphQLObjectType getType(Object object) {
if (object instanceof Dog) {
return DogType;
}
if (object instanceof Person) {
return PersonType;
}
if (object instanceof Cat) {
return CatType;
}
return null;
}
})
.build();
public static GraphQLObjectType DogType = newObject()
.name("Dog")
.field(newFieldDefinition()
.name("name")
.type(GraphQLString)
.build())
.field(newFieldDefinition()
.name("barks")
.type(GraphQLBoolean)
.build())
.withInterface(NamedType)
.build();
public static GraphQLObjectType CatType = newObject()
.name("Cat")
.field(newFieldDefinition()
.name("name")
.type(GraphQLString)
.build())
.field(newFieldDefinition()
.name("meows")
.type(GraphQLBoolean)
.build())
.withInterface(NamedType)
.build();
public static GraphQLUnionType PetType = newUnionType()
.name("Pet")
.possibleType(CatType)
.possibleType(DogType)
.typeResolver(new TypeResolver() {
@Override
public GraphQLObjectType getType(Object object) {
if (object instanceof Cat) {
return CatType;
}
if (object instanceof Dog) {
return DogType;
}
return null;
}
})
.build();
public static GraphQLObjectType PersonType = newObject()
.name("Person")
.field(newFieldDefinition()
.name("name")
.type(GraphQLString)
.build())
.field(newFieldDefinition()
.name("pets")
.type(new GraphQLList(PetType))
.build())
.field(newFieldDefinition()
.name("friends")
.type(new GraphQLList(NamedType))
.build())
.withInterface(NamedType)
.build();
public static GraphQLSchema GarfieldSchema = GraphQLSchema.newSchema()
.query(PersonType)
.build();
}