Search in sources :

Example 1 with GraphQLType

use of graphql.schema.GraphQLType in project graphql-java by graphql-java.

the class AstValueHelper method handleList.

private static Value handleList(Object _value, GraphQLList type) {
    GraphQLType itemType = type.getWrappedType();
    if (_value instanceof Iterable) {
        Iterable iterable = (Iterable) _value;
        List<Value> valuesNodes = new ArrayList<>();
        for (Object item : iterable) {
            Value itemNode = astFromValue(item, itemType);
            if (itemNode != null) {
                valuesNodes.add(itemNode);
            }
        }
        return new ArrayValue(valuesNodes);
    }
    return astFromValue(_value, itemType);
}
Also used : GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList)

Example 2 with GraphQLType

use of graphql.schema.GraphQLType in project graphql-java by graphql-java.

the class SchemaPrinter method print.

/**
 * This can print an in memory GraphQL schema back to a logical schema definition
 *
 * @param schema the schema in play
 *
 * @return the logical schema definition
 */
public String print(GraphQLSchema schema) {
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    GraphqlFieldVisibility visibility = schema.getFieldVisibility();
    printer(schema.getClass()).print(out, schema, visibility);
    List<GraphQLType> typesAsList = schema.getAllTypesAsList().stream().sorted(Comparator.comparing(GraphQLType::getName)).collect(toList());
    printType(out, typesAsList, GraphQLInterfaceType.class, visibility);
    printType(out, typesAsList, GraphQLUnionType.class, visibility);
    printType(out, typesAsList, GraphQLObjectType.class, visibility);
    printType(out, typesAsList, GraphQLEnumType.class, visibility);
    printType(out, typesAsList, GraphQLScalarType.class, visibility);
    printType(out, typesAsList, GraphQLInputObjectType.class, visibility);
    String result = sw.toString();
    if (result.endsWith("\n\n")) {
        result = result.substring(0, result.length() - 1);
    }
    return result;
}
Also used : GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) StringWriter(java.io.StringWriter) GraphQLType(graphql.schema.GraphQLType) PrintWriter(java.io.PrintWriter)

Example 3 with GraphQLType

use of graphql.schema.GraphQLType 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 4 with GraphQLType

use of graphql.schema.GraphQLType 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 5 with GraphQLType

use of graphql.schema.GraphQLType in project graphql-java by graphql-java.

the class SchemaGenerator method makeExecutableSchemaImpl.

private GraphQLSchema makeExecutableSchemaImpl(BuildContext buildCtx) {
    GraphQLObjectType query;
    GraphQLObjectType mutation;
    GraphQLObjectType subscription;
    GraphQLSchema.Builder schemaBuilder = GraphQLSchema.newSchema();
    // 
    // Schema can be missing if the type is called 'Query'.  Pre flight checks have checked that!
    // 
    TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry();
    if (!typeRegistry.schemaDefinition().isPresent()) {
        @SuppressWarnings({ "OptionalGetWithoutIsPresent", "ConstantConditions" }) TypeDefinition queryTypeDef = typeRegistry.getType("Query").get();
        query = buildOutputType(buildCtx, new TypeName(queryTypeDef.getName()));
        schemaBuilder.query(query);
        Optional<TypeDefinition> mutationTypeDef = typeRegistry.getType("Mutation");
        if (mutationTypeDef.isPresent()) {
            mutation = buildOutputType(buildCtx, new TypeName(mutationTypeDef.get().getName()));
            schemaBuilder.mutation(mutation);
        }
        Optional<TypeDefinition> subscriptionTypeDef = typeRegistry.getType("Subscription");
        if (subscriptionTypeDef.isPresent()) {
            subscription = buildOutputType(buildCtx, new TypeName(subscriptionTypeDef.get().getName()));
            schemaBuilder.subscription(subscription);
        }
    } else {
        SchemaDefinition schemaDefinition = typeRegistry.schemaDefinition().get();
        List<OperationTypeDefinition> operationTypes = schemaDefinition.getOperationTypeDefinitions();
        // pre-flight checked via checker
        @SuppressWarnings({ "OptionalGetWithoutIsPresent", "ConstantConditions" }) OperationTypeDefinition queryOp = operationTypes.stream().filter(op -> "query".equals(op.getName())).findFirst().get();
        Optional<OperationTypeDefinition> mutationOp = operationTypes.stream().filter(op -> "mutation".equals(op.getName())).findFirst();
        Optional<OperationTypeDefinition> subscriptionOp = operationTypes.stream().filter(op -> "subscription".equals(op.getName())).findFirst();
        query = buildOperation(buildCtx, queryOp);
        schemaBuilder.query(query);
        if (mutationOp.isPresent()) {
            mutation = buildOperation(buildCtx, mutationOp.get());
            schemaBuilder.mutation(mutation);
        }
        if (subscriptionOp.isPresent()) {
            subscription = buildOperation(buildCtx, subscriptionOp.get());
            schemaBuilder.subscription(subscription);
        }
    }
    Set<GraphQLType> additionalTypes = buildAdditionalTypes(buildCtx);
    schemaBuilder.fieldVisibility(buildCtx.getWiring().getFieldVisibility());
    return schemaBuilder.build(additionalTypes);
}
Also used : Arrays(java.util.Arrays) Value(graphql.language.Value) INPUT_FIELD_DEFINITION(graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) InputValueDefinition(graphql.language.InputValueDefinition) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) SchemaDefinition(graphql.language.SchemaDefinition) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) UNION(graphql.introspection.Introspection.DirectiveLocation.UNION) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Type(graphql.language.Type) INPUT_OBJECT(graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT) TypeResolverProxy(graphql.schema.TypeResolverProxy) GraphQLEnumValueDefinition.newEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition.newEnumValueDefinition) DataFetcherFactory(graphql.schema.DataFetcherFactory) EnumValueDefinition(graphql.language.EnumValueDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) OBJECT(graphql.introspection.Introspection.DirectiveLocation.OBJECT) GraphQLError(graphql.GraphQLError) Map(java.util.Map) TypeName(graphql.language.TypeName) TypeResolver(graphql.schema.TypeResolver) OperationTypeDefinition(graphql.language.OperationTypeDefinition) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) NotAnInputTypeError(graphql.schema.idl.errors.NotAnInputTypeError) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) UnionTypeExtensionDefinition(graphql.language.UnionTypeExtensionDefinition) Collections.emptyList(java.util.Collections.emptyList) FieldDefinition(graphql.language.FieldDefinition) GraphQLInputType(graphql.schema.GraphQLInputType) Set(java.util.Set) TypeDefinition(graphql.language.TypeDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) NotAnOutputTypeError(graphql.schema.idl.errors.NotAnOutputTypeError) List(java.util.List) Stream(java.util.stream.Stream) ENUM_VALUE(graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) GraphQLEnumType(graphql.schema.GraphQLEnumType) InterfaceTypeExtensionDefinition(graphql.language.InterfaceTypeExtensionDefinition) DirectiveLocation(graphql.introspection.Introspection.DirectiveLocation) GraphQLScalarType(graphql.schema.GraphQLScalarType) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) HashMap(java.util.HashMap) GraphQLType(graphql.schema.GraphQLType) Stack(java.util.Stack) ArrayList(java.util.ArrayList) ENUM(graphql.introspection.Introspection.DirectiveLocation.ENUM) Introspection(graphql.introspection.Introspection) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) UnionTypeDefinition(graphql.language.UnionTypeDefinition) DataFetcherFactories(graphql.schema.DataFetcherFactories) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) ScalarTypeExtensionDefinition(graphql.language.ScalarTypeExtensionDefinition) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EnumTypeExtensionDefinition(graphql.language.EnumTypeExtensionDefinition) GraphQLOutputType(graphql.schema.GraphQLOutputType) Directive(graphql.language.Directive) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) PublicApi(graphql.PublicApi) Assert.assertNotNull(graphql.Assert.assertNotNull) GraphQLTypeReference.typeRef(graphql.schema.GraphQLTypeReference.typeRef) Collections(java.util.Collections) TypeName(graphql.language.TypeName) SchemaDefinition(graphql.language.SchemaDefinition) GraphQLType(graphql.schema.GraphQLType) OperationTypeDefinition(graphql.language.OperationTypeDefinition) GraphQLSchema(graphql.schema.GraphQLSchema) EnumTypeDefinition(graphql.language.EnumTypeDefinition) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) GraphQLObjectType(graphql.schema.GraphQLObjectType)

Aggregations

GraphQLType (graphql.schema.GraphQLType)26 GraphQLObjectType (graphql.schema.GraphQLObjectType)7 GraphQLList (graphql.schema.GraphQLList)6 GraphQLNonNull (graphql.schema.GraphQLNonNull)5 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)4 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)4 GraphQLInputType (graphql.schema.GraphQLInputType)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 TypeName (graphql.language.TypeName)3 GraphQLEnumType (graphql.schema.GraphQLEnumType)3 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)3 GraphQLScalarType (graphql.schema.GraphQLScalarType)3 GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)3 Stack (java.util.Stack)3 PublicApi (graphql.PublicApi)2 ArrayValue (graphql.language.ArrayValue)2 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)2 Value (graphql.language.Value)2 GraphQLCompositeType (graphql.schema.GraphQLCompositeType)2