io.leangen.graphql.annotations.GraphQLNonNull Java Examples

The following examples show how to use io.leangen.graphql.annotations.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: UnionTest.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@Test
public void testInlineUnion() {
    InlineUnionService unionService = new InlineUnionService();

    GraphQLSchema schema = new TestSchemaGenerator()
            .withTypeAdapters(new MapToListTypeAdapter())
            .withOperationsFromSingleton(unionService)
            .generate();

    GraphQLOutputType fieldType = schema.getQueryType().getFieldDefinition("union").getType();
    assertNonNull(fieldType, GraphQLList.class);
    GraphQLType list = ((graphql.schema.GraphQLNonNull) fieldType).getWrappedType();
    assertListOf(list, GraphQLList.class);
    GraphQLType map = ((GraphQLList) list).getWrappedType();
    assertMapOf(map, GraphQLUnionType.class, GraphQLUnionType.class);
    GraphQLObjectType entry = (GraphQLObjectType) GraphQLUtils.unwrap(map);
    GraphQLUnionType key = (GraphQLUnionType) entry.getFieldDefinition("key").getType();
    GraphQLNamedOutputType value = (GraphQLNamedOutputType) entry.getFieldDefinition("value").getType();
    assertEquals("Simple_One_Two", key.getName());
    assertEquals("nice", key.getDescription());
    assertEquals(value.getName(), "Education_Street");
    assertUnionOf(key, schema.getType("SimpleOne"), schema.getType("SimpleTwo"));
    assertUnionOf(value, schema.getType("Education"), schema.getType("Street"));
}
 
Example #2
Source File: ResolverBuilderTest.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@Test
public void typeMergeTest() {
    ResolverBuilder[] allBuilders = new ResolverBuilder[] {
            new PublicResolverBuilder(BASE_PACKAGES), new BeanResolverBuilder(BASE_PACKAGES), new AnnotatedResolverBuilder()};
    for(Collection<Resolver> resolvers : resolvers(new MergedTypes(), allBuilders)) {
        assertEquals(2, resolvers.size());

        Optional<AnnotatedType> field1 = resolvers.stream().filter(res -> "field1".equals(res.getOperationName())).findFirst()
                .map(Resolver::getReturnType);
        assertTrue(field1.isPresent());
        assertTrue(field1.get().isAnnotationPresent(GraphQLNonNull.class));

        Optional<AnnotatedType> field2 = resolvers.stream().filter(res -> "field2".equals(res.getOperationName())).findFirst()
                .map(Resolver::getReturnType);
        assertTrue(field2.isPresent());
        assertTrue(field2.get().isAnnotationPresent(GraphQLNonNull.class));
    }
}
 
Example #3
Source File: TypeInferenceTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaps() throws AnnotationFormatException {
    Annotation[] annotations = new Annotation[] {TypeFactory.annotation(GraphQLNonNull.class, Collections.emptyMap())};
    AnnotatedType nonNullLongType = GenericTypeReflector.annotate(Long.class, annotations);
    AnnotatedType doubleType = GenericTypeReflector.annotate(Double.class);
    AnnotatedType nonNullNumberType = TypeFactory.parameterizedAnnotatedClass(Number.class, annotations);
    AnnotatedType i1Type = GenericTypeReflector.annotate(I1.class);
    AnnotatedType i2Type = GenericTypeReflector.annotate(I2.class, annotations);
    AnnotatedType iType = GenericTypeReflector.annotate(I.class, annotations);
    AnnotatedType expected = TypeFactory.parameterizedAnnotatedClass(Map.class, annotations, nonNullNumberType, iType);
    AnnotatedType map1 = TypeFactory.parameterizedAnnotatedClass(Map.class, annotations, nonNullLongType, i1Type);
    AnnotatedType map2 = TypeFactory.parameterizedAnnotatedClass(Map.class, new Annotation[0], doubleType, i2Type);
    AnnotatedType inferred = ClassUtils.getCommonSuperType(Arrays.asList(map1, map2));
    assertTrue(GenericTypeReflector.equals(expected, inferred));
}
 
Example #4
Source File: TypeInferenceTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testClasses() throws AnnotationFormatException {
    Annotation[] nonNull = new Annotation[] {TypeFactory.annotation(Nonnull.class, Collections.emptyMap())};
    Annotation[] graphNonNull = new Annotation[] {TypeFactory.annotation(GraphQLNonNull.class, Collections.emptyMap())};
    Annotation[] mergedAnnotations = new Annotation[] {nonNull[0], graphNonNull[0]};
    AnnotatedType c1 = GenericTypeReflector.annotate(C1.class, nonNull);
    AnnotatedType c2 = GenericTypeReflector.annotate(C2.class, graphNonNull);
    AnnotatedType expected = GenericTypeReflector.annotate(P.class, mergedAnnotations);
    AnnotatedType inferred = ClassUtils.getCommonSuperType(Arrays.asList(c1, c2));
    assertTrue(GenericTypeReflector.equals(expected, inferred));
}
 
Example #5
Source File: InputFieldDiscoveryTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private void assertTypesMerged(Set<InputField> fields) {
    Optional<InputField> field1 = fields.stream().filter(field -> field.getName().equals("field1")).findFirst();
    Optional<InputField> field2 = fields.stream().filter(field -> field.getName().equals("field2")).findFirst();
    Optional<InputField> field3 = fields.stream().filter(field -> field.getName().equals("field3")).findFirst();
    assertTrue(field1.isPresent() && field2.isPresent() && field3.isPresent());
    AnnotatedType type1 = field1.get().getTypedElement().getJavaType();
    assertTrue(type1.isAnnotationPresent(GraphQLNonNull.class) && type1.isAnnotationPresent(GraphQLId.class));
    AnnotatedType type2 = field2.get().getTypedElement().getJavaType();
    assertTrue(type2.isAnnotationPresent(GraphQLNonNull.class) && type2.isAnnotationPresent(GraphQLId.class));
    AnnotatedType type3 = field3.get().getTypedElement().getJavaType();
    assertTrue(type3.isAnnotationPresent(GraphQLNonNull.class));
    AnnotatedType type31 = ((AnnotatedParameterizedType) type3).getAnnotatedActualTypeArguments()[0];
    assertTrue(type31.isAnnotationPresent(GraphQLNonNull.class) && type31.isAnnotationPresent(GraphQLScalar.class));
}
 
Example #6
Source File: TypeInferenceTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testSameClasses() throws AnnotationFormatException {
    Annotation[] nonNull = new Annotation[] {TypeFactory.annotation(Nonnull.class, Collections.emptyMap())};
    Annotation[] graphQLNonNull = new Annotation[] {TypeFactory.annotation(GraphQLNonNull.class, Collections.emptyMap())};
    Annotation[] mergedAnnotations = new Annotation[] {nonNull[0], graphQLNonNull[0]};
    AnnotatedType p1 = GenericTypeReflector.annotate(P.class, nonNull);
    AnnotatedType p2 = GenericTypeReflector.annotate(P.class, graphQLNonNull);
    AnnotatedType expected = GenericTypeReflector.annotate(P.class, mergedAnnotations);
    AnnotatedType inferred = ClassUtils.getCommonSuperType(Arrays.asList(p1, p2));
    assertTrue(GenericTypeReflector.equals(expected, inferred));
}
 
Example #7
Source File: TypeInferenceTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testLists() throws AnnotationFormatException {
    Annotation[] annotations = new Annotation[] {TypeFactory.annotation(GraphQLNonNull.class, Collections.emptyMap())};
    AnnotatedType nonNullLongType = GenericTypeReflector.annotate(Long.class, annotations);
    AnnotatedType doubleType = GenericTypeReflector.annotate(Double.class);
    AnnotatedType nonNullNumberType = TypeFactory.parameterizedAnnotatedClass(Number.class, annotations);
    AnnotatedType expected = TypeFactory.parameterizedAnnotatedClass(AbstractList.class, annotations, nonNullNumberType);
    AnnotatedType list1 = TypeFactory.parameterizedAnnotatedClass(ArrayList.class, annotations, nonNullLongType);
    AnnotatedType list2 = TypeFactory.parameterizedAnnotatedClass(LinkedList.class, new Annotation[0], doubleType);
    AnnotatedType inferred = ClassUtils.getCommonSuperType(Arrays.asList(list1, list2));
    assertTrue(GenericTypeReflector.equals(expected, inferred));
}
 
Example #8
Source File: GenericsTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayGenerics() {
    GenericItemRepo<@GraphQLNonNull List<Number> @GraphQLNonNull []> arrayNumberService = new GenericItemRepo<>();
    List<Number>[] array1 = (List<Number>[]) new List[1];
    array1[0] = Arrays.asList(12, 13.4, new BigDecimal("4000"));
    List<Number>[] array2 = (List<Number>[]) new List[1];
    array2[0] = Arrays.asList(new BigDecimal("12.56"), 14.78);
    arrayNumberService.addItem("scores1", array1);
    arrayNumberService.addItem("scores2", array2);

    GraphQLSchema schemaWithGenerics = new TestSchemaGenerator()
            .withOperationsFromSingleton(arrayNumberService, arrayOfListsOfNumbers)
            .withValueMapperFactory(valueMapperFactory)
            .generate();

    GraphQLOutputType itemType = schemaWithGenerics.getQueryType().getFieldDefinition("item").getType();
    assertNonNull(itemType, GraphQLList.class);
    GraphQLType inner = ((graphql.schema.GraphQLNonNull) itemType).getWrappedType();
    assertListOf(inner, graphql.schema.GraphQLNonNull.class);
    inner = ((graphql.schema.GraphQLNonNull) ((GraphQLList) inner).getWrappedType()).getWrappedType();
    assertListOf(inner, Scalars.GraphQLBigDecimal);

    GraphQLFieldDefinition addOneItem = schemaWithGenerics.getMutationType().getFieldDefinition("addItem");
    GraphQLType itemArgType = addOneItem.getArgument("item").getType();
    assertNonNull(itemArgType, GraphQLList.class);
    inner = ((graphql.schema.GraphQLNonNull) itemType).getWrappedType();
    assertListOf(inner, graphql.schema.GraphQLNonNull.class);
    inner = ((graphql.schema.GraphQLNonNull) ((GraphQLList) inner).getWrappedType()).getWrappedType();
    assertListOf(inner, Scalars.GraphQLBigDecimal);

    GraphQL graphQL = GraphQL.newGraphQL(schemaWithGenerics).build();
    ExecutionResult result = graphQL.execute("{ allItems }");
    assertTrue(ERRORS, result.getErrors().isEmpty());
    Object[] expected = arrayNumberService.getAllItems().toArray();
    Object[] actual = arrayNumberService.getAllItems().toArray();
    assertArrayEquals(expected, actual);
}
 
Example #9
Source File: GenericsTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testWildcardGenerics() {
    GenericItemRepo<@GraphQLNonNull List<? extends Number>> wildcardNumberService = new GenericItemRepo<>();
    wildcardNumberService.addItem("player1", Arrays.asList(12, 13.4, new BigDecimal("4000")));
    wildcardNumberService.addItem("player2", Arrays.asList(new BigDecimal("12.56"), 14.78));

    GraphQLSchema schemaWithGenerics = new TestSchemaGenerator()
            .withOperationsFromSingleton(wildcardNumberService, listOfWildcardNumbers)
            .withValueMapperFactory(valueMapperFactory)
            .generate();

    GraphQLOutputType itemType = schemaWithGenerics.getQueryType().getFieldDefinition("item").getType();
    assertNonNull(itemType, GraphQLList.class);
    assertListOf(((graphql.schema.GraphQLNonNull) itemType).getWrappedType(), Scalars.GraphQLBigDecimal);

    GraphQLOutputType itemCollectionType = schemaWithGenerics.getQueryType().getFieldDefinition("allItems").getType();
    assertListOfNonNull(itemCollectionType, GraphQLList.class);
    assertListOf(((graphql.schema.GraphQLNonNull) ((GraphQLList) itemCollectionType).getWrappedType()).getWrappedType(), Scalars.GraphQLBigDecimal);

    GraphQLFieldDefinition addOneItem = schemaWithGenerics.getMutationType().getFieldDefinition("addItem");
    GraphQLType itemArgType = addOneItem.getArgument("item").getType();
    assertNonNull(itemArgType, GraphQLList.class);
    assertListOf(((graphql.schema.GraphQLNonNull) itemArgType).getWrappedType(), Scalars.GraphQLBigDecimal);

    GraphQL graphQL = GraphQL.newGraphQL(schemaWithGenerics).build();
    ExecutionResult result = graphQL.execute("{ allItems }");
    assertTrue(ERRORS, result.getErrors().isEmpty());
    Object[] expected = wildcardNumberService.getAllItems().toArray();
    Object[] actual = wildcardNumberService.getAllItems().toArray();
    assertArrayEquals(expected, actual);
}
 
Example #10
Source File: GenericsTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonNullGenerics() {
    GenericItemRepo<@GraphQLNonNull String> nonNullStringService = new GenericItemRepo<>();
    nonNullStringService.addItem("pooch", "Strudel, the poodle");
    nonNullStringService.addItem("booze", "Fire-water");

    GraphQLSchema schemaWithNonNullGenerics = new TestSchemaGenerator()
            .withOperationsFromSingleton(nonNullStringService, nonNullString)
            .withValueMapperFactory(valueMapperFactory)
            .generate();

    GraphQLOutputType itemType = schemaWithNonNullGenerics.getQueryType().getFieldDefinition("item").getType();
    assertNonNull(itemType, Scalars.GraphQLString);

    GraphQLOutputType itemCollectionType = schemaWithNonNullGenerics.getQueryType().getFieldDefinition("allItems").getType();
    assertListOfNonNull(itemCollectionType, Scalars.GraphQLString);

    GraphQLFieldDefinition addOneItem = schemaWithNonNullGenerics.getMutationType().getFieldDefinition("addItem");
    assertEquals(addOneItem.getArguments().size(), 2);
    assertArgumentsPresent(addOneItem, "item", "name");
    assertNonNull(addOneItem.getArgument("item").getType(), Scalars.GraphQLString);

    GraphQLFieldDefinition addManyItems = schemaWithNonNullGenerics.getMutationType().getFieldDefinition("addItems");
    assertEquals(addManyItems.getArguments().size(), 1);
    assertArgumentsPresent(addManyItems, "items");
    assertListOfNonNull(addManyItems.getArgument("items").getType(), Scalars.GraphQLString);

    GraphQL graphQL = GraphQL.newGraphQL(schemaWithNonNullGenerics).build();
    ExecutionResult result = graphQL.execute("{ allItems }");
    assertTrue(ERRORS, result.getErrors().isEmpty());
    assertEquals(new ArrayList<>(nonNullStringService.getAllItems()), ((Map<String, Object>) result.getData()).get("allItems"));
}
 
Example #11
Source File: TypeInferenceTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrays() throws AnnotationFormatException {
    Annotation[] graphQlNonNull = new Annotation[] {TypeFactory.annotation(GraphQLNonNull.class, Collections.emptyMap())};
    Annotation[] nonNull = new Annotation[] {TypeFactory.annotation(Nonnull.class, Collections.emptyMap())};
    Annotation[] empty = new Annotation[0];
    AnnotatedType a1 = TypeFactory.arrayOf(GenericTypeReflector.annotate(Long.class, graphQlNonNull), nonNull);
    AnnotatedType a2 = TypeFactory.arrayOf(GenericTypeReflector.annotate(Double.class, empty), empty);
    AnnotatedType nonNullNumberType = TypeFactory.parameterizedAnnotatedClass(Number.class, graphQlNonNull);
    AnnotatedType expected = TypeFactory.arrayOf(nonNullNumberType, nonNull);
    AnnotatedType inferred = ClassUtils.getCommonSuperType(Arrays.asList(a1, a2));
    assertTrue(GenericTypeReflector.equals(expected, inferred));
}
 
Example #12
Source File: ToDoService.java    From micronaut-graphql with Apache License 2.0 5 votes vote down vote up
@GraphQLMutation
public boolean deleteToDo(@GraphQLNonNull @GraphQLArgument(name = "id") String id) {
    ToDo toDo = toDoRepository.findById(id);
    if (toDo != null) {
        toDoRepository.deleteById(id);
        return true;
    } else {
        return false;
    }
}
 
Example #13
Source File: ToDoService.java    From micronaut-graphql with Apache License 2.0 5 votes vote down vote up
@GraphQLMutation
public boolean completeToDo(@GraphQLNonNull @GraphQLArgument(name = "id") String id) {
    ToDo toDo = toDoRepository.findById(id);
    if (toDo != null) {
        toDo.setCompleted(true);
        toDoRepository.save(toDo);
        return true;
    } else {
        return false;
    }
}
 
Example #14
Source File: UnionTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery(name = "union")
public @GraphQLNonNull List<Map<@io.leangen.graphql.annotations.GraphQLUnion(name = "Simple_One_Two") ? extends SimpleTwo, @io.leangen.graphql.annotations.GraphQLUnion(name = "Education_Street") Education>> union2(@GraphQLArgument(name = "id") int id) {
    return null;
}
 
Example #15
Source File: NonNullTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery
public Integer integerWithDefault(@GraphQLArgument(name = "in", defaultValue = "3") @GraphQLNonNull Integer in) {
    return in;
}
 
Example #16
Source File: ComplexityTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery(name = "pet")
public @GraphQLNonNull Pet findPet(@GraphQLArgument(name = "cat") boolean cat) {
    return cat ? new Cat() : new Dog();
}
 
Example #17
Source File: ComplexityTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLMutation
@GraphQLComplexity("2 + childScore")
public @GraphQLNonNull List<@GraphQLNonNull Pet> addPet(@GraphQLArgument(name = "pet") Pet pet) {
    return Collections.singletonList(pet);
}
 
Example #18
Source File: TypeInferenceTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Test(expected = TypeMappingException.class)
public void testAnnotations() {
    AnnotatedType t1 = GenericTypeReflector.annotate(Nonnull.class);
    AnnotatedType t2 = GenericTypeReflector.annotate(GraphQLNonNull.class);
    ClassUtils.getCommonSuperType(Arrays.asList(t1, t2)).getType();
}
 
Example #19
Source File: Street.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery(name = "name", description = "Street name")
public @GraphQLNonNull String getName() {
    return name;
}
 
Example #20
Source File: InputFieldDiscoveryTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLInputField
public @GraphQLNonNull List<RelayTest.Book> getField3() {
    return field3;
}
 
Example #21
Source File: InputFieldDiscoveryTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLInputField
public RelayTest.@GraphQLNonNull Book getField2() {
    return field2;
}
 
Example #22
Source File: DirectiveTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery
public List<@GraphQLNonNull Book> books(String searchString) {
    return Collections.singletonList(new Book(searchString, "x123"));
}
 
Example #23
Source File: ToDoService.java    From micronaut-graphql with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery
public @GraphQLNonNull Iterable<@GraphQLNonNull ToDo> toDos() {
    return toDoRepository.findAll();
}
 
Example #24
Source File: RelayTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery
public Page<List<@GraphQLNonNull Book>> getBookLists(@GraphQLArgument(name = "first") int first, @GraphQLArgument(name = "after") String after) {
    return PageFactory.createOffsetBasedPage(Collections.singletonList(Collections.singletonList(new Book("Tesseract", "x123"))), 5, 0);
}
 
Example #25
Source File: ResolverBuilderTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery
public @GraphQLNonNull Object getField2() {
    return field2;
}
 
Example #26
Source File: PrimitivesTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery
public @GraphQLNonNull Integer nonNullInteger(@GraphQLNonNull Integer in) {
    return in;
}
 
Example #27
Source File: PrimitivesTest.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery
public @GraphQLNonNull int nonNullPrimitive(@GraphQLNonNull int in) {
    return in;
}
 
Example #28
Source File: ToDo.java    From micronaut-graphql with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery(name = "title")
public @GraphQLNonNull String getTitle() {
    return title;
}
 
Example #29
Source File: ToDo.java    From micronaut-graphql with Apache License 2.0 4 votes vote down vote up
@GraphQLQuery(name = "id")
public @GraphQLNonNull String getId() {
    return id;
}
 
Example #30
Source File: ToDoService.java    From micronaut-graphql with Apache License 2.0 4 votes vote down vote up
@GraphQLMutation
public ToDo createToDo(@GraphQLNonNull @GraphQLArgument(name = "title") String title) {
    ToDo toDo = new ToDo(title);
    return toDoRepository.save(toDo);
}