Search in sources :

Example 6 with UnionTypeDefinition

use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.

the class SchemaGenerator method buildUnionType.

private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) {
    GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType();
    builder.definition(typeDefinition);
    builder.name(typeDefinition.getName());
    builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription()));
    builder.typeResolver(getTypeResolverForUnion(buildCtx, typeDefinition));
    List<UnionTypeExtensionDefinition> extensions = unionTypeExtensions(typeDefinition, buildCtx);
    typeDefinition.getMemberTypes().forEach(mt -> {
        GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
        if (outputType instanceof GraphQLTypeReference) {
            builder.possibleType((GraphQLTypeReference) outputType);
        } else {
            builder.possibleType((GraphQLObjectType) outputType);
        }
    });
    builder.withDirectives(buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), UNION));
    extensions.forEach(extension -> extension.getMemberTypes().forEach(mt -> {
        GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
        if (!builder.containType(outputType.getName())) {
            if (outputType instanceof GraphQLTypeReference) {
                builder.possibleType((GraphQLTypeReference) outputType);
            } else {
                builder.possibleType((GraphQLObjectType) outputType);
            }
        }
    }));
    return builder.build();
}
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) GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) GraphQLUnionType(graphql.schema.GraphQLUnionType) UnionTypeExtensionDefinition(graphql.language.UnionTypeExtensionDefinition) GraphQLObjectType(graphql.schema.GraphQLObjectType)

Example 7 with UnionTypeDefinition

use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.

the class SchemaDiff method checkType.

private void checkType(DiffCtx ctx, Type oldType, Type newType) {
    String typeName = getTypeName(oldType);
    // prevent circular references
    if (ctx.examiningType(typeName)) {
        return;
    }
    if (isSystemScalar(typeName)) {
        return;
    }
    if (isReservedType(typeName)) {
        return;
    }
    Optional<TypeDefinition> oldTD = ctx.getOldTypeDef(oldType, TypeDefinition.class);
    Optional<TypeDefinition> newTD = ctx.getNewTypeDef(newType, TypeDefinition.class);
    if (!oldTD.isPresent()) {
        ctx.report(DiffEvent.apiInfo().typeName(typeName).reasonMsg("Type '%s' is missing", typeName).build());
        return;
    }
    TypeDefinition oldDef = oldTD.get();
    ctx.report(DiffEvent.apiInfo().typeName(typeName).typeKind(getTypeKind(oldDef)).reasonMsg("Examining type '%s' ...", typeName).build());
    if (!newTD.isPresent()) {
        ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(typeName).typeKind(getTypeKind(oldDef)).reasonMsg("The new API does not have a type called '%s'", typeName).build());
        ctx.exitType();
        return;
    }
    TypeDefinition newDef = newTD.get();
    if (!oldDef.getClass().equals(newDef.getClass())) {
        ctx.report(DiffEvent.apiBreakage().category(DiffCategory.INVALID).typeName(typeName).typeKind(getTypeKind(oldDef)).components(getTypeKind(oldDef), getTypeKind(newDef)).reasonMsg("The new API has changed '%s' from a '%s' to a '%s'", typeName, getTypeKind(oldDef), getTypeKind(newDef)).build());
        ctx.exitType();
        return;
    }
    if (oldDef instanceof ObjectTypeDefinition) {
        checkObjectType(ctx, (ObjectTypeDefinition) oldDef, (ObjectTypeDefinition) newDef);
    }
    if (oldDef instanceof InterfaceTypeDefinition) {
        checkInterfaceType(ctx, (InterfaceTypeDefinition) oldDef, (InterfaceTypeDefinition) newDef);
    }
    if (oldDef instanceof UnionTypeDefinition) {
        checkUnionType(ctx, (UnionTypeDefinition) oldDef, (UnionTypeDefinition) newDef);
    }
    if (oldDef instanceof InputObjectTypeDefinition) {
        checkInputObjectType(ctx, (InputObjectTypeDefinition) oldDef, (InputObjectTypeDefinition) newDef);
    }
    if (oldDef instanceof EnumTypeDefinition) {
        checkEnumType(ctx, (EnumTypeDefinition) oldDef, (EnumTypeDefinition) newDef);
    }
    if (oldDef instanceof ScalarTypeDefinition) {
        checkScalarType(ctx, (ScalarTypeDefinition) oldDef, (ScalarTypeDefinition) newDef);
    }
    ctx.exitType();
}
Also used : ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition)

Example 8 with UnionTypeDefinition

use of graphql.language.UnionTypeDefinition in project graphql-java by graphql-java.

the class IntrospectionResultToSchema method createUnion.

@SuppressWarnings("unchecked")
UnionTypeDefinition createUnion(Map<String, Object> input) {
    assertTrue(input.get("kind").equals("UNION"), "wrong input");
    UnionTypeDefinition unionTypeDefinition = new UnionTypeDefinition((String) input.get("name"));
    unionTypeDefinition.setComments(toComment((String) input.get("description")));
    List<Map<String, Object>> possibleTypes = (List<Map<String, Object>>) input.get("possibleTypes");
    for (Map<String, Object> possibleType : possibleTypes) {
        TypeName typeName = new TypeName((String) possibleType.get("name"));
        unionTypeDefinition.getMemberTypes().add(typeName);
    }
    return unionTypeDefinition;
}
Also used : TypeName(graphql.language.TypeName) UnionTypeDefinition(graphql.language.UnionTypeDefinition) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

UnionTypeDefinition (graphql.language.UnionTypeDefinition)8 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)6 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)6 TypeDefinition (graphql.language.TypeDefinition)6 EnumTypeDefinition (graphql.language.EnumTypeDefinition)5 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)5 ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)5 OperationTypeDefinition (graphql.language.OperationTypeDefinition)4 Type (graphql.language.Type)4 TypeName (graphql.language.TypeName)4 List (java.util.List)4 Map (java.util.Map)4 GraphQLError (graphql.GraphQLError)3 Directive (graphql.language.Directive)3 EnumValueDefinition (graphql.language.EnumValueDefinition)3 FieldDefinition (graphql.language.FieldDefinition)3 InputValueDefinition (graphql.language.InputValueDefinition)3 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3