graphql.schema.GraphQLList Java Examples

The following examples show how to use graphql.schema.GraphQLList. 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: PageMapper.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@Override
public GraphQLOutputType toGraphQLType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
    BuildContext buildContext = env.buildContext;

    AnnotatedType edgeType = GenericTypeReflector.getTypeParameter(javaType, Connection.class.getTypeParameters()[0]);
    AnnotatedType nodeType = GenericTypeReflector.getTypeParameter(edgeType, Edge.class.getTypeParameters()[0]);
    String connectionName = buildContext.typeInfoGenerator.generateTypeName(nodeType, buildContext.messageBundle) + "Connection";
    if (buildContext.typeCache.contains(connectionName)) {
        return new GraphQLTypeReference(connectionName);
    }
    buildContext.typeCache.register(connectionName);
    GraphQLOutputType type = env.operationMapper.toGraphQLType(nodeType, env);
    GraphQLNamedType unwrapped = GraphQLUtils.unwrap(type);
    String baseName = type instanceof GraphQLList ? unwrapped.getName() + "List" : unwrapped.getName();
    List<GraphQLFieldDefinition> edgeFields = getFields(baseName + "Edge", edgeType, env).stream()
            .filter(field -> !GraphQLUtils.isRelayEdgeField(field))
            .collect(Collectors.toList());
    GraphQLObjectType edge = buildContext.relay.edgeType(baseName, type, null, edgeFields);
    List<GraphQLFieldDefinition> connectionFields = getFields(baseName + "Connection", javaType, env).stream()
            .filter(field -> !GraphQLUtils.isRelayConnectionField(field))
            .collect(Collectors.toList());
    buildContext.typeRegistry.getDiscoveredTypes().add(Relay.pageInfoType);
    return buildContext.relay.connectionType(baseName, edge, connectionFields);
}
 
Example #2
Source File: GraphQLSchemaBuilder.java    From graphql-jpa with MIT License 6 votes vote down vote up
private GraphQLFieldDefinition getQueryFieldPageableDefinition(EntityType<?> entityType) {
    GraphQLObjectType pageType = GraphQLObjectType.newObject()
            .name(entityType.getName() + "Connection")
            .description("'Connection' response wrapper object for " + entityType.getName() + ".  When pagination or aggregation is requested, this object will be returned with metadata about the query.")
            .field(GraphQLFieldDefinition.newFieldDefinition().name("totalPages").description("Total number of pages calculated on the database for this pageSize.").type(Scalars.GraphQLLong).build())
            .field(GraphQLFieldDefinition.newFieldDefinition().name("totalElements").description("Total number of results on the database for this query.").type(Scalars.GraphQLLong).build())
            .field(GraphQLFieldDefinition.newFieldDefinition().name("content").description("The actual object results").type(new GraphQLList(getObjectType(entityType))).build())
            .build();

    return GraphQLFieldDefinition.newFieldDefinition()
            .name(entityType.getName() + "Connection")
            .description("'Connection' request wrapper object for " + entityType.getName() + ".  Use this object in a query to request things like pagination or aggregation in an argument.  Use the 'content' field to request actual fields ")
            .type(pageType)
            .dataFetcher(new ExtendedJpaDataFetcher(entityManager, entityType))
            .argument(paginationArgument)
            .build();
}
 
Example #3
Source File: RxExecutionStrategy.java    From graphql-rxjava with MIT License 6 votes vote down vote up
@Override
protected ExecutionResult completeValueForList(ExecutionContext executionContext, GraphQLList fieldType, List<Field> fields, List<Object> result) {
    Observable<?> resultObservable =
            Observable.from(
                    IntStream.range(0, result.size())
                            .mapToObj(idx -> new ListTuple(idx, result.get(idx)))
                            .toArray(ListTuple[]::new)
            )
            .flatMap(tuple -> {
                ExecutionResult executionResult = completeValue(executionContext, fieldType.getWrappedType(), fields, tuple.result);

                if (executionResult instanceof RxExecutionResult) {
                    return Observable.zip(Observable.just(tuple.index), ((RxExecutionResult)executionResult).getDataObservable(), ListTuple::new);
                }
                return Observable.just(new ListTuple(tuple.index, executionResult.getData()));
            })
            .toList()
            .map(listTuples -> {
                return listTuples.stream()
                        .sorted(Comparator.comparingInt(x -> x.index))
                        .map(x -> x.result)
                        .collect(Collectors.toList());
            });

    return new RxExecutionResult(resultObservable, null);
}
 
Example #4
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 #5
Source File: MapToListTypeAdapter.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
private String getTypeName(GraphQLType type) {
    if (type instanceof GraphQLModifiedType) {
        StringBuilder name = new StringBuilder();
        while (type instanceof GraphQLModifiedType) {
            if (type instanceof GraphQLList) {
                name.append("list_");
            } else {
                name.append("__");
            }
            type = ((GraphQLModifiedType) type).getWrappedType();
        }
        return name.append(name(type)).toString();
    } else {
        return name(type);
    }
}
 
Example #6
Source File: SchemaToProto.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("JdkObsolete")
static Set<GraphQLType> getAllTypes(GraphQLSchema schema) {
  LinkedHashSet<GraphQLType> types = new LinkedHashSet<>();
  LinkedList<GraphQLObjectType> loop = new LinkedList<>();
  loop.add(schema.getQueryType());
  types.add(schema.getQueryType());

  while (!loop.isEmpty()) {
    for (GraphQLFieldDefinition field : loop.pop().getFieldDefinitions()) {
      GraphQLType type = field.getType();
      if (type instanceof GraphQLList) {
        type = ((GraphQLList) type).getWrappedType();
      }
      if (!types.contains(type)) {
        if (type instanceof GraphQLEnumType) {
          types.add(field.getType());
        }
        if (type instanceof GraphQLObjectType) {
          types.add(type);
          loop.add((GraphQLObjectType) type);
        }
      }
    }
  }
  return types;
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: GraphQLSchemaBuilder.java    From graphql-jpa with MIT License 5 votes vote down vote up
GraphQLFieldDefinition getQueryEmbeddedFieldDefinition(EmbeddableType<?> embeddableType) {
	String embeddedName = embeddableType.getJavaType().getSimpleName();
    return GraphQLFieldDefinition.newFieldDefinition()
            .name(embeddedName)
            .description(getSchemaDocumentation(embeddableType.getJavaType()))
            .type(new GraphQLList(getObjectType(embeddableType)))
            .argument(embeddableType.getAttributes().stream().filter(this::isValidInput).filter(this::isNotIgnored).flatMap(this::getArgument).collect(Collectors.toList()))
            .build();
}
 
Example #12
Source File: GraphQLSchemaBuilder.java    From graphql-jpa with MIT License 5 votes vote down vote up
GraphQLFieldDefinition getQueryFieldDefinition(EntityType<?> entityType) {
    return GraphQLFieldDefinition.newFieldDefinition()
            .name(entityType.getName())
            .description(getSchemaDocumentation(entityType.getJavaType()))
            .type(new GraphQLList(getObjectType(entityType)))
            .dataFetcher(new JpaDataFetcher(entityManager, entityType))
            .argument(entityType.getAttributes().stream().filter(this::isValidInput).filter(this::isNotIgnored).flatMap(this::getArgument).collect(Collectors.toList()))
            .build();
}
 
Example #13
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 #14
Source File: GraphQLTypeAssertions.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
public static void assertInputMapOf(GraphQLType mapType, Class<? extends GraphQLType> keyType, Class<? extends GraphQLType> valueType) {
    assertEquals(GraphQLList.class, mapType.getClass());
    GraphQLType entry = GraphQLUtils.unwrap(mapType);
    assertTrue(entry instanceof GraphQLInputObjectType);
    GraphQLInputType key = ((GraphQLInputObjectType) entry).getFieldDefinition("key").getType();
    GraphQLInputType value = ((GraphQLInputObjectType) entry).getFieldDefinition("value").getType();
    assertTrue(keyType.isAssignableFrom(key.getClass()));
    assertTrue(valueType.isAssignableFrom(value.getClass()));
}
 
Example #15
Source File: GraphQLTypeAssertions.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
public static void assertMapOf(GraphQLType mapType, Class<? extends GraphQLType> keyType, Class<? extends GraphQLType> valueType) {
    assertEquals(GraphQLList.class, mapType.getClass());
    GraphQLType entry = GraphQLUtils.unwrap(mapType);
    assertTrue(entry instanceof GraphQLObjectType);
    GraphQLOutputType key = ((GraphQLObjectType) entry).getFieldDefinition("key").getType();
    GraphQLOutputType value = ((GraphQLObjectType) entry).getFieldDefinition("value").getType();
    assertTrue(keyType.isAssignableFrom(key.getClass()));
    assertTrue(valueType.isAssignableFrom(value.getClass()));
}
 
Example #16
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 #17
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 #18
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 #19
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 #20
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 #21
Source File: TypeGeneratorRawArrayTest.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
public void assertListOfInt(Object objectType) {
    Assert.assertThat(objectType, instanceOf(GraphQLObjectType.class));
    Assert.assertThat(objectType, not(instanceOf(GraphQLList.class)));
    GraphQLFieldDefinition fieldDefinition = ((GraphQLObjectType) objectType).getFieldDefinition("integers");
    Assert.assertThat(fieldDefinition, notNullValue());
    GraphQLOutputType outputType = fieldDefinition.getType();

    Assert.assertThat(outputType, CoreMatchers.instanceOf(GraphQLList.class));
    GraphQLType wrappedType = ((GraphQLList) outputType).getWrappedType();
    Assert.assertThat(wrappedType, instanceOf(GraphQLScalarType.class));
    Assert.assertThat((GraphQLScalarType)wrappedType, is(Scalars.GraphQLInt));
}
 
Example #22
Source File: TypeGeneratorGenericsTest.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
@Test
public void testGeneratedListOfParam() {
    logger.debug("testGeneratedListOfParam");
    Object objectType = generator.getOutputType(ClassWithListOfGenerics.class);
    Assert.assertThat(objectType, instanceOf(GraphQLObjectType.class));
    Assert.assertThat(objectType, not(instanceOf(GraphQLList.class)));
    GraphQLFieldDefinition field = ((GraphQLObjectType) objectType)
            .getFieldDefinition("listOfParamOfInts");
    
    Assert.assertThat(field, notNullValue());
    GraphQLOutputType listType = field.getType();
    Assert.assertThat(listType, instanceOf(GraphQLList.class));
    GraphQLType wrappedType = ((GraphQLList) listType).getWrappedType();
    Assert.assertThat(wrappedType, instanceOf(GraphQLObjectType.class));
}
 
Example #23
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 #24
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 #25
Source File: DirectivesAndTypeWalker.java    From samples with MIT License 5 votes vote down vote up
private static boolean walkInputType(GraphQLInputType inputType, List<GraphQLDirective> directives, BiFunction<GraphQLInputType, GraphQLDirective, Boolean> isSuitable) {
  GraphQLInputType unwrappedInputType = Util.unwrapNonNull(inputType);
  for (GraphQLDirective directive : directives) {
    if (isSuitable.apply(unwrappedInputType, directive)) {
      return true;
    }
  }
  if (unwrappedInputType instanceof GraphQLInputObjectType) {
    GraphQLInputObjectType inputObjType = (GraphQLInputObjectType) unwrappedInputType;
    for (GraphQLInputObjectField inputField : inputObjType.getFieldDefinitions()) {
      inputType = inputField.getType();
      directives = inputField.getDirectives();

      if (walkInputType(inputType, directives, isSuitable)) {
        return true;
      }
    }
  }
  if (unwrappedInputType instanceof GraphQLList) {
    GraphQLInputType innerListType = Util.unwrapOneAndAllNonNull(unwrappedInputType);
    if (innerListType instanceof GraphQLDirectiveContainer) {
      directives = ((GraphQLDirectiveContainer) innerListType).getDirectives();
      if (walkInputType(innerListType, directives, isSuitable)) {
        return true;
      }
    }
  }
  return false;
}
 
Example #26
Source File: SchemaToProto.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
private static String toType(GraphQLType type) {
  if (type instanceof GraphQLList) {
    return "repeated " + toType(((GraphQLList) type).getWrappedType());
  } else if (type instanceof GraphQLObjectType) {
    return ((GraphQLObjectType) type).getName();
  } else if (type instanceof GraphQLEnumType) {
    return ((GraphQLEnumType) type).getName() + ".Enum";
  } else {
    return TYPE_MAP.get(((GraphQLNamedType) type).getName());
  }
}
 
Example #27
Source File: ProtoToGql.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
/** Returns a GraphQLOutputType generated from a FieldDescriptor. */
static GraphQLOutputType convertType(
    FieldDescriptor fieldDescriptor, SchemaOptions schemaOptions) {
  final GraphQLOutputType type;

  if (fieldDescriptor.getType() == Type.MESSAGE) {
    type = getReference(fieldDescriptor.getMessageType());
  } else if (fieldDescriptor.getType() == Type.GROUP) {
    type = getReference(fieldDescriptor.getMessageType());
  } else if (fieldDescriptor.getType() == Type.ENUM) {
    type = getReference(fieldDescriptor.getEnumType());
  } else if (schemaOptions.useProtoScalarTypes()) {
    type = PROTO_TYPE_MAP.get(fieldDescriptor.getType());
  } else {
    type = TYPE_MAP.get(fieldDescriptor.getType());
  }

  if (type == null) {
    throw new RuntimeException("Unknown type: " + fieldDescriptor.getType());
  }

  if (fieldDescriptor.isRepeated()) {
    return new GraphQLList(type);
  } else {
    return type;
  }
}
 
Example #28
Source File: GqlInputConverter.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
private static GraphQLType getFieldType(FieldDescriptor field, SchemaOptions schemaOptions) {
  if (field.getType() == FieldDescriptor.Type.MESSAGE
      || field.getType() == FieldDescriptor.Type.GROUP) {
    return new GraphQLTypeReference(getReferenceName(field.getMessageType()));
  }
  if (field.getType() == FieldDescriptor.Type.ENUM) {
    return new GraphQLTypeReference(ProtoToGql.getReferenceName(field.getEnumType()));
  }
  GraphQLType type = ProtoToGql.convertType(field, schemaOptions);
  if (type instanceof GraphQLList) {
    return ((GraphQLList) type).getWrappedType();
  }
  return type;
}
 
Example #29
Source File: TypeWrapper_ReflectionList.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
@Override
public GraphQLType wrapType(GraphQLType interiorType, TypeSpecContainer originalObject) {
    if (!canWrapList(originalObject)) {
        return interiorType;
    }
    return new GraphQLList(interiorType);
}
 
Example #30
Source File: TypeGeneratorListOfListTest.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
public static void assertClassWithListOfList(Object objectType) {
    Assert.assertThat(objectType, instanceOf(GraphQLObjectType.class));
    Assert.assertThat(objectType, not(instanceOf(GraphQLList.class)));
    GraphQLFieldDefinition field = ((GraphQLObjectType) objectType)
            .getFieldDefinition("listOfListOfInts");
    
    Assert.assertThat(field, notNullValue());
    GraphQLOutputType outputType = field.getType();
    assertListOfListOfInt(outputType);
}