Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,17 @@ GraphQLInputObjectType inputObjectType = newInputObject()

`GraphQLList` and `GraphQLNonNull` wrap another type to declare a list or to forbid null values.

There are no builders to create new objects. Just normal constructors, because they are so simple.

Example:

```java
new GraphQLList(GraphQLString); // a list of Strings
GraphQLList.list((GraphQLString); // a list of Strings

GraphQLNonNull.nonNull(GraphQLString); // a non null String

new GraphQLNonNull(GraphQLString); // a non null String
// with static imports its even shorter
newArgument()
.name("example")
.type(nonNull(list(GraphQLString)));

```

Expand Down
90 changes: 55 additions & 35 deletions src/main/java/graphql/introspection/Introspection.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package graphql.introspection;


import graphql.schema.*;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLEnumValueDefinition;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLTypeReference;
import graphql.schema.GraphQLUnionType;
import graphql.schema.SchemaUtil;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,6 +28,8 @@
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLList.list;
import static graphql.schema.GraphQLNonNull.nonNull;
import static graphql.schema.GraphQLObjectType.newObject;

public class Introspection {
Expand Down Expand Up @@ -68,24 +88,24 @@ public Object get(DataFetchingEnvironment environment) {
.name("__InputValue")
.field(newFieldDefinition()
.name("name")
.type(new GraphQLNonNull(GraphQLString)))
.type(nonNull(GraphQLString)))
.field(newFieldDefinition()
.name("description")
.type(GraphQLString))
.field(newFieldDefinition()
.name("type")
.type(new GraphQLNonNull(new GraphQLTypeReference("__Type"))))
.type(nonNull(new GraphQLTypeReference("__Type"))))
.field(newFieldDefinition()
.name("defaultValue")
.type(GraphQLString)
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
if (environment.getSource() instanceof GraphQLArgument) {
GraphQLArgument inputField = (GraphQLArgument) environment.getSource();
GraphQLArgument inputField = environment.getSource();
return inputField.getDefaultValue() != null ? inputField.getDefaultValue().toString() : null;
} else if (environment.getSource() instanceof GraphQLInputObjectField) {
GraphQLInputObjectField inputField = (GraphQLInputObjectField) environment.getSource();
GraphQLInputObjectField inputField = environment.getSource();
return inputField.getDefaultValue() != null ? inputField.getDefaultValue().toString() : null;
}
return null;
Expand All @@ -98,13 +118,13 @@ public Object get(DataFetchingEnvironment environment) {
.name("__Field")
.field(newFieldDefinition()
.name("name")
.type(new GraphQLNonNull(GraphQLString)))
.type(nonNull(GraphQLString)))
.field(newFieldDefinition()
.name("description")
.type(GraphQLString))
.field(newFieldDefinition()
.name("args")
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__InputValue))))
.type(nonNull(list(nonNull(__InputValue))))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
Expand All @@ -114,10 +134,10 @@ public Object get(DataFetchingEnvironment environment) {
}))
.field(newFieldDefinition()
.name("type")
.type(new GraphQLNonNull(new GraphQLTypeReference("__Type"))))
.type(nonNull(new GraphQLTypeReference("__Type"))))
.field(newFieldDefinition()
.name("isDeprecated")
.type(new GraphQLNonNull(GraphQLBoolean))
.type(nonNull(GraphQLBoolean))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
Expand All @@ -135,17 +155,17 @@ public Object get(DataFetchingEnvironment environment) {
.name("__EnumValue")
.field(newFieldDefinition()
.name("name")
.type(new GraphQLNonNull(GraphQLString)))
.type(nonNull(GraphQLString)))
.field(newFieldDefinition()
.name("description")
.type(GraphQLString))
.field(newFieldDefinition()
.name("isDeprecated")
.type(new GraphQLNonNull(GraphQLBoolean))
.type(nonNull(GraphQLBoolean))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLEnumValueDefinition enumValue = (GraphQLEnumValueDefinition) environment.getSource();
GraphQLEnumValueDefinition enumValue = environment.getSource();
return enumValue.isDeprecated();
}
}))
Expand Down Expand Up @@ -246,7 +266,7 @@ public Object get(DataFetchingEnvironment environment) {
.name("__Type")
.field(newFieldDefinition()
.name("kind")
.type(new GraphQLNonNull(__TypeKind))
.type(nonNull(__TypeKind))
.dataFetcher(kindDataFetcher))
.field(newFieldDefinition()
.name("name")
Expand All @@ -256,31 +276,31 @@ public Object get(DataFetchingEnvironment environment) {
.type(GraphQLString))
.field(newFieldDefinition()
.name("fields")
.type(new GraphQLList(new GraphQLNonNull(__Field)))
.type(list(nonNull(__Field)))
.argument(newArgument()
.name("includeDeprecated")
.type(GraphQLBoolean)
.defaultValue(false))
.dataFetcher(fieldsFetcher))
.field(newFieldDefinition()
.name("interfaces")
.type(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("__Type"))))
.type(list(nonNull(new GraphQLTypeReference("__Type"))))
.dataFetcher(interfacesFetcher))
.field(newFieldDefinition()
.name("possibleTypes")
.type(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("__Type"))))
.type(list(nonNull(new GraphQLTypeReference("__Type"))))
.dataFetcher(possibleTypesFetcher))
.field(newFieldDefinition()
.name("enumValues")
.type(new GraphQLList(new GraphQLNonNull(__EnumValue)))
.type(list(nonNull(__EnumValue)))
.argument(newArgument()
.name("includeDeprecated")
.type(GraphQLBoolean)
.defaultValue(false))
.dataFetcher(enumValuesTypesFetcher))
.field(newFieldDefinition()
.name("inputFields")
.type(new GraphQLList(new GraphQLNonNull(__InputValue)))
.type(list(nonNull(__InputValue)))
.dataFetcher(inputFieldsFetcher))
.field(newFieldDefinition()
.name("ofType")
Expand Down Expand Up @@ -318,21 +338,21 @@ public enum DirectiveLocation {
.type(GraphQLString))
.field(newFieldDefinition()
.name("locations")
.type(new GraphQLList(new GraphQLNonNull(__DirectiveLocation)))
.type(list(nonNull(__DirectiveLocation)))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
GraphQLDirective directive = environment.getSource();
return new ArrayList<DirectiveLocation>(directive.validLocations());
}
}))
.field(newFieldDefinition()
.name("args")
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__InputValue))))
.type(nonNull(list(nonNull(__InputValue))))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
GraphQLDirective directive = environment.getSource();
return directive.getArguments();
}
}))
Expand All @@ -343,7 +363,7 @@ public Object get(DataFetchingEnvironment environment) {
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
GraphQLDirective directive = environment.getSource();
return directive.isOnOperation();
}
}))
Expand All @@ -354,10 +374,10 @@ public Object get(DataFetchingEnvironment environment) {
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
GraphQLDirective directive = environment.getSource();
return directive.isOnFragment() ||
(directive.validLocations().contains(DirectiveLocation.INLINE_FRAGMENT)
&& directive.validLocations().contains(DirectiveLocation.FRAGMENT_SPREAD));
&& directive.validLocations().contains(DirectiveLocation.FRAGMENT_SPREAD));
}
}))
.field(newFieldDefinition()
Expand All @@ -367,7 +387,7 @@ public Object get(DataFetchingEnvironment environment) {
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLDirective directive = (GraphQLDirective) environment.getSource();
GraphQLDirective directive = environment.getSource();
return directive.isOnField() ||
directive.validLocations().contains(DirectiveLocation.FIELD);
}
Expand All @@ -382,22 +402,22 @@ public Object get(DataFetchingEnvironment environment) {
.field(newFieldDefinition()
.name("types")
.description("A list of all types supported by this server.")
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__Type))))
.type(nonNull(list(nonNull(__Type))))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLSchema schema = (GraphQLSchema) environment.getSource();
GraphQLSchema schema = environment.getSource();
return schema.getAllTypesAsList();
}
}))
.field(newFieldDefinition()
.name("queryType")
.description("The type that query operations will be rooted at.")
.type(new GraphQLNonNull(__Type))
.type(nonNull(__Type))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLSchema schema = (GraphQLSchema) environment.getSource();
GraphQLSchema schema = environment.getSource();
return schema.getQueryType();
}
}))
Expand All @@ -408,14 +428,14 @@ public Object get(DataFetchingEnvironment environment) {
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
GraphQLSchema schema = (GraphQLSchema) environment.getSource();
GraphQLSchema schema = environment.getSource();
return schema.getMutationType();
}
}))
.field(newFieldDefinition()
.name("directives")
.description("'A list of all directives supported by this server.")
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(__Directive))))
.type(nonNull(list(nonNull(__Directive))))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
Expand All @@ -438,7 +458,7 @@ public Object get(DataFetchingEnvironment environment) {

public static GraphQLFieldDefinition SchemaMetaFieldDef = newFieldDefinition()
.name("__schema")
.type(new GraphQLNonNull(__Schema))
.type(nonNull(__Schema))
.description("Access the current type schema of this server.")
.dataFetcher(new DataFetcher() {
@Override
Expand All @@ -453,7 +473,7 @@ public Object get(DataFetchingEnvironment environment) {
.description("Request the type information of a single type.")
.argument(newArgument()
.name("name")
.type(new GraphQLNonNull(GraphQLString)))
.type(nonNull(GraphQLString)))
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
Expand All @@ -464,7 +484,7 @@ public Object get(DataFetchingEnvironment environment) {

public static GraphQLFieldDefinition TypeNameMetaFieldDef = newFieldDefinition()
.name("__typename")
.type(new GraphQLNonNull(GraphQLString))
.type(nonNull(GraphQLString))
.description("The name of the current Object type at runtime.")
.dataFetcher(new DataFetcher() {
@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/graphql/schema/GraphQLList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

public class GraphQLList implements GraphQLType, GraphQLInputType, GraphQLOutputType, GraphQLModifiedType, GraphQLNullableType {

/**
* A factory method for creating list types so that when used with static imports allows
* more readable code such as
* {@code .type(list(GraphQLString)) }
*
* @param wrappedType the type to wrap as being a list
*
* @return a GraphQLList of that wrapped type
*/
public static GraphQLList list(GraphQLType wrappedType) {
return new GraphQLList(wrappedType);
}

private GraphQLType wrappedType;

public GraphQLList(GraphQLType wrappedType) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/graphql/schema/GraphQLNonNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@

public class GraphQLNonNull implements GraphQLType, GraphQLInputType, GraphQLOutputType, GraphQLModifiedType {

/**
* A factory method for creating non null types so that when used with static imports allows
* more readable code such as
* {@code .type(nonNull(GraphQLString)) }
*
* @param wrappedType the type to wrap as being non null
* @return a GraphQLNonNull of that wrapped type
*/
public static GraphQLNonNull nonNull(GraphQLType wrappedType) {
return new GraphQLNonNull(wrappedType);
}

private GraphQLType wrappedType;

public GraphQLNonNull(GraphQLType wrappedType) {
Expand Down
Loading