Search in sources :

Example 6 with InterfaceTypeDefinition

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

the class SchemaGenerator method buildOutputType.

/**
 * This is the main recursive spot that builds out the various forms of Output types
 *
 * @param buildCtx the context we need to work out what we are doing
 * @param rawType  the type to be built
 *
 * @return an output type
 */
@SuppressWarnings("unchecked")
private <T extends GraphQLOutputType> T buildOutputType(BuildContext buildCtx, Type rawType) {
    TypeDefinition typeDefinition = buildCtx.getTypeDefinition(rawType);
    TypeInfo typeInfo = TypeInfo.typeInfo(rawType);
    GraphQLOutputType outputType = buildCtx.hasOutputType(typeDefinition);
    if (outputType != null) {
        return typeInfo.decorate(outputType);
    }
    if (buildCtx.stackContains(typeInfo)) {
        // otherwise we will go into an infinite loop
        return typeInfo.decorate(typeRef(typeInfo.getName()));
    }
    buildCtx.push(typeInfo);
    if (typeDefinition instanceof ObjectTypeDefinition) {
        outputType = buildObjectType(buildCtx, (ObjectTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof InterfaceTypeDefinition) {
        outputType = buildInterfaceType(buildCtx, (InterfaceTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof UnionTypeDefinition) {
        outputType = buildUnionType(buildCtx, (UnionTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof EnumTypeDefinition) {
        outputType = buildEnumType(buildCtx, (EnumTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof ScalarTypeDefinition) {
        outputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
    } else {
        // typeDefinition is not a valid output type
        throw new NotAnOutputTypeError(typeDefinition);
    }
    buildCtx.put(outputType);
    buildCtx.pop();
    return (T) typeInfo.decorate(outputType);
}
Also used : ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) GraphQLOutputType(graphql.schema.GraphQLOutputType) INPUT_OBJECT(graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT) OBJECT(graphql.introspection.Introspection.DirectiveLocation.OBJECT) EnumTypeDefinition(graphql.language.EnumTypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) NotAnOutputTypeError(graphql.schema.idl.errors.NotAnOutputTypeError) 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)

Example 7 with InterfaceTypeDefinition

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

the class SchemaTypeChecker method checkTypeResolversArePresent.

private void checkTypeResolversArePresent(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) {
    Predicate<InterfaceTypeDefinition> noDynamicResolverForInterface = interaceTypeDef -> !wiring.getWiringFactory().providesTypeResolver(new InterfaceWiringEnvironment(typeRegistry, interaceTypeDef));
    Predicate<UnionTypeDefinition> noDynamicResolverForUnion = unionTypeDef -> !wiring.getWiringFactory().providesTypeResolver(new UnionWiringEnvironment(typeRegistry, unionTypeDef));
    Predicate<TypeDefinition> noTypeResolver = typeDefinition -> !wiring.getTypeResolvers().containsKey(typeDefinition.getName());
    Consumer<TypeDefinition> addError = typeDefinition -> errors.add(new MissingTypeResolverError(typeDefinition));
    typeRegistry.types().values().stream().filter(typeDef -> typeDef instanceof InterfaceTypeDefinition).map(InterfaceTypeDefinition.class::cast).filter(noDynamicResolverForInterface).filter(noTypeResolver).forEach(addError);
    typeRegistry.types().values().stream().filter(typeDef -> typeDef instanceof UnionTypeDefinition).map(UnionTypeDefinition.class::cast).filter(noDynamicResolverForUnion).filter(noTypeResolver).forEach(addError);
}
Also used : MissingInterfaceTypeError(graphql.schema.idl.errors.MissingInterfaceTypeError) BiFunction(java.util.function.BiFunction) InputValueDefinition(graphql.language.InputValueDefinition) SchemaDefinition(graphql.language.SchemaDefinition) MissingInterfaceFieldError(graphql.schema.idl.errors.MissingInterfaceFieldError) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Type(graphql.language.Type) MissingScalarImplementationError(graphql.schema.idl.errors.MissingScalarImplementationError) EnumValueDefinition(graphql.language.EnumValueDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) GraphQLError(graphql.GraphQLError) Map(java.util.Map) TypeName(graphql.language.TypeName) QueryOperationMissingError(graphql.schema.idl.errors.QueryOperationMissingError) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) InterfaceFieldRedefinitionError(graphql.schema.idl.errors.InterfaceFieldRedefinitionError) Predicate(java.util.function.Predicate) Collection(java.util.Collection) FieldDefinition(graphql.language.FieldDefinition) Set(java.util.Set) TypeDefinition(graphql.language.TypeDefinition) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) AstPrinter(graphql.language.AstPrinter) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) List(java.util.List) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) InterfaceFieldArgumentRedefinitionError(graphql.schema.idl.errors.InterfaceFieldArgumentRedefinitionError) SchemaMissingError(graphql.schema.idl.errors.SchemaMissingError) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) Internal(graphql.Internal) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) MissingTypeResolverError(graphql.schema.idl.errors.MissingTypeResolverError) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UnionTypeDefinition(graphql.language.UnionTypeDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) OperationTypesMustBeObjects(graphql.schema.idl.errors.OperationTypesMustBeObjects) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) MissingInterfaceFieldArgumentsError(graphql.schema.idl.errors.MissingInterfaceFieldArgumentsError) Directive(graphql.language.Directive) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) StringValue(graphql.language.StringValue) Collections(java.util.Collections) MissingTypeResolverError(graphql.schema.idl.errors.MissingTypeResolverError) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) 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)

Example 8 with InterfaceTypeDefinition

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

the class SchemaDiff method checkImplements.

private void checkImplements(DiffCtx ctx, ObjectTypeDefinition old, List<Type> oldImplements, List<Type> newImplements) {
    Map<String, Type> oldImplementsMap = sortedMap(oldImplements, t -> ((TypeName) t).getName());
    Map<String, Type> newImplementsMap = sortedMap(newImplements, t -> ((TypeName) t).getName());
    for (Map.Entry<String, Type> entry : oldImplementsMap.entrySet()) {
        InterfaceTypeDefinition oldInterface = ctx.getOldTypeDef(entry.getValue(), InterfaceTypeDefinition.class).get();
        Optional<InterfaceTypeDefinition> newInterface = ctx.getNewTypeDef(newImplementsMap.get(entry.getKey()), InterfaceTypeDefinition.class);
        if (!newInterface.isPresent()) {
            ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(oldInterface.getName()).reasonMsg("The new API is missing the interface named '%s'", oldInterface.getName()).build());
        } else {
            checkInterfaceType(ctx, oldInterface, newInterface.get());
        }
    }
}
Also used : Type(graphql.language.Type) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 9 with InterfaceTypeDefinition

use of graphql.language.InterfaceTypeDefinition 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)

Aggregations

InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)9 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)6 TypeDefinition (graphql.language.TypeDefinition)6 UnionTypeDefinition (graphql.language.UnionTypeDefinition)6 EnumTypeDefinition (graphql.language.EnumTypeDefinition)5 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)5 Type (graphql.language.Type)5 Map (java.util.Map)5 OperationTypeDefinition (graphql.language.OperationTypeDefinition)4 ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)4 GraphQLError (graphql.GraphQLError)3 Internal (graphql.Internal)3 Argument (graphql.language.Argument)3 AstPrinter (graphql.language.AstPrinter)3 Directive (graphql.language.Directive)3 EnumValueDefinition (graphql.language.EnumValueDefinition)3 FieldDefinition (graphql.language.FieldDefinition)3 InputValueDefinition (graphql.language.InputValueDefinition)3 TypeName (graphql.language.TypeName)3 InvalidDeprecationDirectiveError (graphql.schema.idl.errors.InvalidDeprecationDirectiveError)3