use of graphql.language.EnumTypeDefinition 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;
}
use of graphql.language.EnumTypeDefinition 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()));
});
});
}
use of graphql.language.EnumTypeDefinition 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);
}
use of graphql.language.EnumTypeDefinition 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();
}
use of graphql.language.EnumTypeDefinition 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);
}));
}
Aggregations