Search in sources :

Example 1 with EnumValueDefinition

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

the class IntrospectionResultToSchema method createEnum.

@SuppressWarnings("unchecked")
EnumTypeDefinition createEnum(Map<String, Object> input) {
    assertTrue(input.get("kind").equals("ENUM"), "wrong input");
    EnumTypeDefinition enumTypeDefinition = new EnumTypeDefinition((String) input.get("name"));
    enumTypeDefinition.setComments(toComment((String) input.get("description")));
    List<Map<String, Object>> enumValues = (List<Map<String, Object>>) input.get("enumValues");
    for (Map<String, Object> enumValue : enumValues) {
        EnumValueDefinition enumValueDefinition = new EnumValueDefinition((String) enumValue.get("name"));
        enumValueDefinition.setComments(toComment((String) enumValue.get("description")));
        createDeprecatedDirective(enumValue, enumValueDefinition.getDirectives());
        enumTypeDefinition.getEnumValueDefinitions().add(enumValueDefinition);
    }
    return enumTypeDefinition;
}
Also used : EnumTypeDefinition(graphql.language.EnumTypeDefinition) EnumValueDefinition(graphql.language.EnumValueDefinition) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with EnumValueDefinition

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

the class SchemaTypeExtensionsChecker method checkForEnumValueRedefinition.

private void checkForEnumValueRedefinition(List<GraphQLError> errors, TypeDefinition typeDefinition, List<EnumValueDefinition> enumValueDefinitions, List<EnumValueDefinition> referenceEnumValueDefinitions) {
    Map<String, EnumValueDefinition> referenceMap = FpKit.getByName(referenceEnumValueDefinitions, EnumValueDefinition::getName, mergeFirst());
    enumValueDefinitions.forEach(fld -> {
        if (referenceMap.containsKey(fld.getName())) {
            errors.add(new TypeExtensionEnumValueRedefinitionError(typeDefinition, fld));
        }
    });
}
Also used : EnumValueDefinition(graphql.language.EnumValueDefinition) TypeExtensionEnumValueRedefinitionError(graphql.schema.idl.errors.TypeExtensionEnumValueRedefinitionError)

Example 3 with EnumValueDefinition

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

the class SchemaTypeExtensionsChecker method checkEnumTypeExtensions.

/*
     * Enum type extensions have the potential to be invalid if incorrectly defined.
     *
     * The named type must already be defined and must be an Enum type.
     * All values of an Enum type extension must be unique.
     * All values of an Enum type extension must not already be a value of the original Enum.
     * Any directives provided must not already apply to the original Enum type.
     */
private void checkEnumTypeExtensions(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
    typeRegistry.enumTypeExtensions().forEach((name, extensions) -> {
        checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, EnumTypeDefinition.class);
        checkTypeExtensionDirectiveRedefinition(errors, typeRegistry, name, extensions, EnumTypeDefinition.class);
        extensions.forEach(extension -> {
            // field unique ness
            List<EnumValueDefinition> enumValueDefinitions = extension.getEnumValueDefinitions();
            checkNamedUniqueness(errors, enumValueDefinitions, EnumValueDefinition::getName, (namedField, enumValue) -> new NonUniqueNameError(extension, enumValue));
            // 
            // enum values must be unique within a type extension
            forEachBut(extension, extensions, otherTypeExt -> checkForEnumValueRedefinition(errors, otherTypeExt, otherTypeExt.getEnumValueDefinitions(), enumValueDefinitions));
            // 
            // then check for field re-defs from the base type
            Optional<EnumTypeDefinition> baseTypeOpt = typeRegistry.getType(extension.getName(), EnumTypeDefinition.class);
            baseTypeOpt.ifPresent(baseTypeDef -> checkForEnumValueRedefinition(errors, extension, enumValueDefinitions, baseTypeDef.getEnumValueDefinitions()));
        });
    });
}
Also used : EnumValueDefinition(graphql.language.EnumValueDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError)

Example 4 with EnumValueDefinition

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

the class SchemaGenerator method buildEnumType.

private GraphQLEnumType buildEnumType(BuildContext buildCtx, EnumTypeDefinition typeDefinition) {
    GraphQLEnumType.Builder builder = GraphQLEnumType.newEnum();
    builder.definition(typeDefinition);
    builder.name(typeDefinition.getName());
    builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription()));
    List<EnumTypeExtensionDefinition> extensions = enumTypeExtensions(typeDefinition, buildCtx);
    EnumValuesProvider enumValuesProvider = buildCtx.getWiring().getEnumValuesProviders().get(typeDefinition.getName());
    typeDefinition.getEnumValueDefinitions().forEach(evd -> {
        GraphQLEnumValueDefinition enumValueDefinition = buildEnumValue(buildCtx, typeDefinition, enumValuesProvider, evd);
        builder.value(enumValueDefinition);
    });
    extensions.forEach(extension -> extension.getEnumValueDefinitions().forEach(evd -> {
        GraphQLEnumValueDefinition enumValueDefinition = buildEnumValue(buildCtx, typeDefinition, enumValuesProvider, evd);
        if (!builder.hasValue(enumValueDefinition.getName())) {
            builder.value(enumValueDefinition);
        }
    }));
    builder.withDirectives(buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), ENUM));
    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) GraphQLEnumType(graphql.schema.GraphQLEnumType) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) EnumTypeExtensionDefinition(graphql.language.EnumTypeExtensionDefinition)

Example 5 with EnumValueDefinition

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

the class SchemaTypeChecker method checkEnumValues.

private void checkEnumValues(List<GraphQLError> errors, EnumTypeDefinition enumType, List<EnumValueDefinition> enumValueDefinitions) {
    // enum unique ness
    checkNamedUniqueness(errors, enumValueDefinitions, EnumValueDefinition::getName, (name, inputValueDefinition) -> new NonUniqueNameError(enumType, inputValueDefinition));
    // directive checks
    enumValueDefinitions.forEach(enumValue -> {
        BiFunction<String, Directive, NonUniqueDirectiveError> errorFunction = (directiveName, directive) -> new NonUniqueDirectiveError(enumType, enumValue, directiveName);
        checkNamedUniqueness(errors, enumValue.getDirectives(), Directive::getName, errorFunction);
    });
    enumValueDefinitions.forEach(enumValue -> enumValue.getDirectives().forEach(directive -> {
        checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(enumType, enumValue));
        BiFunction<String, Argument, NonUniqueArgumentError> errorFunction = (argumentName, argument) -> new NonUniqueArgumentError(enumType, enumValue, argumentName);
        checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, errorFunction);
    }));
}
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) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) BiFunction(java.util.function.BiFunction) EnumValueDefinition(graphql.language.EnumValueDefinition) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) Directive(graphql.language.Directive)

Aggregations

EnumValueDefinition (graphql.language.EnumValueDefinition)8 EnumTypeDefinition (graphql.language.EnumTypeDefinition)5 Directive (graphql.language.Directive)3 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)3 InputValueDefinition (graphql.language.InputValueDefinition)3 ArrayList (java.util.ArrayList)3 GraphQLError (graphql.GraphQLError)2 FieldDefinition (graphql.language.FieldDefinition)2 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)2 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)2 ObjectTypeExtensionDefinition (graphql.language.ObjectTypeExtensionDefinition)2 OperationTypeDefinition (graphql.language.OperationTypeDefinition)2 ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)2 SchemaDefinition (graphql.language.SchemaDefinition)2 Type (graphql.language.Type)2 TypeDefinition (graphql.language.TypeDefinition)2 TypeName (graphql.language.TypeName)2 UnionTypeDefinition (graphql.language.UnionTypeDefinition)2 List (java.util.List)2 Map (java.util.Map)2