use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class FunWithStringsSchemaFactory method createSchema.
GraphQLSchema createSchema() {
GraphQLObjectType stringObjectType = newObject().name("StringObject").field(newFieldDefinition().name("value").type(GraphQLString).dataFetcher(stringObjectValueFetcher)).field(newFieldDefinition().name("nonNullValue").type(new GraphQLNonNull(GraphQLString)).dataFetcher(stringObjectValueFetcher)).field(newFieldDefinition().name("veryNonNullValue").type(new GraphQLNonNull(new GraphQLNonNull(GraphQLString))).dataFetcher(stringObjectValueFetcher)).field(newFieldDefinition().name("throwException").type(GraphQLString).dataFetcher(throwExceptionFetcher)).field(newFieldDefinition().name("returnBadList").type(GraphQLString).dataFetcher(returnBadListFetcher)).field(newFieldDefinition().name("anyIterable").type(new GraphQLList(GraphQLString)).dataFetcher(anyIterableFetcher)).field(newFieldDefinition().name("shatter").type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("StringObject"))))).dataFetcher(shatterFetcher)).field(newFieldDefinition().name("wordsAndLetters").type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(new GraphQLNonNull(new GraphQLTypeReference("StringObject")))))))).dataFetcher(wordsAndLettersFetcher)).field(newFieldDefinition().name("split").description("String#split(regex) but replace empty strings with nulls to help us test null behavior in lists").type(new GraphQLList(new GraphQLTypeReference("StringObject"))).argument(GraphQLArgument.newArgument().name("regex").type(GraphQLString)).dataFetcher(splitFetcher)).field(newFieldDefinition().name("splitNotNull").description("String#split(regex) but replace empty strings with nulls to help us test null behavior in lists").type(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference("StringObject")))).argument(GraphQLArgument.newArgument().name("regex").type(GraphQLString)).dataFetcher(splitFetcher)).field(newFieldDefinition().name("append").type(new GraphQLTypeReference("StringObject")).argument(GraphQLArgument.newArgument().name("text").type(GraphQLString)).dataFetcher(appendFetcher)).field(newFieldDefinition().name("emptyOptional").type(GraphQLString).argument(GraphQLArgument.newArgument().name("text").type(GraphQLString)).dataFetcher(emptyOptionalFetcher)).field(newFieldDefinition().name("optional").type(GraphQLString).argument(GraphQLArgument.newArgument().name("text").type(GraphQLString)).dataFetcher(optionalFetcher)).field(newFieldDefinition().name("completableFuture").type(GraphQLString).argument(GraphQLArgument.newArgument().name("text").type(GraphQLString)).dataFetcher(completableFutureFetcher)).build();
GraphQLEnumType enumDayType = newEnum().name("Day").value("MONDAY").value("TUESDAY").description("Day of the week").build();
GraphQLObjectType simpleObjectType = newObject().name("SimpleObject").field(newFieldDefinition().name("value").type(GraphQLString)).build();
GraphQLInterfaceType interfaceType = newInterface().name("InterfaceType").field(newFieldDefinition().name("value").type(GraphQLString)).typeResolver(env -> {
// always this for testing
return simpleObjectType;
}).build();
GraphQLObjectType queryType = newObject().name("StringQuery").field(newFieldDefinition().name("string").type(stringObjectType).argument(GraphQLArgument.newArgument().name("value").type(GraphQLString)).dataFetcher(env -> env.getArgument("value"))).field(newFieldDefinition().name("interface").type(interfaceType).argument(GraphQLArgument.newArgument().name("value").type(GraphQLString)).dataFetcher(env -> CompletableFuture.completedFuture(new SimpleObject()))).field(newFieldDefinition().name("nullEnum").type(enumDayType).dataFetcher(env -> null)).build();
return GraphQLSchema.newSchema().query(queryType).build();
}
use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class SchemaPrinter method typeString.
String typeString(GraphQLType rawType) {
StringBuilder sb = new StringBuilder();
Stack<String> stack = new Stack<>();
GraphQLType type = rawType;
while (true) {
if (type instanceof GraphQLNonNull) {
type = ((GraphQLNonNull) type).getWrappedType();
stack.push("!");
} else if (type instanceof GraphQLList) {
type = ((GraphQLList) type).getWrappedType();
sb.append("[");
stack.push("]");
} else {
sb.append(type.getName());
break;
}
}
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.toString();
}
use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class TypeInfo method decorate.
/**
* This will decorate a graphql type with the original hierarchy of non null and list'ness
* it originally contained in its definition type
*
* @param objectType this should be a graphql type that was originally built from this raw type
* @param <T> the type
*
* @return the decorated type
*/
public <T extends GraphQLType> T decorate(GraphQLType objectType) {
GraphQLType out = objectType;
Stack<Class<?>> wrappingStack = new Stack<>();
wrappingStack.addAll(this.decoration);
while (!wrappingStack.isEmpty()) {
Class<?> clazz = wrappingStack.pop();
if (clazz.equals(NonNullType.class)) {
out = new GraphQLNonNull(out);
}
if (clazz.equals(ListType.class)) {
out = new GraphQLList(out);
}
}
// noinspection unchecked
return (T) out;
}
use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class VariableDefaultValuesOfCorrectType method checkVariableDefinition.
@Override
public void checkVariableDefinition(VariableDefinition variableDefinition) {
GraphQLInputType inputType = getValidationContext().getInputType();
if (inputType == null)
return;
if (inputType instanceof GraphQLNonNull && variableDefinition.getDefaultValue() != null) {
String message = "Missing value for non null type";
addError(ValidationErrorType.DefaultForNonNullArgument, variableDefinition.getSourceLocation(), message);
}
if (variableDefinition.getDefaultValue() != null && !getValidationUtil().isValidLiteralValue(variableDefinition.getDefaultValue(), inputType, getValidationContext().getSchema())) {
String message = String.format("Bad default value %s for type %s", variableDefinition.getDefaultValue(), inputType.getName());
addError(ValidationErrorType.BadValueForDefaultArg, variableDefinition.getSourceLocation(), message);
}
}
use of graphql.schema.GraphQLNonNull in project graphql-java by graphql-java.
the class NoUnbrokenInputCycles method check.
private void check(GraphQLInputObjectType type, Set<GraphQLType> seen, List<String> path, SchemaValidationErrorCollector validationErrorCollector) {
if (seen.contains(type)) {
validationErrorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.UnbrokenInputCycle, getErrorMessage(path)));
return;
}
seen.add(type);
for (GraphQLInputObjectField field : type.getFieldDefinitions()) {
if (field.getType() instanceof GraphQLNonNull) {
GraphQLType unwrapped = unwrapNonNull((GraphQLNonNull) field.getType());
if (unwrapped instanceof GraphQLInputObjectType) {
path = new ArrayList<>(path);
path.add(field.getName() + "!");
check((GraphQLInputObjectType) unwrapped, new HashSet<>(seen), path, validationErrorCollector);
}
}
}
}
Aggregations