graphql.schema.GraphQLNonNull Java Examples

The following examples show how to use graphql.schema.GraphQLNonNull. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
private GraphQLInputType createGraphQLInputType(Field field) {

        GraphQLInputType graphQLInputType = referenceGraphQLInputType(field);

        // Collection
        if (field.hasArray()) {
            Array array = field.getArray();
            // Mandatory in the collection
            if (array.isNotEmpty()) {
                graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
            }
            // Collection depth
            for (int i = 0; i < array.getDepth(); i++) {
                graphQLInputType = GraphQLList.list(graphQLInputType);
            }
        }

        // Mandatory
        if (field.isNotNull()) {
            graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
        }

        return graphQLInputType;
    }
 
Example #2
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
private GraphQLOutputType createGraphQLOutputType(Field field) {
    GraphQLOutputType graphQLOutputType = referenceGraphQLOutputType(field);

    // Collection
    if (field.hasArray()) {
        Array array = field.getArray();
        // Mandatory in the collection
        if (array.isNotEmpty()) {
            graphQLOutputType = GraphQLNonNull.nonNull(graphQLOutputType);
        }
        // Collection depth
        for (int i = 0; i < array.getDepth(); i++) {
            graphQLOutputType = GraphQLList.list(graphQLOutputType);
        }
    }

    // Mandatory
    if (field.isNotNull()) {
        graphQLOutputType = GraphQLNonNull.nonNull(graphQLOutputType);
    }

    return graphQLOutputType;
}
 
Example #3
Source File: RelayImpl.java    From glitr with MIT License 6 votes vote down vote up
@Override
public GraphQLObjectType connectionType(String name, GraphQLObjectType edgeType, List<GraphQLFieldDefinition> connectionFields) {
    return newObject()
            .name(name + "Connection")
            .description("A connection to a list of items.")
            .field(newFieldDefinition()
                    .name("edges")
                    .type(new GraphQLConnectionList(edgeType))
                    .build())
            .field(newFieldDefinition()
                    .name("pageInfo")
                    .type(new GraphQLNonNull(pageInfoType))
                    .definition(new GlitrFieldDefinition(name, Sets.newHashSet(new GlitrMetaDefinition(COMPLEXITY_IGNORE_KEY, true))))
                    .build())
            .fields(connectionFields)
            .build();
}
 
Example #4
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public GraphQLArgument transformArgument(GraphQLArgument argument, DirectiveArgument directiveArgument, OperationMapper operationMapper, BuildContext buildContext) {
    if (directiveArgument.getAnnotation() != null && directiveArgument.getDefaultValue() == null) {
        return argument.transform(builder -> builder.type(GraphQLNonNull.nonNull(argument.getType())));
    }
    return transformArgument(argument, directiveArgument.getTypedElement(), directiveArgument.toString(), operationMapper, buildContext);
}
 
Example #5
Source File: SchemaIDLUtil.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Provides the IDL string version of a type including handling of types wrapped in non-null/list-types
 */
public static String typeString(GraphQLType rawType) {

    final StringBuilder sb = new StringBuilder();
    final 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 if (type instanceof GraphQLUnmodifiedType) {
            sb.append(((GraphQLUnmodifiedType) type).getName());
            break;
        } else {
            sb.append(type.toString());
            break;
        }
    }
    while (!stack.isEmpty()) {
        sb.append(stack.pop());
    }

    return sb.toString();

}
 
Example #6
Source File: MethodDataFetcher.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private boolean isBooleanMethod(GraphQLOutputType outputType) {
    if (outputType == GraphQLBoolean) return true;
    if (outputType instanceof GraphQLNonNull) {
        return ((GraphQLNonNull) outputType).getWrappedType() == GraphQLBoolean;
    }
    return false;
}
 
Example #7
Source File: MapToListTypeAdapter.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    return new GraphQLList(new GraphQLNonNull(
            mapEntry(
                    //MapEntry fields are artificial - no Java element is backing them
                    env.forElement(null).toGraphQLInputType(getElementType(javaType, 0)),
                    env.forElement(null).toGraphQLInputType(getElementType(javaType, 1)), env.buildContext)));
}
 
Example #8
Source File: MapToListTypeAdapter.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public GraphQLOutputType toGraphQLType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    return new GraphQLList(new GraphQLNonNull(
            mapEntry(
                    //MapEntry fields are artificial - no Java element is backing them
                    env.forElement(null).toGraphQLType(getElementType(javaType, 0)),
                    env.forElement(null).toGraphQLType(getElementType(javaType, 1)), env.buildContext)));
}
 
Example #9
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private GraphQLArgument transformArgument(GraphQLArgument argument, TypedElement element, String description, OperationMapper operationMapper, BuildContext buildContext) {
    if (argument.getDefaultValue() == null && shouldWrap(argument.getType(), element)) {
        return argument.transform(builder -> builder.type(new GraphQLNonNull(argument.getType())));
    }
    if (shouldUnwrap(argument.getDefaultValue(), argument.getType())) {
        //do not warn on primitives as their non-nullness is implicit
        if (!ClassUtils.getRawType(element.getJavaType().getType()).isPrimitive()) {
            log.warn("Non-null argument with a default value will be treated as nullable: " + description);
        }
        return argument.transform(builder -> builder.type((GraphQLInputType) GraphQLUtils.unwrapNonNull(argument.getType())));
    }
    return argument;
}
 
Example #10
Source File: _Entity.java    From federation-jvm with MIT License 5 votes vote down vote up
static GraphQLFieldDefinition field(@NotNull Set<String> typeNames) {
    return newFieldDefinition()
            .name(fieldName)
            .argument(newArgument()
                    .name(argumentName)
                    .type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(new GraphQLTypeReference(_Any.typeName)))))
                    .build())
            .type(new GraphQLNonNull(
                            new GraphQLList(
                                    GraphQLUnionType.newUnionType()
                                            .name(typeName)
                                            .possibleTypes(typeNames.stream()
                                                    .map(GraphQLTypeReference::new)
                                                    .toArray(GraphQLTypeReference[]::new))
                                            .build()
                            )
                    )
            )
            .build();
}
 
Example #11
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public GraphQLInputObjectField transformInputField(GraphQLInputObjectField field, InputField inputField, OperationMapper operationMapper, BuildContext buildContext) {
    if (field.getDefaultValue() == null && shouldWrap(field.getType(), inputField.getTypedElement())) {
        return field.transform(builder -> builder.type(new GraphQLNonNull(field.getType())));
    }
    if (shouldUnwrap(field.getDefaultValue(), field.getType())) {
        //do not warn on primitives as their non-nullness is implicit
        if (!ClassUtils.getRawType(inputField.getJavaType().getType()).isPrimitive()) {
            log.warn("Non-null input field with a default value will be treated as nullable: " + inputField);
        }
        return field.transform(builder -> builder.type((GraphQLInputType) GraphQLUtils.unwrapNonNull(field.getType())));
    }
    return field;
}
 
Example #12
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public GraphQLFieldDefinition transformField(GraphQLFieldDefinition field, Operation operation, OperationMapper operationMapper, BuildContext buildContext) {
    if (shouldWrap(field.getType(), operation.getTypedElement())) {
        return field.transform(builder -> builder.type(new GraphQLNonNull(field.getType())));
    }
    return field;
}
 
Example #13
Source File: NameHelper.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
public static String getName(GraphQLType graphQLType) {
    if (graphQLType instanceof GraphQLNamedType) {
        return ((GraphQLNamedType) graphQLType).getName();
    } else if (graphQLType instanceof GraphQLNonNull) {
        return getName(((GraphQLNonNull) graphQLType).getWrappedType());
    } else if (graphQLType instanceof GraphQLList) {
        return getName(((GraphQLList) graphQLType).getWrappedType());
    }
    return EMPTY;
}
 
Example #14
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public NonNullMapper() {
    Set<Class<? extends Annotation>> annotations = new HashSet<>();
    annotations.add(io.leangen.graphql.annotations.GraphQLNonNull.class);
    for (String additional : COMMON_NON_NULL_ANNOTATIONS) {
        try {
            annotations.add((Class<? extends Annotation>) ClassUtils.forName(additional));
        } catch (ClassNotFoundException e) {
            /*no-op*/
        }
    }
    this.nonNullAnnotations = Collections.unmodifiableSet(annotations);
}
 
Example #15
Source File: RejoinerIntegrationTest.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
@Test
public void schemaShouldListSync() {
  GraphQLOutputType listOfStuff =
      schema.getQueryType().getFieldDefinition("listOfStuffSync").getType();
  assertThat(listOfStuff).isInstanceOf(GraphQLList.class);
  assertThat(((GraphQLList) listOfStuff).getWrappedType()).isInstanceOf(GraphQLNonNull.class);
}
 
Example #16
Source File: RejoinerIntegrationTest.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
@Test
public void schemaShouldList() {
  GraphQLOutputType listOfStuff =
      schema.getQueryType().getFieldDefinition("listOfStuff").getType();
  assertThat(listOfStuff).isInstanceOf(GraphQLList.class);
  assertThat(((GraphQLList) listOfStuff).getWrappedType()).isInstanceOf(GraphQLNonNull.class);
}
 
Example #17
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private GraphQLArgument createGraphQLArgument(Argument argument) {
    GraphQLArgument.Builder argumentBuilder = GraphQLArgument.newArgument()
            .name(argument.getName())
            .description(argument.getDescription())
            .defaultValue(sanitizeDefaultValue(argument));

    GraphQLInputType graphQLInputType = referenceGraphQLInputType(argument);

    // Collection
    if (argument.hasArray()) {
        Array array = argument.getArray();
        // Mandatory in the collection
        if (array.isNotEmpty()) {
            graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
        }
        // Collection depth
        for (int i = 0; i < array.getDepth(); i++) {
            graphQLInputType = GraphQLList.list(graphQLInputType);
        }
    }

    // Mandatory
    if (argument.isNotNull()) {
        graphQLInputType = GraphQLNonNull.nonNull(graphQLInputType);
    }

    argumentBuilder = argumentBuilder.type(graphQLInputType);

    return argumentBuilder.build();

}
 
Example #18
Source File: DefaultValueSchemaTransformer.java    From graphql-spqr-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
default GraphQLArgument transformArgument(GraphQLArgument argument, OperationArgument operationArgument, OperationMapper operationMapper, BuildContext buildContext) {
    if (supports(operationArgument.getJavaType()) && !(argument.getType() instanceof GraphQLNonNull) && argument.getDefaultValue() == null) {
        return argument.transform(builder -> builder.defaultValue(getDefaultValue()));
    }
    return argument;
}
 
Example #19
Source File: Fields.java    From spring-boot-starter-graphql with Apache License 2.0 5 votes vote down vote up
public static Builder notNull (Builder aSource) {
  GraphQLFieldDefinition field = aSource.build();
  return create(field.getName()).argument(field.getArguments())
                                .type(new GraphQLNonNull(field.getType()))
                                .dataFetcher(field.getDataFetcher())
                                .definition(field.getDefinition())
                                .deprecate(field.getDeprecationReason())
                                .description(field.getDescription());
}
 
Example #20
Source File: Arguments.java    From spring-boot-starter-graphql with Apache License 2.0 5 votes vote down vote up
public static GraphQLArgument.Builder notNull (GraphQLArgument.Builder aBuilder) {
  GraphQLArgument arg = aBuilder.build();
  return GraphQLArgument.newArgument()
                        .name(arg.getName())
                        .defaultValue(arg.getDefaultValue())
                        .definition(arg.getDefinition())
                        .description(arg.getDescription())
                        .type(new GraphQLNonNull(arg.getType()));
}
 
Example #21
Source File: GraphQLUtils.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static GraphQLType unwrapNonNull(GraphQLType type) {
    while (type instanceof GraphQLNonNull) {
        type = ((GraphQLNonNull) type).getWrappedType();
    }
    return type;
}
 
Example #22
Source File: GraphQLClientTest.java    From vertx-graphql-service-discovery with Apache License 2.0 4 votes vote down vote up
@Test
public void testSchemaValidation(TestContext context) {
	
       Async async = context.async();

	GraphQLObjectType droidType = GraphQLObjectType.newObject()
            .name("Droid")
            .description("A mechanical creature in the Star Wars universe.")
            .field(GraphQLFieldDefinition.newFieldDefinition()
                    .name("id")
                    .description("The id of the droid.")
                    .type(new GraphQLNonNull(Scalars.GraphQLString))
                    .build()).build();
	
	
	GraphQLObjectType query = GraphQLObjectType.newObject()
            .name("DroidQueries")
            .field(GraphQLFieldDefinition.newFieldDefinition()
                    .name("droid")
                    .type(droidType)
                    .argument(GraphQLArgument.newArgument()
                            .name("id")
                            .description("id of the droid")
                            .type(new GraphQLNonNull(Scalars.GraphQLString))
                            .build())
                    .build())
            .build();
	
	GraphQLSchema schema = GraphQLSchema.newSchema()
            .query(query)
            .build();
	
	String DROIDS_VARIABLES_QUERY =
            "        query GetDroidNameR2($id: String!) {\n" +
            "            droid(id: $id) {\n" +
            "                id\n" +
            "            }\n" +
            "        }";
	
	GraphQL graphQL = new GraphQL.Builder(schema).build();
	
	ExecutionInput.Builder asyncExecBuilder = ExecutionInput.newExecutionInput().query(DROIDS_VARIABLES_QUERY);
	
	HashMap<String, Object> variables = new HashMap<String, Object>();
	variables.put("nonsense", "xxx");
	
	asyncExecBuilder.variables(variables);
               
	try {
       CompletableFuture<ExecutionResult> promise = graphQL.executeAsync(asyncExecBuilder.build());
       
       promise.thenAccept(new Consumer<ExecutionResult>() {

		@Override
		public void accept(ExecutionResult result) {
			context.assertFalse(true);
			
			async.complete();
			

	}});
	} catch (Exception e ) {
		context.assertTrue(true);
		async.complete();
	}
}
 
Example #23
Source File: GraphQLTypeAssertions.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static void assertListOfNonNull(GraphQLType wrapperType, Class<? extends GraphQLModifiedType> wrappedTypeClass) {
    assertEquals(wrapperType.getClass(), GraphQLList.class);
    assertEquals(((GraphQLList) wrapperType).getWrappedType().getClass(), GraphQLNonNull.class);
    assertEquals(((GraphQLNonNull) (((GraphQLList) wrapperType).getWrappedType())).getWrappedType().getClass(), wrappedTypeClass);
}
 
Example #24
Source File: GraphQLTypeAssertions.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static void assertListOfNonNull(GraphQLType wrapperType, GraphQLType wrappedType) {
    assertEquals(wrapperType.getClass(), GraphQLList.class);
    assertEquals(((GraphQLList) wrapperType).getWrappedType().getClass(), GraphQLNonNull.class);
    assertEquals(((GraphQLNonNull) (((GraphQLList) wrapperType).getWrappedType())).getWrappedType(), wrappedType);
}
 
Example #25
Source File: GraphQLTypeAssertions.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static void assertNonNull(GraphQLType wrapperType, Class<? extends GraphQLType> wrappedTypeClass) {
    assertEquals(GraphQLNonNull.class, wrapperType.getClass());
    assertEquals(((GraphQLNonNull) wrapperType).getWrappedType().getClass(), wrappedTypeClass);
}
 
Example #26
Source File: GraphQLTypeAssertions.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static void assertNonNull(GraphQLType wrapperType, GraphQLType wrappedType) {
    assertEquals(GraphQLNonNull.class, wrapperType.getClass());
    assertEquals(((GraphQLNonNull) wrapperType).getWrappedType(), wrappedType);
}
 
Example #27
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public GraphQLNonNull toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    mappersToSkip.add(this.getClass());
    GraphQLInputType inner = env.operationMapper.toGraphQLInputType(javaType, mappersToSkip, env);
    return inner instanceof GraphQLNonNull ? (GraphQLNonNull) inner : new GraphQLNonNull(inner);
}
 
Example #28
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
private boolean shouldUnwrap(Object defaultValue, GraphQLType type) {
    return defaultValue != null && type instanceof GraphQLNonNull;
}
 
Example #29
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
private boolean shouldWrap(GraphQLType type, TypedElement typedElement) {
    return !(type instanceof GraphQLNonNull) && nonNullAnnotations.stream().anyMatch(typedElement::isAnnotationPresent);
}
 
Example #30
Source File: NonNullMapper.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public GraphQLNonNull toGraphQLType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    mappersToSkip.add(this.getClass());
    GraphQLOutputType inner = env.operationMapper.toGraphQLType(javaType, mappersToSkip, env);
    return inner instanceof GraphQLNonNull ? (GraphQLNonNull) inner : new GraphQLNonNull(inner);
}