From 4e0fecd89da8d190d60d5ae3215e10175447ae7b Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Tue, 11 Apr 2017 12:24:53 +1000 Subject: [PATCH] #357 - link a runtime object back to its possible definition + some Java 8 fix ups --- .../java/graphql/schema/GraphQLArgument.java | 30 +++++++++++++++---- .../java/graphql/schema/GraphQLEnumType.java | 22 ++++++++++++-- .../schema/GraphQLFieldDefinition.java | 29 +++++++++++++----- .../schema/GraphQLInputObjectField.java | 24 +++++++++++++-- .../schema/GraphQLInputObjectType.java | 24 +++++++++++---- .../graphql/schema/GraphQLInterfaceType.java | 23 ++++++++++++-- .../graphql/schema/GraphQLObjectType.java | 29 ++++++++++++++---- .../graphql/schema/GraphQLScalarType.java | 15 ++++++++-- .../java/graphql/schema/GraphQLUnionType.java | 26 ++++++++++++++-- 9 files changed, 183 insertions(+), 39 deletions(-) diff --git a/src/main/java/graphql/schema/GraphQLArgument.java b/src/main/java/graphql/schema/GraphQLArgument.java index aa1a888013..f04ff28a5e 100644 --- a/src/main/java/graphql/schema/GraphQLArgument.java +++ b/src/main/java/graphql/schema/GraphQLArgument.java @@ -1,6 +1,8 @@ package graphql.schema; +import graphql.language.InputValueDefinition; + import java.util.Map; import static graphql.Assert.assertNotNull; @@ -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; } @@ -48,6 +56,10 @@ public String getDescription() { return description; } + public InputValueDefinition getDefinition() { + return definition; + } + public static Builder newArgument() { return new Builder(); } @@ -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; @@ -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; @@ -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); } } diff --git a/src/main/java/graphql/schema/GraphQLEnumType.java b/src/main/java/graphql/schema/GraphQLEnumType.java index 4fe7b8a62a..133694d930 100644 --- a/src/main/java/graphql/schema/GraphQLEnumType.java +++ b/src/main/java/graphql/schema/GraphQLEnumType.java @@ -1,6 +1,7 @@ package graphql.schema; +import graphql.language.EnumTypeDefinition; import graphql.language.EnumValue; import graphql.AssertException; @@ -16,6 +17,7 @@ public class GraphQLEnumType implements GraphQLType, GraphQLInputType, GraphQLOu private final String name; private final String description; private final Map valueDefinitionMap = new LinkedHashMap<>(); + private final EnumTypeDefinition definition; private final Coercing coercing = new Coercing() { @Override @@ -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; } @@ -63,9 +65,14 @@ public List getValues() { public GraphQLEnumType(String name, String description, List values) { - assertValidName(name); + this(name,description,values,null); + } + + public GraphQLEnumType(String name, String description, List values, EnumTypeDefinition definition) { + assertValidName(name); this.name = name; this.description = description; + this.definition = definition; buildMap(values); } @@ -90,6 +97,9 @@ public Coercing getCoercing() { return coercing; } + public EnumTypeDefinition getDefinition() { + return definition; + } public static Builder newEnum() { return new Builder(); @@ -99,6 +109,7 @@ public static class Builder { private String name; private String description; + private EnumTypeDefinition definition; private final List values = new ArrayList<>(); public Builder name(String name) { @@ -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; @@ -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); } } diff --git a/src/main/java/graphql/schema/GraphQLFieldDefinition.java b/src/main/java/graphql/schema/GraphQLFieldDefinition.java index 2ef2863bc4..c5caeaa5f2 100644 --- a/src/main/java/graphql/schema/GraphQLFieldDefinition.java +++ b/src/main/java/graphql/schema/GraphQLFieldDefinition.java @@ -1,6 +1,8 @@ package graphql.schema; +import graphql.language.FieldDefinition; + import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -16,10 +18,15 @@ public class GraphQLFieldDefinition { private final DataFetcher dataFetcher; private final String deprecationReason; private final List arguments = new ArrayList<>(); + private final FieldDefinition definition; public GraphQLFieldDefinition(String name, String description, GraphQLOutputType type, DataFetcher dataFetcher, List arguments, String deprecationReason) { - assertValidName(name); + this(name,description,type, dataFetcher,arguments,deprecationReason,null); + } + + public GraphQLFieldDefinition(String name, String description, GraphQLOutputType type, DataFetcher dataFetcher, List 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"); @@ -29,6 +36,7 @@ public GraphQLFieldDefinition(String name, String description, GraphQLOutputType this.dataFetcher = dataFetcher; this.arguments.addAll(arguments); this.deprecationReason = deprecationReason; + this.definition = definition; } @@ -64,6 +72,10 @@ public String getDescription() { return description; } + public FieldDefinition getDefinition() { + return definition; + } + public String getDeprecationReason() { return deprecationReason; } @@ -85,6 +97,7 @@ public static class Builder { private List arguments = new ArrayList<>(); private String deprecationReason; private boolean isField; + private FieldDefinition definition; public Builder name(String name) { @@ -92,6 +105,11 @@ public Builder name(String name) { return this; } + public Builder definition(FieldDefinition definition) { + this.definition = definition; + return this; + } + public Builder description(String description) { this.description = description; return this; @@ -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; } @@ -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); } diff --git a/src/main/java/graphql/schema/GraphQLInputObjectField.java b/src/main/java/graphql/schema/GraphQLInputObjectField.java index f712d428fa..03f0fe306f 100644 --- a/src/main/java/graphql/schema/GraphQLInputObjectField.java +++ b/src/main/java/graphql/schema/GraphQLInputObjectField.java @@ -1,6 +1,8 @@ package graphql.schema; +import graphql.language.InputValueDefinition; + import java.util.Map; import static graphql.Assert.assertNotNull; @@ -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 typeMap) { @@ -46,6 +54,10 @@ public String getDescription() { return description; } + public InputValueDefinition getDefinition() { + return definition; + } + public static Builder newInputObjectField() { return new Builder(); } @@ -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; @@ -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()); } @@ -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); } } } \ No newline at end of file diff --git a/src/main/java/graphql/schema/GraphQLInputObjectType.java b/src/main/java/graphql/schema/GraphQLInputObjectType.java index 58d66f49ac..8c6e335311 100644 --- a/src/main/java/graphql/schema/GraphQLInputObjectType.java +++ b/src/main/java/graphql/schema/GraphQLInputObjectType.java @@ -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; @@ -15,15 +16,18 @@ public class GraphQLInputObjectType implements GraphQLType, GraphQLInputType, Gr private final String name; private final String description; - - private final Map fieldMap = new LinkedHashMap<>(); + private final InputObjectTypeDefinition definition; public GraphQLInputObjectType(String name, String description, List fields) { - assertValidName(name); + this(name,description,fields,null); + } + public GraphQLInputObjectType(String name, String description, List fields, InputObjectTypeDefinition definition) { + assertValidName(name); assertNotNull(fields, "fields can't be null"); this.name = name; this.description = description; + this.definition = definition; buildMap(fields); } @@ -70,9 +74,14 @@ public List 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 fields = new ArrayList<>(); public Builder name(String name) { @@ -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); @@ -130,14 +144,14 @@ public Builder fields(List 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.emptyList()); + super(name, "", Collections.emptyList()); } } } diff --git a/src/main/java/graphql/schema/GraphQLInterfaceType.java b/src/main/java/graphql/schema/GraphQLInterfaceType.java index 7abdeca4de..5dbadc6421 100644 --- a/src/main/java/graphql/schema/GraphQLInterfaceType.java +++ b/src/main/java/graphql/schema/GraphQLInterfaceType.java @@ -7,6 +7,7 @@ import java.util.Map; import graphql.AssertException; +import graphql.language.InterfaceTypeDefinition; import static graphql.Assert.assertNotNull; import static graphql.Assert.assertValidName; @@ -17,15 +18,21 @@ public class GraphQLInterfaceType implements GraphQLType, GraphQLOutputType, Gra private final String description; private final Map fieldDefinitionsByName = new LinkedHashMap<>(); private final TypeResolver typeResolver; + private final InterfaceTypeDefinition definition; public GraphQLInterfaceType(String name, String description, List fieldDefinitions, TypeResolver typeResolver) { - assertValidName(name); + this(name,description,fieldDefinitions,typeResolver,null); + } + + public GraphQLInterfaceType(String name, String description, List fieldDefinitions, TypeResolver typeResolver, InterfaceTypeDefinition definition) { + assertValidName(name); assertNotNull(typeResolver, "typeResolver can't null"); assertNotNull(fieldDefinitions, "fieldDefinitions can't null"); this.name = name; this.description = description; buildDefinitionMap(fieldDefinitions); this.typeResolver = typeResolver; + this.definition = definition; } private void buildDefinitionMap(List fieldDefinitions) { @@ -58,6 +65,10 @@ public TypeResolver getTypeResolver() { return typeResolver; } + public InterfaceTypeDefinition getDefinition() { + return definition; + } + @Override public String toString() { return "GraphQLInterfaceType{" + @@ -81,6 +92,7 @@ public static class Builder { private String description; private List fields = new ArrayList<>(); private TypeResolver typeResolver; + private InterfaceTypeDefinition definition; public Builder name(String name) { @@ -93,6 +105,11 @@ public Builder description(String description) { return this; } + public Builder definition(InterfaceTypeDefinition definition) { + this.definition = definition; + return this; + } + public Builder field(GraphQLFieldDefinition fieldDefinition) { fields.add(fieldDefinition); return this; @@ -141,7 +158,7 @@ public Builder typeResolver(TypeResolver typeResolver) { } public GraphQLInterfaceType build() { - return new GraphQLInterfaceType(name, description, fields, typeResolver); + return new GraphQLInterfaceType(name, description, fields, typeResolver, definition); } @@ -149,7 +166,7 @@ public GraphQLInterfaceType build() { private static class Reference extends GraphQLInterfaceType implements TypeReference { private Reference(String name) { - super(name, "", Collections.emptyList(), new TypeResolverProxy()); + super(name, "", Collections.emptyList(), new TypeResolverProxy()); } } } diff --git a/src/main/java/graphql/schema/GraphQLObjectType.java b/src/main/java/graphql/schema/GraphQLObjectType.java index 57a0e8d5c4..ca0652d28d 100644 --- a/src/main/java/graphql/schema/GraphQLObjectType.java +++ b/src/main/java/graphql/schema/GraphQLObjectType.java @@ -1,13 +1,14 @@ package graphql.schema; +import graphql.AssertException; +import graphql.language.ObjectTypeDefinition; + import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import graphql.AssertException; - import static graphql.Assert.assertNotNull; import static graphql.Assert.assertValidName; @@ -17,16 +18,23 @@ public class GraphQLObjectType implements GraphQLType, GraphQLOutputType, GraphQ private final String description; private final Map fieldDefinitionsByName = new LinkedHashMap<>(); private final List interfaces = new ArrayList<>(); + private final ObjectTypeDefinition definition; public GraphQLObjectType(String name, String description, List fieldDefinitions, List interfaces) { - assertValidName(name); + this(name, description, fieldDefinitions, interfaces, null); + } + + public GraphQLObjectType(String name, String description, List fieldDefinitions, + List interfaces, ObjectTypeDefinition definition) { + assertValidName(name); assertNotNull(fieldDefinitions, "fieldDefinitions can't be null"); assertNotNull(interfaces, "interfaces can't be null"); assertNotNull(interfaces, "unresolvedInterfaces can't be null"); this.name = name; this.description = description; this.interfaces.addAll(interfaces); + this.definition = definition; buildDefinitionMap(fieldDefinitions); } @@ -71,6 +79,9 @@ public String getName() { return name; } + public ObjectTypeDefinition getDefinition() { + return definition; + } @Override public String toString() { @@ -89,12 +100,13 @@ public static Builder newObject() { public static Reference reference(String name) { return new Reference(name); } - + public static class Builder { private String name; private String description; private List fieldDefinitions = new ArrayList<>(); private List interfaces = new ArrayList<>(); + private ObjectTypeDefinition definition; public Builder name(String name) { this.name = name; @@ -106,6 +118,11 @@ public Builder description(String description) { return this; } + public Builder definition(ObjectTypeDefinition definition) { + this.definition = definition; + return this; + } + public Builder field(GraphQLFieldDefinition fieldDefinition) { assertNotNull(fieldDefinition, "fieldDefinition can't be null"); this.fieldDefinitions.add(fieldDefinition); @@ -163,14 +180,14 @@ public Builder withInterfaces(GraphQLInterfaceType... interfaceType) { } public GraphQLObjectType build() { - return new GraphQLObjectType(name, description, fieldDefinitions, interfaces); + return new GraphQLObjectType(name, description, fieldDefinitions, interfaces, definition); } } private static class Reference extends GraphQLObjectType implements TypeReference { private Reference(String name) { - super(name, "", Collections.emptyList(), Collections.emptyList()); + super(name, "", Collections.emptyList(), Collections.emptyList(), null); } } } diff --git a/src/main/java/graphql/schema/GraphQLScalarType.java b/src/main/java/graphql/schema/GraphQLScalarType.java index ef67c70538..8b7a17e49d 100644 --- a/src/main/java/graphql/schema/GraphQLScalarType.java +++ b/src/main/java/graphql/schema/GraphQLScalarType.java @@ -1,6 +1,8 @@ package graphql.schema; +import graphql.language.ScalarTypeDefinition; + import static graphql.Assert.assertNotNull; import static graphql.Assert.assertValidName; @@ -9,14 +11,19 @@ public class GraphQLScalarType implements GraphQLType, GraphQLInputType, GraphQL private final String name; private final String description; private final Coercing coercing; - + private final ScalarTypeDefinition definition; public GraphQLScalarType(String name, String description, Coercing coercing) { - assertValidName(name); + this(name,description,coercing,null); + } + + public GraphQLScalarType(String name, String description, Coercing coercing, ScalarTypeDefinition definition) { + assertValidName(name); assertNotNull(coercing, "coercing can't be null"); this.name = name; this.description = description; this.coercing = coercing; + this.definition = definition; } public String getName() { @@ -33,6 +40,10 @@ public Coercing getCoercing() { return coercing; } + public ScalarTypeDefinition getDefinition() { + return definition; + } + @Override public String toString() { return "GraphQLScalarType{" + diff --git a/src/main/java/graphql/schema/GraphQLUnionType.java b/src/main/java/graphql/schema/GraphQLUnionType.java index d51f301f67..6fc433f226 100644 --- a/src/main/java/graphql/schema/GraphQLUnionType.java +++ b/src/main/java/graphql/schema/GraphQLUnionType.java @@ -1,11 +1,15 @@ package graphql.schema; +import graphql.language.UnionTypeDefinition; + import java.util.ArrayList; import java.util.List; import java.util.Map; -import static graphql.Assert.*; +import static graphql.Assert.assertNotEmpty; +import static graphql.Assert.assertNotNull; +import static graphql.Assert.assertValidName; public class GraphQLUnionType implements GraphQLType, GraphQLOutputType, GraphQLCompositeType, GraphQLUnmodifiedType, GraphQLNullableType { @@ -13,10 +17,15 @@ public class GraphQLUnionType implements GraphQLType, GraphQLOutputType, GraphQL private final String description; private final List types = new ArrayList<>(); private final TypeResolver typeResolver; + private final UnionTypeDefinition definition; public GraphQLUnionType(String name, String description, List types, TypeResolver typeResolver) { - assertValidName(name); + this(name, description, types, typeResolver, null); + } + + public GraphQLUnionType(String name, String description, List types, TypeResolver typeResolver, UnionTypeDefinition definition) { + assertValidName(name); assertNotNull(types, "types can't be null"); assertNotEmpty(types, "A Union type must define one or more member types."); assertNotNull(typeResolver, "typeResolver can't be null"); @@ -24,6 +33,7 @@ public GraphQLUnionType(String name, String description, List this.description = description; this.types.addAll(types); this.typeResolver = typeResolver; + this.definition = definition; } void replaceTypeReferences(Map typeMap) { @@ -52,6 +62,10 @@ public String getDescription() { return description; } + public UnionTypeDefinition getDefinition() { + return definition; + } + public static Builder newUnionType() { return new Builder(); } @@ -61,6 +75,7 @@ public static class Builder { private String description; private List types = new ArrayList<>(); private TypeResolver typeResolver; + private UnionTypeDefinition definition; public Builder name(String name) { this.name = name; @@ -72,6 +87,11 @@ public Builder description(String description) { return this; } + public Builder definition(UnionTypeDefinition definition) { + this.definition = definition; + return this; + } + public Builder typeResolver(TypeResolver typeResolver) { this.typeResolver = typeResolver; @@ -93,7 +113,7 @@ public Builder possibleTypes(GraphQLObjectType... type) { } public GraphQLUnionType build() { - return new GraphQLUnionType(name, description, types, typeResolver); + return new GraphQLUnionType(name, description, types, typeResolver, definition); } } }