Search in sources :

Example 1 with GraphQLNonNull

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();
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLObjectType(graphql.schema.GraphQLObjectType) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Arrays(java.util.Arrays) GraphQLString(graphql.Scalars.GraphQLString) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) CompletableFuture(java.util.concurrent.CompletableFuture) GraphQLInterfaceType.newInterface(graphql.schema.GraphQLInterfaceType.newInterface) GraphQLArgument(graphql.schema.GraphQLArgument) ArrayList(java.util.ArrayList) GraphQLFieldDefinition.newFieldDefinition(graphql.schema.GraphQLFieldDefinition.newFieldDefinition) List(java.util.List) GraphQLList(graphql.schema.GraphQLList) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GraphQLEnumType.newEnum(graphql.schema.GraphQLEnumType.newEnum) Map(java.util.Map) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) Optional(java.util.Optional) GraphQLEnumType(graphql.schema.GraphQLEnumType) ArrayDeque(java.util.ArrayDeque) LinkedHashSet(java.util.LinkedHashSet) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType)

Example 2 with GraphQLNonNull

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();
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLType(graphql.schema.GraphQLType) GraphQLNonNull(graphql.schema.GraphQLNonNull) Stack(java.util.Stack)

Example 3 with GraphQLNonNull

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;
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLType(graphql.schema.GraphQLType) GraphQLNonNull(graphql.schema.GraphQLNonNull) Stack(java.util.Stack)

Example 4 with GraphQLNonNull

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);
    }
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLNonNull(graphql.schema.GraphQLNonNull)

Example 5 with GraphQLNonNull

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);
            }
        }
    }
}
Also used : GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLType(graphql.schema.GraphQLType)

Aggregations

GraphQLNonNull (graphql.schema.GraphQLNonNull)11 GraphQLList (graphql.schema.GraphQLList)6 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)4 GraphQLType (graphql.schema.GraphQLType)4 GraphQLArgument (graphql.schema.GraphQLArgument)3 GraphQLEnumType (graphql.schema.GraphQLEnumType)3 GraphQLObjectType (graphql.schema.GraphQLObjectType)3 Argument (graphql.language.Argument)2 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)2 GraphQLScalarType (graphql.schema.GraphQLScalarType)2 Stack (java.util.Stack)2 AssertException (graphql.AssertException)1 GraphQLString (graphql.Scalars.GraphQLString)1 ArrayValue (graphql.language.ArrayValue)1 EnumValue (graphql.language.EnumValue)1 NullValue (graphql.language.NullValue)1 ObjectValue (graphql.language.ObjectValue)1 DataFetcher (graphql.schema.DataFetcher)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 GraphQLDirective (graphql.schema.GraphQLDirective)1