Search in sources :

Example 6 with ObjectTypeDefinition

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

the class SchemaTypeExtensionsChecker method checkUnionTypeExtensions.

/*
     * Union type extensions have the potential to be invalid if incorrectly defined.
     *
     * The named type must already be defined and must be a Union type.
     * The member types of a Union type extension must all be Object base types; Scalar, Interface and Union types must not be member types of a Union. Similarly, wrapping types must not be member types of a Union.
     * All member types of a Union type extension must be unique.
     * All member types of a Union type extension must not already be a member of the original Union type.
     * Any directives provided must not already apply to the original Union type.
     */
private void checkUnionTypeExtensions(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
    typeRegistry.unionTypeExtensions().forEach((name, extensions) -> {
        checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, UnionTypeDefinition.class);
        checkTypeExtensionDirectiveRedefinition(errors, typeRegistry, name, extensions, UnionTypeDefinition.class);
        extensions.forEach(extension -> {
            List<TypeName> memberTypes = extension.getMemberTypes().stream().map(t -> TypeInfo.typeInfo(t).getTypeName()).collect(Collectors.toList());
            checkNamedUniqueness(errors, memberTypes, TypeName::getName, (namedMember, memberType) -> new NonUniqueNameError(extension, namedMember));
            memberTypes.forEach(memberType -> {
                Optional<ObjectTypeDefinition> unionTypeDefinition = typeRegistry.getType(memberType, ObjectTypeDefinition.class);
                if (!unionTypeDefinition.isPresent()) {
                    errors.add(new MissingTypeError("union member", extension, memberType));
                }
            });
        });
    });
}
Also used : NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) Internal(graphql.Internal) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InputValueDefinition(graphql.language.InputValueDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) TypeExtensionFieldRedefinitionError(graphql.schema.idl.errors.TypeExtensionFieldRedefinitionError) Type(graphql.language.Type) UnionTypeDefinition(graphql.language.UnionTypeDefinition) EnumValueDefinition(graphql.language.EnumValueDefinition) GraphQLError(graphql.GraphQLError) TypeExtensionDirectiveRedefinitionError(graphql.schema.idl.errors.TypeExtensionDirectiveRedefinitionError) Map(java.util.Map) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) TypeExtensionEnumValueRedefinitionError(graphql.schema.idl.errors.TypeExtensionEnumValueRedefinitionError) TypeExtensionMissingBaseTypeError(graphql.schema.idl.errors.TypeExtensionMissingBaseTypeError) TypeName(graphql.language.TypeName) SchemaTypeChecker.checkNamedUniqueness(graphql.schema.idl.SchemaTypeChecker.checkNamedUniqueness) FpKit.mergeFirst(graphql.util.FpKit.mergeFirst) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) FieldDefinition(graphql.language.FieldDefinition) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) SchemaTypeChecker.checkDeprecatedDirective(graphql.schema.idl.SchemaTypeChecker.checkDeprecatedDirective) TypeDefinition(graphql.language.TypeDefinition) Collectors(java.util.stream.Collectors) Directive(graphql.language.Directive) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) AstPrinter(graphql.language.AstPrinter) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) List(java.util.List) FpKit(graphql.util.FpKit) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeName(graphql.language.TypeName) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError)

Example 7 with ObjectTypeDefinition

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

the class SchemaTypeExtensionsChecker method checkObjectTypeExtensions.

/*
     * Object type extensions have the potential to be invalid if incorrectly defined.
     *
     * The named type must already be defined and must be an Object type.
     * The fields of an Object type extension must have unique names; no two fields may share the same name.
     * Any fields of an Object type extension must not be already defined on the original Object type.
     * Any directives provided must not already apply to the original Object type.
     * Any interfaces provided must not be already implemented by the original Object type.
     * The resulting extended object type must be a super-set of all interfaces it implements.
     */
private void checkObjectTypeExtensions(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
    typeRegistry.objectTypeExtensions().forEach((name, extensions) -> {
        checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, ObjectTypeDefinition.class);
        checkTypeExtensionDirectiveRedefinition(errors, typeRegistry, name, extensions, ObjectTypeDefinition.class);
        extensions.forEach(extension -> {
            List<FieldDefinition> fieldDefinitions = extension.getFieldDefinitions();
            // field unique ness
            checkNamedUniqueness(errors, extension.getFieldDefinitions(), FieldDefinition::getName, (namedField, fieldDef) -> new NonUniqueNameError(extension, fieldDef));
            // field arg unique ness
            extension.getFieldDefinitions().forEach(fld -> checkNamedUniqueness(errors, fld.getInputValueDefinitions(), InputValueDefinition::getName, (namedField, inputValueDefinition) -> new NonUniqueArgumentError(extension, fld, name)));
            // directive checks
            extension.getFieldDefinitions().forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName, (directiveName, directive) -> new NonUniqueDirectiveError(extension, fld, directiveName)));
            fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
                checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(extension, fld));
                checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, (argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName));
            }));
            // 
            // fields must be unique within a type extension
            forEachBut(extension, extensions, otherTypeExt -> checkForFieldRedefinition(errors, otherTypeExt, otherTypeExt.getFieldDefinitions(), fieldDefinitions));
            // 
            // then check for field re-defs from the base type
            Optional<ObjectTypeDefinition> baseTypeOpt = typeRegistry.getType(extension.getName(), ObjectTypeDefinition.class);
            baseTypeOpt.ifPresent(baseTypeDef -> checkForFieldRedefinition(errors, extension, fieldDefinitions, baseTypeDef.getFieldDefinitions()));
        });
    });
}
Also used : NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) Internal(graphql.Internal) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InputValueDefinition(graphql.language.InputValueDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) TypeExtensionFieldRedefinitionError(graphql.schema.idl.errors.TypeExtensionFieldRedefinitionError) Type(graphql.language.Type) UnionTypeDefinition(graphql.language.UnionTypeDefinition) EnumValueDefinition(graphql.language.EnumValueDefinition) GraphQLError(graphql.GraphQLError) TypeExtensionDirectiveRedefinitionError(graphql.schema.idl.errors.TypeExtensionDirectiveRedefinitionError) Map(java.util.Map) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) TypeExtensionEnumValueRedefinitionError(graphql.schema.idl.errors.TypeExtensionEnumValueRedefinitionError) TypeExtensionMissingBaseTypeError(graphql.schema.idl.errors.TypeExtensionMissingBaseTypeError) TypeName(graphql.language.TypeName) SchemaTypeChecker.checkNamedUniqueness(graphql.schema.idl.SchemaTypeChecker.checkNamedUniqueness) FpKit.mergeFirst(graphql.util.FpKit.mergeFirst) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) FieldDefinition(graphql.language.FieldDefinition) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) SchemaTypeChecker.checkDeprecatedDirective(graphql.schema.idl.SchemaTypeChecker.checkDeprecatedDirective) TypeDefinition(graphql.language.TypeDefinition) Collectors(java.util.stream.Collectors) Directive(graphql.language.Directive) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) AstPrinter(graphql.language.AstPrinter) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) List(java.util.List) FpKit(graphql.util.FpKit) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) FieldDefinition(graphql.language.FieldDefinition) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError)

Example 8 with ObjectTypeDefinition

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

Example 9 with ObjectTypeDefinition

use of graphql.language.ObjectTypeDefinition 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 10 with ObjectTypeDefinition

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

the class IntrospectionResultToSchema method createObject.

@SuppressWarnings("unchecked")
ObjectTypeDefinition createObject(Map<String, Object> input) {
    assertTrue(input.get("kind").equals("OBJECT"), "wrong input");
    ObjectTypeDefinition objectTypeDefinition = new ObjectTypeDefinition((String) input.get("name"));
    objectTypeDefinition.setComments(toComment((String) input.get("description")));
    if (input.containsKey("interfaces")) {
        objectTypeDefinition.getImplements().addAll(((List<Map<String, Object>>) input.get("interfaces")).stream().map(this::createTypeIndirection).collect(Collectors.toList()));
    }
    List<Map<String, Object>> fields = (List<Map<String, Object>>) input.get("fields");
    objectTypeDefinition.getFieldDefinitions().addAll(createFields(fields));
    return objectTypeDefinition;
}
Also used : InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

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