Skip to content

Commit 703ee33

Browse files
committed
refactor parser and making Nodes immutable (WIP, not all test pass)
1 parent 795c232 commit 703ee33

43 files changed

Lines changed: 3432 additions & 974 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/main/java/graphql/introspection/IntrospectionResultToSchema.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,15 @@ InterfaceTypeDefinition createInterface(Map<String, Object> input) {
194194
InputObjectTypeDefinition createInputObject(Map<String, Object> input) {
195195
assertTrue(input.get("kind").equals("INPUT_OBJECT"), "wrong input");
196196

197-
InputObjectTypeDefinition inputObjectTypeDefinition = new InputObjectTypeDefinition((String) input.get("name"));
198-
inputObjectTypeDefinition.setComments(toComment((String) input.get("description")));
197+
InputObjectTypeDefinition.Builder inputObjectTypeDefinition = InputObjectTypeDefinition.newInputObjectDefinition()
198+
.name((String) input.get("name"))
199+
.comments(toComment((String) input.get("description")));
200+
199201
List<Map<String, Object>> fields = (List<Map<String, Object>>) input.get("inputFields");
200202
List<InputValueDefinition> inputValueDefinitions = createInputValueDefinitions(fields);
201-
inputObjectTypeDefinition.getInputValueDefinitions().addAll(inputValueDefinitions);
203+
inputObjectTypeDefinition.inputValueDefinitions(inputValueDefinitions);
202204

203-
return inputObjectTypeDefinition;
205+
return inputObjectTypeDefinition.build();
204206
}
205207

206208
@SuppressWarnings("unchecked")

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.util.TraverserContext;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.List;
910

1011
public class Argument extends AbstractNode<Argument> implements NamedNode<Argument> {
@@ -62,4 +63,46 @@ public String toString() {
6263
public TraversalControl accept(TraverserContext<Node> context, NodeVisitor visitor) {
6364
return visitor.visitArgument(this, context);
6465
}
66+
67+
public static Builder newArgument() {
68+
return new Builder();
69+
}
70+
71+
72+
public static final class Builder implements NodeBuilder {
73+
private SourceLocation sourceLocation;
74+
private String name;
75+
private Value value;
76+
private List<Comment> comments = Collections.emptyList();
77+
78+
private Builder() {
79+
}
80+
81+
public Builder sourceLocation(SourceLocation sourceLocation) {
82+
this.sourceLocation = sourceLocation;
83+
return this;
84+
}
85+
86+
public Builder name(String name) {
87+
this.name = name;
88+
return this;
89+
}
90+
91+
public Builder value(Value value) {
92+
this.value = value;
93+
return this;
94+
}
95+
96+
public Builder comments(List<Comment> comments) {
97+
this.comments = comments;
98+
return this;
99+
}
100+
101+
public Argument build() {
102+
Argument argument = new Argument(name, value);
103+
argument.setSourceLocation(sourceLocation);
104+
argument.setComments(comments);
105+
return argument;
106+
}
107+
}
65108
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.util.TraverserContext;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.List;
910

1011
public class ArrayValue extends AbstractNode<ArrayValue> implements Value<ArrayValue> {
@@ -57,4 +58,41 @@ public ArrayValue deepCopy() {
5758
public TraversalControl accept(TraverserContext<Node> context, NodeVisitor visitor) {
5859
return visitor.visitArrayValue(this, context);
5960
}
61+
62+
public static Builder newArrayValue() {
63+
return new Builder();
64+
}
65+
66+
67+
public static final class Builder implements NodeBuilder {
68+
private SourceLocation sourceLocation;
69+
private List<Value> values = new ArrayList<>();
70+
private List<Comment> comments = Collections.emptyList();
71+
72+
private Builder() {
73+
}
74+
75+
public Builder sourceLocation(SourceLocation sourceLocation) {
76+
this.sourceLocation = sourceLocation;
77+
return this;
78+
}
79+
80+
public Builder values(List<Value> values) {
81+
this.values = values;
82+
return this;
83+
}
84+
85+
public Builder comments(List<Comment> comments) {
86+
this.comments = comments;
87+
return this;
88+
}
89+
90+
public ArrayValue build() {
91+
ArrayValue arrayValue = new ArrayValue();
92+
arrayValue.setSourceLocation(sourceLocation);
93+
arrayValue.setValues(values);
94+
arrayValue.setComments(comments);
95+
return arrayValue;
96+
}
97+
}
6098
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.util.TraverserContext;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.List;
910

1011
public class BooleanValue extends AbstractNode<BooleanValue> implements Value<BooleanValue> {
@@ -56,4 +57,40 @@ public String toString() {
5657
public TraversalControl accept(TraverserContext<Node> context, NodeVisitor visitor) {
5758
return visitor.visitBooleanValue(this, context);
5859
}
60+
61+
public static Builder newBooleanValue() {
62+
return new Builder();
63+
}
64+
65+
public static final class Builder implements NodeBuilder {
66+
private SourceLocation sourceLocation;
67+
private boolean value;
68+
private List<Comment> comments = Collections.emptyList();
69+
70+
private Builder() {
71+
}
72+
73+
74+
public Builder sourceLocation(SourceLocation sourceLocation) {
75+
this.sourceLocation = sourceLocation;
76+
return this;
77+
}
78+
79+
public Builder value(boolean value) {
80+
this.value = value;
81+
return this;
82+
}
83+
84+
public Builder comments(List<Comment> comments) {
85+
this.comments = comments;
86+
return this;
87+
}
88+
89+
public BooleanValue build() {
90+
BooleanValue booleanValue = new BooleanValue(value);
91+
booleanValue.setSourceLocation(sourceLocation);
92+
booleanValue.setComments(comments);
93+
return booleanValue;
94+
}
95+
}
5996
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,46 @@ public String toString() {
7676
public TraversalControl accept(TraverserContext<Node> context, NodeVisitor visitor) {
7777
return visitor.visitDirective(this, context);
7878
}
79+
80+
public static Builder newDirective() {
81+
return new Builder();
82+
}
83+
84+
public static final class Builder implements NodeBuilder {
85+
private SourceLocation sourceLocation;
86+
private List<Comment> comments = Collections.emptyList();
87+
private String name;
88+
private List<Argument> arguments = new ArrayList<>();
89+
90+
private Builder() {
91+
}
92+
93+
94+
public Builder sourceLocation(SourceLocation sourceLocation) {
95+
this.sourceLocation = sourceLocation;
96+
return this;
97+
}
98+
99+
public Builder comments(List<Comment> comments) {
100+
this.comments = comments;
101+
return this;
102+
}
103+
104+
public Builder name(String name) {
105+
this.name = name;
106+
return this;
107+
}
108+
109+
public Builder arguments(List<Argument> arguments) {
110+
this.arguments = arguments;
111+
return this;
112+
}
113+
114+
public Directive build() {
115+
Directive directive = new Directive(name, arguments);
116+
directive.setSourceLocation(sourceLocation);
117+
directive.setComments(comments);
118+
return directive;
119+
}
120+
}
79121
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.util.TraverserContext;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.List;
910

1011
public class DirectiveDefinition extends AbstractNode<DirectiveDefinition> implements Definition<DirectiveDefinition>, NamedNode<DirectiveDefinition> {
@@ -82,4 +83,59 @@ public String toString() {
8283
public TraversalControl accept(TraverserContext<Node> context, NodeVisitor visitor) {
8384
return visitor.visitDirectiveDefinition(this, context);
8485
}
86+
87+
public static Builder newDirectiveDefinition() {
88+
return new Builder();
89+
}
90+
91+
92+
public static final class Builder implements NodeBuilder {
93+
private SourceLocation sourceLocation;
94+
private List<Comment> comments = Collections.emptyList();
95+
private String name;
96+
private Description description;
97+
private List<InputValueDefinition> inputValueDefinitions;
98+
private List<DirectiveLocation> directiveLocations;
99+
100+
private Builder() {
101+
}
102+
103+
public Builder sourceLocation(SourceLocation sourceLocation) {
104+
this.sourceLocation = sourceLocation;
105+
return this;
106+
}
107+
108+
public Builder comments(List<Comment> comments) {
109+
this.comments = comments;
110+
return this;
111+
}
112+
113+
public Builder name(String name) {
114+
this.name = name;
115+
return this;
116+
}
117+
118+
public Builder description(Description description) {
119+
this.description = description;
120+
return this;
121+
}
122+
123+
public Builder inputValueDefinitions(List<InputValueDefinition> inputValueDefinitions) {
124+
this.inputValueDefinitions = inputValueDefinitions;
125+
return this;
126+
}
127+
128+
public Builder directiveLocations(List<DirectiveLocation> directiveLocations) {
129+
this.directiveLocations = directiveLocations;
130+
return this;
131+
}
132+
133+
public DirectiveDefinition build() {
134+
DirectiveDefinition directiveDefinition = new DirectiveDefinition(name, inputValueDefinitions, directiveLocations);
135+
directiveDefinition.setSourceLocation(sourceLocation);
136+
directiveDefinition.setComments(comments);
137+
directiveDefinition.setDescription(description);
138+
return directiveDefinition;
139+
}
140+
}
85141
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.util.TraverserContext;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.List;
910

1011
// This should probably be an enum... but the grammar
@@ -58,4 +59,39 @@ public String toString() {
5859
public TraversalControl accept(TraverserContext<Node> context, NodeVisitor visitor) {
5960
return visitor.visitDirectiveLocation(this, context);
6061
}
62+
63+
public static Builder newDirectiveLocation() {
64+
return new Builder();
65+
}
66+
67+
public static final class Builder implements NodeBuilder {
68+
private SourceLocation sourceLocation;
69+
private List<Comment> comments = Collections.emptyList();
70+
private String name;
71+
72+
private Builder() {
73+
}
74+
75+
public Builder sourceLocation(SourceLocation sourceLocation) {
76+
this.sourceLocation = sourceLocation;
77+
return this;
78+
}
79+
80+
public Builder comments(List<Comment> comments) {
81+
this.comments = comments;
82+
return this;
83+
}
84+
85+
public Builder name(String name) {
86+
this.name = name;
87+
return this;
88+
}
89+
90+
public DirectiveLocation build() {
91+
DirectiveLocation directiveLocation = new DirectiveLocation(name);
92+
directiveLocation.setSourceLocation(sourceLocation);
93+
directiveLocation.setComments(comments);
94+
return directiveLocation;
95+
}
96+
}
6197
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.util.TraverserContext;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.List;
910

1011
public class Document extends AbstractNode<Document> {
@@ -58,4 +59,41 @@ public String toString() {
5859
public TraversalControl accept(TraverserContext<Node> context, NodeVisitor visitor) {
5960
return visitor.visitDocument(this, context);
6061
}
62+
63+
public static Builder newDocument() {
64+
return new Builder();
65+
}
66+
67+
68+
public static final class Builder implements NodeBuilder {
69+
private List<Definition> definitions;
70+
private SourceLocation sourceLocation;
71+
private List<Comment> comments = Collections.emptyList();
72+
73+
private Builder() {
74+
}
75+
76+
public Builder definitions(List<Definition> definitions) {
77+
this.definitions = definitions;
78+
return this;
79+
}
80+
81+
public Builder sourceLocation(SourceLocation sourceLocation) {
82+
this.sourceLocation = sourceLocation;
83+
return this;
84+
}
85+
86+
public Builder comments(List<Comment> comments) {
87+
this.comments = comments;
88+
return this;
89+
}
90+
91+
public Document build() {
92+
Document document = new Document();
93+
document.setDefinitions(definitions);
94+
document.setSourceLocation(sourceLocation);
95+
document.setComments(comments);
96+
return document;
97+
}
98+
}
6199
}

0 commit comments

Comments
 (0)