Skip to content

Commit bb7e36d

Browse files
committed
Finish the GraphQL Schema parsing so it supports SchemaDefinition,
DirectiveDefinition, and TypeExtensionDefinition type declarations.
1 parent a2513f3 commit bb7e36d

9 files changed

Lines changed: 452 additions & 22 deletions

File tree

‎src/main/antlr/Graphql.g4‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ typeCondition : typeName;
5959

6060
// Value
6161

62-
name: NAME | SCALAR | TYPE | INTERFACE | IMPLEMENTS | ENUM | UNION | INPUT | EXTEND | DIRECTIVE;
62+
name: NAME | SCHEMA | SCALAR | TYPE | INTERFACE | IMPLEMENTS | ENUM | UNION | INPUT | EXTEND | DIRECTIVE;
6363

6464
value :
6565
IntValue |
@@ -114,6 +114,17 @@ nonNullType: typeName '!' | listType '!';
114114

115115
// Type System
116116
typeSystemDefinition:
117+
schemaDefinition |
118+
typeDefinition |
119+
typeExtensionDefinition |
120+
directiveDefinition
121+
;
122+
123+
schemaDefinition : SCHEMA directives? '{' operationTypeDefinition+ '}';
124+
125+
operationTypeDefinition : operationType ':' typeName;
126+
127+
typeDefinition:
117128
scalarTypeDefinition |
118129
objectTypeDefinition |
119130
interfaceTypeDefinition |
@@ -122,14 +133,11 @@ enumTypeDefinition |
122133
inputObjectTypeDefinition
123134
;
124135

125-
namedType : name;
126-
127136
scalarTypeDefinition : SCALAR name directives?;
128137

129-
130138
objectTypeDefinition : TYPE name implementsInterfaces? directives? '{' fieldDefinition+ '}';
131139

132-
implementsInterfaces : IMPLEMENTS namedType+;
140+
implementsInterfaces : IMPLEMENTS typeName+;
133141

134142
fieldDefinition : name argumentsDefinition? ':' type directives?;
135143

@@ -142,8 +150,8 @@ interfaceTypeDefinition : INTERFACE name directives? '{' fieldDefinition+ '}';
142150
unionTypeDefinition : UNION name directives? '=' unionMembers;
143151

144152
unionMembers:
145-
namedType |
146-
unionMembers '|' namedType
153+
typeName |
154+
unionMembers '|' typeName
147155
;
148156

149157
enumTypeDefinition : ENUM name directives? '{' enumValueDefinition+ '}';
@@ -156,16 +164,19 @@ typeExtensionDefinition : EXTEND objectTypeDefinition;
156164

157165
directiveDefinition : DIRECTIVE '@' name argumentsDefinition? 'on' directiveLocations;
158166

167+
directiveLocation : name;
168+
159169
directiveLocations :
160-
name |
161-
directiveLocations '|' name
170+
directiveLocation |
171+
directiveLocations '|' directiveLocation
162172
;
163173

164174

165175
// Token
166176

167177
BooleanValue: 'true' | 'false';
168178

179+
SCHEMA: 'schema';
169180
SCALAR: 'scalar';
170181
TYPE: 'type';
171182
INTERFACE: 'interface';
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package graphql.language;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class DirectiveDefinition extends AbstractNode implements Definition {
8+
private String name;
9+
private List<InputValueDefinition> inputValueDefinitions = new ArrayList<InputValueDefinition>();
10+
private List<DirectiveLocation> directiveLocations = new ArrayList<DirectiveLocation>();
11+
12+
public DirectiveDefinition(String name) {
13+
this.name = name;
14+
}
15+
16+
public List<InputValueDefinition> getInputValueDefinitions() {
17+
return inputValueDefinitions;
18+
}
19+
20+
public List<DirectiveLocation> getDirectiveLocations() {
21+
return directiveLocations;
22+
}
23+
24+
@Override
25+
public List<Node> getChildren() {
26+
List<Node> result = new ArrayList<Node>();
27+
result.addAll(inputValueDefinitions);
28+
result.addAll(directiveLocations);
29+
return result;
30+
}
31+
32+
@Override
33+
public boolean isEqualTo(Node o) {
34+
if (this == o) return true;
35+
if (o == null || getClass() != o.getClass()) return false;
36+
37+
DirectiveDefinition that = (DirectiveDefinition) o;
38+
39+
if ( null == name ) {
40+
if ( null != that.name ) return false;
41+
} else if ( !name.equals(that.name) ) {
42+
return false;
43+
}
44+
return true;
45+
}
46+
47+
48+
@Override
49+
public String toString() {
50+
return "DirectiveDefinition{" +
51+
"name='" + name + "'" +
52+
", inputValueDefinitions=" + inputValueDefinitions +
53+
", directiveLocations=" + directiveLocations +
54+
"}";
55+
}
56+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package graphql.language;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
// This should probably be an enum... but the grammar
8+
// doesn't enforce the names. These are the current names:
9+
// QUERY
10+
// MUTATION
11+
// FIELD
12+
// FRAGMENT_DEFINITION
13+
// FRAGMENT_SPREAD
14+
// INLINE_FRAGMENT
15+
public class DirectiveLocation extends AbstractNode {
16+
private String name;
17+
18+
public DirectiveLocation(String name) {
19+
this.name = name;
20+
}
21+
22+
@Override
23+
public List<Node> getChildren() {
24+
List<Node> result = new ArrayList<Node>();
25+
return result;
26+
}
27+
28+
@Override
29+
public boolean isEqualTo(Node o) {
30+
if (this == o) return true;
31+
if (o == null || getClass() != o.getClass()) return false;
32+
33+
DirectiveLocation that = (DirectiveLocation) o;
34+
35+
if ( null == name ) {
36+
if ( null != that.name ) return false;
37+
} else if ( !name.equals(that.name) ) {
38+
return false;
39+
}
40+
return true;
41+
}
42+
43+
44+
@Override
45+
public String toString() {
46+
return "DirectiveLocation{" +
47+
"name='" + name + "'" +
48+
"}";
49+
}
50+
}

‎src/main/java/graphql/language/ObjectTypeDefinition.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public String getName() {
3131
return name;
3232
}
3333

34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
3438
@Override
3539
public List<Node> getChildren() {
3640
List<Node> result = new ArrayList<Node>();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package graphql.language;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class OperationTypeDefinition extends AbstractNode {
8+
private String name;
9+
private Type type;
10+
11+
public OperationTypeDefinition() {
12+
this(null);
13+
}
14+
15+
public OperationTypeDefinition(String name) {
16+
this(name, null);
17+
}
18+
19+
public OperationTypeDefinition(String name, Type type) {
20+
this.name = name;
21+
this.type = type;
22+
}
23+
24+
public Type getType() {
25+
return type;
26+
}
27+
28+
public void setType(Type type) {
29+
this.type = type;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
@Override
37+
public List<Node> getChildren() {
38+
List<Node> result = new ArrayList<Node>();
39+
result.add(type);
40+
return result;
41+
}
42+
43+
@Override
44+
public boolean isEqualTo(Node o) {
45+
if (this == o) return true;
46+
if (o == null || getClass() != o.getClass()) return false;
47+
48+
OperationTypeDefinition that = (OperationTypeDefinition) o;
49+
50+
if ( null == name ) {
51+
if ( null != that.name ) return false;
52+
} else if ( !name.equals(that.name) ) {
53+
return false;
54+
}
55+
return true;
56+
}
57+
58+
59+
@Override
60+
public String toString() {
61+
return "OperationTypeDefinition{" +
62+
"name='" + name + "'" +
63+
", type=" + type +
64+
"}";
65+
}
66+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package graphql.language;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class SchemaDefinition extends AbstractNode implements Definition {
8+
private List<Directive> directives = new ArrayList<Directive>();
9+
private List<OperationTypeDefinition> operationTypeDefinitions = new ArrayList<OperationTypeDefinition>();
10+
11+
public SchemaDefinition() {
12+
}
13+
14+
public List<Directive> getDirectives() {
15+
return directives;
16+
}
17+
18+
public List<OperationTypeDefinition> getOperationTypeDefinitions() {
19+
return operationTypeDefinitions;
20+
}
21+
22+
@Override
23+
public List<Node> getChildren() {
24+
List<Node> result = new ArrayList<Node>();
25+
result.addAll(directives);
26+
result.addAll(operationTypeDefinitions);
27+
return result;
28+
}
29+
30+
@Override
31+
public boolean isEqualTo(Node o) {
32+
if (this == o) return true;
33+
if (o == null || getClass() != o.getClass()) return false;
34+
35+
SchemaDefinition that = (SchemaDefinition) o;
36+
37+
return true;
38+
}
39+
40+
41+
@Override
42+
public String toString() {
43+
return "SchemaDefinition{" +
44+
"directives=" + directives +
45+
", operationTypeDefinitions=" + operationTypeDefinitions +
46+
"}";
47+
}
48+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package graphql.language;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class TypeExtensionDefinition extends ObjectTypeDefinition {
8+
public TypeExtensionDefinition() {
9+
super(null);
10+
}
11+
12+
public TypeExtensionDefinition(String name) {
13+
super(name);
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "TypeExtensionDefinition{" +
19+
"name='" + getName() + '\'' +
20+
", implements=" + getImplements() +
21+
", directives=" + getDirectives() +
22+
", fieldDefinitions=" + getFieldDefinitions() +
23+
'}';
24+
}
25+
}

0 commit comments

Comments
 (0)