Skip to content

Commit

Permalink
graphql-java#357 - link a runtime object back to its possible definition
Browse files Browse the repository at this point in the history
+ some Java 8 fix ups
  • Loading branch information
bbakerman committed Apr 11, 2017
1 parent 6581b27 commit 4e0fecd
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 39 deletions.
30 changes: 24 additions & 6 deletions src/main/java/graphql/schema/GraphQLArgument.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graphql.schema;


import graphql.language.InputValueDefinition;

import java.util.Map;

import static graphql.Assert.assertNotNull;
Expand All @@ -12,18 +14,24 @@ public class GraphQLArgument {
private final String description;
private GraphQLInputType type;
private final Object defaultValue;
private final InputValueDefinition definition;

public GraphQLArgument(String name, String description, GraphQLInputType type, Object defaultValue) {
assertValidName(name);
this(name, description, type, defaultValue, null);
}

public GraphQLArgument(String name, GraphQLInputType type) {
this(name, null, type, null, null);
}

public GraphQLArgument(String name, String description, GraphQLInputType type, Object defaultValue, InputValueDefinition definition) {
assertValidName(name);
assertNotNull(type, "type can't be null");
this.name = name;
this.description = description;
this.type = type;
this.defaultValue = defaultValue;
}

public GraphQLArgument(String name, GraphQLInputType type) {
this(name, null, type, null);
this.definition = definition;
}


Expand All @@ -48,6 +56,10 @@ public String getDescription() {
return description;
}

public InputValueDefinition getDefinition() {
return definition;
}

public static Builder newArgument() {
return new Builder();
}
Expand All @@ -58,6 +70,7 @@ public static class Builder {
private GraphQLInputType type;
private Object defaultValue;
private String description;
private InputValueDefinition definition;

public Builder name(String name) {
this.name = name;
Expand All @@ -69,6 +82,11 @@ public Builder description(String description) {
return this;
}

public Builder definition(InputValueDefinition definition) {
this.definition = definition;
return this;
}


public Builder type(GraphQLInputType type) {
this.type = type;
Expand All @@ -81,7 +99,7 @@ public Builder defaultValue(Object defaultValue) {
}

public GraphQLArgument build() {
return new GraphQLArgument(name, description, type, defaultValue);
return new GraphQLArgument(name, description, type, defaultValue, definition);
}
}

Expand Down
22 changes: 19 additions & 3 deletions src/main/java/graphql/schema/GraphQLEnumType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package graphql.schema;


import graphql.language.EnumTypeDefinition;
import graphql.language.EnumValue;
import graphql.AssertException;

Expand All @@ -16,6 +17,7 @@ public class GraphQLEnumType implements GraphQLType, GraphQLInputType, GraphQLOu
private final String name;
private final String description;
private final Map<String, GraphQLEnumValueDefinition> valueDefinitionMap = new LinkedHashMap<>();
private final EnumTypeDefinition definition;

private final Coercing coercing = new Coercing() {
@Override
Expand All @@ -39,7 +41,7 @@ public Object parseLiteral(Object input) {
};

private Object getValueByName(Object value) {
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(value);
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(value.toString());
if (enumValueDefinition != null) return enumValueDefinition.getValue();
return null;
}
Expand All @@ -63,9 +65,14 @@ public List<GraphQLEnumValueDefinition> getValues() {


public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values) {
assertValidName(name);
this(name,description,values,null);
}

public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values, EnumTypeDefinition definition) {
assertValidName(name);
this.name = name;
this.description = description;
this.definition = definition;
buildMap(values);
}

Expand All @@ -90,6 +97,9 @@ public Coercing getCoercing() {
return coercing;
}

public EnumTypeDefinition getDefinition() {
return definition;
}

public static Builder newEnum() {
return new Builder();
Expand All @@ -99,6 +109,7 @@ public static class Builder {

private String name;
private String description;
private EnumTypeDefinition definition;
private final List<GraphQLEnumValueDefinition> values = new ArrayList<>();

public Builder name(String name) {
Expand All @@ -111,6 +122,11 @@ public Builder description(String description) {
return this;
}

public Builder definition(EnumTypeDefinition definition) {
this.definition = definition;
return this;
}

public Builder value(String name, Object value, String description, String deprecationReason) {
values.add(new GraphQLEnumValueDefinition(name, description, value, deprecationReason));
return this;
Expand All @@ -133,7 +149,7 @@ public Builder value(String name) {


public GraphQLEnumType build() {
return new GraphQLEnumType(name, description, values);
return new GraphQLEnumType(name, description, values, definition);
}

}
Expand Down
29 changes: 21 additions & 8 deletions src/main/java/graphql/schema/GraphQLFieldDefinition.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graphql.schema;


import graphql.language.FieldDefinition;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -16,10 +18,15 @@ public class GraphQLFieldDefinition {
private final DataFetcher dataFetcher;
private final String deprecationReason;
private final List<GraphQLArgument> arguments = new ArrayList<>();
private final FieldDefinition definition;


public GraphQLFieldDefinition(String name, String description, GraphQLOutputType type, DataFetcher dataFetcher, List<GraphQLArgument> arguments, String deprecationReason) {
assertValidName(name);
this(name,description,type, dataFetcher,arguments,deprecationReason,null);
}

public GraphQLFieldDefinition(String name, String description, GraphQLOutputType type, DataFetcher dataFetcher, List<GraphQLArgument> arguments, String deprecationReason, FieldDefinition definition) {
assertValidName(name);
assertNotNull(dataFetcher, "dataFetcher can't be null");
assertNotNull(type, "type can't be null");
assertNotNull(arguments, "arguments can't be null");
Expand All @@ -29,6 +36,7 @@ public GraphQLFieldDefinition(String name, String description, GraphQLOutputType
this.dataFetcher = dataFetcher;
this.arguments.addAll(arguments);
this.deprecationReason = deprecationReason;
this.definition = definition;
}


Expand Down Expand Up @@ -64,6 +72,10 @@ public String getDescription() {
return description;
}

public FieldDefinition getDefinition() {
return definition;
}

public String getDeprecationReason() {
return deprecationReason;
}
Expand All @@ -85,13 +97,19 @@ public static class Builder {
private List<GraphQLArgument> arguments = new ArrayList<>();
private String deprecationReason;
private boolean isField;
private FieldDefinition definition;


public Builder name(String name) {
this.name = name;
return this;
}

public Builder definition(FieldDefinition definition) {
this.definition = definition;
return this;
}

public Builder description(String description) {
this.description = description;
return this;
Expand Down Expand Up @@ -120,12 +138,7 @@ public Builder dataFetcher(DataFetcher dataFetcher) {
}

public Builder staticValue(final Object value) {
this.dataFetcher = new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
return value;
}
};
this.dataFetcher = environment -> value;
return this;
}

Expand Down Expand Up @@ -192,7 +205,7 @@ public GraphQLFieldDefinition build() {
dataFetcher = new PropertyDataFetcher(name);
}
}
return new GraphQLFieldDefinition(name, description, type, dataFetcher, arguments, deprecationReason);
return new GraphQLFieldDefinition(name, description, type, dataFetcher, arguments, deprecationReason, definition);
}


Expand Down
24 changes: 21 additions & 3 deletions src/main/java/graphql/schema/GraphQLInputObjectField.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graphql.schema;


import graphql.language.InputValueDefinition;

import java.util.Map;

import static graphql.Assert.assertNotNull;
Expand All @@ -12,18 +14,24 @@ public class GraphQLInputObjectField {
private final String description;
private GraphQLInputType type;
private final Object defaultValue;
private final InputValueDefinition definition;

public GraphQLInputObjectField(String name, GraphQLInputType type) {
this(name, null, type, null);
this(name, null, type, null, null);
}

public GraphQLInputObjectField(String name, String description, GraphQLInputType type, Object defaultValue) {
assertValidName(name);
this(name,description,type,defaultValue,null);
}

public GraphQLInputObjectField(String name, String description, GraphQLInputType type, Object defaultValue, InputValueDefinition definition) {
assertValidName(name);
assertNotNull(type, "type can't be null");
this.name = name;
this.type = type;
this.defaultValue = defaultValue;
this.description = description;
this.definition = definition;
}

void replaceTypeReferences(Map<String, GraphQLType> typeMap) {
Expand All @@ -46,6 +54,10 @@ public String getDescription() {
return description;
}

public InputValueDefinition getDefinition() {
return definition;
}

public static Builder newInputObjectField() {
return new Builder();
}
Expand All @@ -55,6 +67,7 @@ public static class Builder {
private String description;
private Object defaultValue;
private GraphQLInputType type;
private InputValueDefinition definition;

public Builder name(String name) {
this.name = name;
Expand All @@ -66,6 +79,11 @@ public Builder description(String description) {
return this;
}

public Builder definition(InputValueDefinition definition) {
this.definition = definition;
return this;
}

public Builder type(GraphQLInputObjectType.Builder type) {
return type(type.build());
}
Expand All @@ -81,7 +99,7 @@ public Builder defaultValue(Object defaultValue) {
}

public GraphQLInputObjectField build() {
return new GraphQLInputObjectField(name, description, type, defaultValue);
return new GraphQLInputObjectField(name, description, type, defaultValue, definition);
}
}
}
24 changes: 19 additions & 5 deletions src/main/java/graphql/schema/GraphQLInputObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;

import graphql.AssertException;
import graphql.language.InputObjectTypeDefinition;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;
Expand All @@ -15,15 +16,18 @@ public class GraphQLInputObjectType implements GraphQLType, GraphQLInputType, Gr

private final String name;
private final String description;


private final Map<String, GraphQLInputObjectField> fieldMap = new LinkedHashMap<>();
private final InputObjectTypeDefinition definition;

public GraphQLInputObjectType(String name, String description, List<GraphQLInputObjectField> fields) {
assertValidName(name);
this(name,description,fields,null);
}
public GraphQLInputObjectType(String name, String description, List<GraphQLInputObjectField> fields, InputObjectTypeDefinition definition) {
assertValidName(name);
assertNotNull(fields, "fields can't be null");
this.name = name;
this.description = description;
this.definition = definition;
buildMap(fields);
}

Expand Down Expand Up @@ -70,9 +74,14 @@ public List<GraphQLInputObjectField> getFieldDefinitions() {
return new ArrayList<>(fieldMap.values());
}

public InputObjectTypeDefinition getDefinition() {
return definition;
}

public static class Builder {
private String name;
private String description;
private InputObjectTypeDefinition definition;
private List<GraphQLInputObjectField> fields = new ArrayList<>();

public Builder name(String name) {
Expand All @@ -85,6 +94,11 @@ public Builder description(String description) {
return this;
}

public Builder definition(InputObjectTypeDefinition definition) {
this.definition = definition;
return this;
}

public Builder field(GraphQLInputObjectField field) {
assertNotNull(field, "field can't be null");
fields.add(field);
Expand Down Expand Up @@ -130,14 +144,14 @@ public Builder fields(List<GraphQLInputObjectField> fields) {
}

public GraphQLInputObjectType build() {
return new GraphQLInputObjectType(name, description, fields);
return new GraphQLInputObjectType(name, description, fields, definition);
}

}

private static class Reference extends GraphQLInputObjectType implements TypeReference {
private Reference(String name) {
super(name, "", Collections.<GraphQLInputObjectField>emptyList());
super(name, "", Collections.emptyList());
}
}
}
Loading

0 comments on commit 4e0fecd

Please sign in to comment.