Skip to content

Commit c96cf83

Browse files
committed
star wars schema example
1 parent 5fe8f70 commit c96cf83

File tree

6 files changed

+199
-9
lines changed

6 files changed

+199
-9
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ and the JavaScript [reference implementation](https://github.com/graphql/graphql
55

66
It's an early version, but the query parser should be near 100%.
77

8-
The execution part is WIP, and validation, error handling and more is still missing.
8+
The execution part is WIP, and validation, error handling and more is still missing.
9+
10+
### How to use it
11+
12+
#### Schema definition
13+
14+
A complex schema (stolen from the js reference implementation): [StarWarsSchema](src/test/java/graphql/StarWarsSchema.java)
15+
16+
#### Query
17+
18+
For how to define a simple schema and execute queries: [GraphQL Test](src/test/groovy/graphql/GraphQLTest.groovy)
919

10-
For how to define a schema and execute queries: [GraphQL Test](src/test/groovy/graphql/GraphQLTest.groovy)
1120

1221
### Details
1322

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,75 @@
11
package graphql.schema;
22

33

4+
import java.util.ArrayList;
5+
import java.util.LinkedHashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
49
public class GraphQLEnumType implements GraphQLType, GraphQLInputType, GraphQLOutputType {
510

6-
String name;
11+
private final String name;
12+
private final String description;
13+
private final Map<String, GraphQLEnumValueDefinition> valueDefinitionMap = new LinkedHashMap<>();
714

815
private Coercing coercing;
916

17+
public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values) {
18+
this.name = name;
19+
this.description = description;
20+
buildMap(values);
21+
}
22+
23+
private void buildMap(List<GraphQLEnumValueDefinition> values) {
24+
for (GraphQLEnumValueDefinition valueDefinition : values) {
25+
valueDefinitionMap.put(valueDefinition.getName(), valueDefinition);
26+
}
27+
}
28+
1029
public String getName() {
1130
return name;
1231
}
1332

14-
public void setName(String name) {
15-
this.name = name;
16-
}
1733

1834
public Coercing getCoercing() {
1935
return coercing;
2036
}
2137

22-
public void setCoercing(Coercing coercing) {
23-
this.coercing = coercing;
38+
39+
public static Builder newEnum() {
40+
return new Builder();
41+
}
42+
43+
public static class Builder {
44+
45+
private String name;
46+
private String description;
47+
private final List<GraphQLEnumValueDefinition> values = new ArrayList<>();
48+
49+
public Builder name(String name) {
50+
this.name = name;
51+
return this;
52+
}
53+
54+
public Builder description(String description) {
55+
this.description = description;
56+
return this;
57+
}
58+
59+
public Builder value(String name, Object value, String description) {
60+
values.add(new GraphQLEnumValueDefinition(name, description, value));
61+
return this;
62+
}
63+
64+
public Builder value(String name) {
65+
values.add(new GraphQLEnumValueDefinition(name, null, null));
66+
return this;
67+
}
68+
69+
70+
public GraphQLEnumType build() {
71+
return new GraphQLEnumType(name, description, values);
72+
}
73+
2474
}
2575
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package graphql.schema;
2+
3+
4+
public class GraphQLEnumValueDefinition {
5+
6+
private final String name;
7+
private final String description;
8+
private final Object value;
9+
10+
public GraphQLEnumValueDefinition(String name, String description, Object value) {
11+
this.name = name;
12+
this.description = description;
13+
this.value = value;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public String getDescription() {
21+
return description;
22+
}
23+
24+
public Object getValue() {
25+
return value;
26+
}
27+
}

src/main/java/graphql/schema/GraphQLInterfaceType.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
public class GraphQLInterfaceType implements GraphQLType, GraphQLOutputType {
1010

1111
private final String name;
12+
private final String description;
1213
private final Map<String, GraphQLFieldDefinition> fieldDefinitionsByName = new LinkedHashMap<>();
1314

14-
public GraphQLInterfaceType(String name, List<GraphQLFieldDefinition> fieldDefinitions) {
15+
public GraphQLInterfaceType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions) {
1516
this.name = name;
17+
this.description = description;
1618
buildDefinitionMap(fieldDefinitions);
1719
}
1820

@@ -34,4 +36,42 @@ public List<GraphQLFieldDefinition> getFieldDefinitions() {
3436
public String getName() {
3537
return name;
3638
}
39+
40+
public String getDescription() {
41+
return description;
42+
}
43+
44+
public static Builder newInterface() {
45+
return new Builder();
46+
}
47+
48+
49+
public static class Builder {
50+
private String name;
51+
private String description;
52+
private List<GraphQLFieldDefinition> fields = new ArrayList<>();
53+
54+
55+
public Builder name(String name) {
56+
this.name = name;
57+
return this;
58+
}
59+
60+
public Builder description(String description) {
61+
this.description = description;
62+
return this;
63+
}
64+
65+
public Builder field(GraphQLFieldDefinition fieldDefinition) {
66+
fields.add(fieldDefinition);
67+
return this;
68+
}
69+
70+
public GraphQLInterfaceType build() {
71+
return new GraphQLInterfaceType(name,description,fields);
72+
}
73+
74+
}
75+
76+
3777
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package graphql.schema;
2+
3+
4+
public class GraphQLSelfReference implements GraphQLType {
5+
6+
private final String name;
7+
8+
public GraphQLSelfReference(String name) {
9+
this.name = name;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package grapqhl;
2+
3+
4+
import graphql.Scalars;
5+
import graphql.schema.*;
6+
7+
import static graphql.Scalars.GraphQLString;
8+
import static graphql.schema.GraphQLEnumType.newEnum;
9+
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
10+
import static graphql.schema.GraphQLInterfaceType.newInterface;
11+
12+
public class StarWarsSchema {
13+
14+
15+
GraphQLEnumType episodeEnum = newEnum()
16+
.name("Episode")
17+
.description("One of the films in the Star Wars Trilogy")
18+
.value("NEWHOPE", 4, "Released in 1977.")
19+
.value("EMPIRE", 5, "Released in 1980.")
20+
.value("JEDI", 6, "Released in 1983.")
21+
.build();
22+
23+
24+
GraphQLInterfaceType characterInterface = newInterface()
25+
.name("Character")
26+
.description("A character in the Star Wars Trilogy")
27+
.field(newFieldDefinition()
28+
.name("id")
29+
.description("The id of the character.")
30+
.type(new GraphQLNonNull(GraphQLString))
31+
.build())
32+
.field(newFieldDefinition()
33+
.name("name")
34+
.description("The name of the character.")
35+
.type(GraphQLString)
36+
.build())
37+
.field(newFieldDefinition()
38+
.name("friends")
39+
.description("The friends of the character, or an empty list if they have none.")
40+
.type(new GraphQLList(new GraphQLSelfReference("Character")))
41+
.build())
42+
.field(newFieldDefinition()
43+
.name("appearsIn")
44+
.description("Which movies they appear in.")
45+
.type(new GraphQLList(episodeEnum))
46+
.build())
47+
.build();
48+
49+
}

0 commit comments

Comments
 (0)