Search in sources :

Example 1 with GraphQLInterfaceType

use of graphql.schema.GraphQLInterfaceType 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 GraphQLInterfaceType

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

the class ExecutionStrategy method resolveType.

protected GraphQLObjectType resolveType(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLType fieldType) {
    GraphQLObjectType resolvedType;
    if (fieldType instanceof GraphQLInterfaceType) {
        TypeResolutionParameters resolutionParams = TypeResolutionParameters.newParameters().graphQLInterfaceType((GraphQLInterfaceType) fieldType).field(parameters.getField().get(0)).value(parameters.getSource()).argumentValues(parameters.getArguments()).context(executionContext.getContext()).schema(executionContext.getGraphQLSchema()).build();
        resolvedType = resolveTypeForInterface(resolutionParams);
    } else if (fieldType instanceof GraphQLUnionType) {
        TypeResolutionParameters resolutionParams = TypeResolutionParameters.newParameters().graphQLUnionType((GraphQLUnionType) fieldType).field(parameters.getField().get(0)).value(parameters.getSource()).argumentValues(parameters.getArguments()).context(executionContext.getContext()).schema(executionContext.getGraphQLSchema()).build();
        resolvedType = resolveTypeForUnion(resolutionParams);
    } else {
        resolvedType = (GraphQLObjectType) fieldType;
    }
    return resolvedType;
}
Also used : GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType)

Example 3 with GraphQLInterfaceType

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

the class ObjectsImplementInterfaces method isCompatible.

/**
 * @return {@code true} if the specified objectType satisfies the constraintType.
 */
boolean isCompatible(GraphQLOutputType constraintType, GraphQLOutputType objectType) {
    if (isSameType(constraintType, objectType)) {
        return true;
    } else if (constraintType instanceof GraphQLUnionType) {
        return objectIsMemberOfUnion((GraphQLUnionType) constraintType, objectType);
    } else if (constraintType instanceof GraphQLInterfaceType && objectType instanceof GraphQLObjectType) {
        return objectImplementsInterface((GraphQLInterfaceType) constraintType, (GraphQLObjectType) objectType);
    } else if (constraintType instanceof GraphQLList && objectType instanceof GraphQLList) {
        GraphQLOutputType wrappedConstraintType = (GraphQLOutputType) ((GraphQLList) constraintType).getWrappedType();
        GraphQLOutputType wrappedObjectType = (GraphQLOutputType) ((GraphQLList) objectType).getWrappedType();
        return isCompatible(wrappedConstraintType, wrappedObjectType);
    } else if (objectType instanceof GraphQLNonNull) {
        GraphQLOutputType nullableConstraint;
        if (constraintType instanceof GraphQLNonNull) {
            nullableConstraint = (GraphQLOutputType) ((GraphQLNonNull) constraintType).getWrappedType();
        } else {
            nullableConstraint = constraintType;
        }
        GraphQLOutputType nullableObjectType = (GraphQLOutputType) ((GraphQLNonNull) objectType).getWrappedType();
        return isCompatible(nullableConstraint, nullableObjectType);
    } else {
        return false;
    }
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLUnionType(graphql.schema.GraphQLUnionType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType)

Example 4 with GraphQLInterfaceType

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

the class SchemaGenerator method buildObjectTypeInterfaces.

private void buildObjectTypeInterfaces(BuildContext buildCtx, ObjectTypeDefinition typeDefinition, GraphQLObjectType.Builder builder, List<ObjectTypeExtensionDefinition> extensions) {
    Map<String, GraphQLInterfaceType> interfaces = new LinkedHashMap<>();
    typeDefinition.getImplements().forEach(type -> {
        GraphQLInterfaceType newInterfaceType = buildOutputType(buildCtx, type);
        interfaces.put(newInterfaceType.getName(), newInterfaceType);
    });
    extensions.forEach(extension -> extension.getImplements().forEach(type -> {
        GraphQLInterfaceType interfaceType = buildOutputType(buildCtx, type);
        if (!interfaces.containsKey(interfaceType.getName())) {
            interfaces.put(interfaceType.getName(), interfaceType);
        }
    }));
    interfaces.values().forEach(builder::withInterface);
}
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) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)4 GraphQLObjectType (graphql.schema.GraphQLObjectType)4 GraphQLUnionType (graphql.schema.GraphQLUnionType)3 DataFetcher (graphql.schema.DataFetcher)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)2 Assert.assertNotNull (graphql.Assert.assertNotNull)1 GraphQLError (graphql.GraphQLError)1 PublicApi (graphql.PublicApi)1 GraphQLString (graphql.Scalars.GraphQLString)1 Introspection (graphql.introspection.Introspection)1 DirectiveLocation (graphql.introspection.Introspection.DirectiveLocation)1 ENUM (graphql.introspection.Introspection.DirectiveLocation.ENUM)1 ENUM_VALUE (graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE)1 INPUT_FIELD_DEFINITION (graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION)1 INPUT_OBJECT (graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT)1 OBJECT (graphql.introspection.Introspection.DirectiveLocation.OBJECT)1 UNION (graphql.introspection.Introspection.DirectiveLocation.UNION)1 Directive (graphql.language.Directive)1 EnumTypeDefinition (graphql.language.EnumTypeDefinition)1 EnumTypeExtensionDefinition (graphql.language.EnumTypeExtensionDefinition)1