/** * This helps you transform the current GraphQLEnumType into another one by starting a builder with all * the current values and allows you to transform it how you want. * * @param builderConsumer the consumer code that will be given a builder to transform * * @return a new field based on calling build on that builder */ public GraphQLEnumType transform(Consumer<Builder> builderConsumer) { Builder builder = newEnum(this); builderConsumer.accept(builder); return builder.build(); }
private Object coerceValueForEnum(GraphQLEnumType graphQLEnumType, Object value) { return graphQLEnumType.getCoercing().parseValue(value); }
public GraphQLEnumType build() { return new GraphQLEnumType(name, description, valuesToList(values), valuesToList(directives), definition); } }
public Builder(GraphQLEnumType existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.definition = existing.getDefinition(); this.values.putAll(getByName(existing.getValues(), GraphQLEnumValueDefinition::getName)); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
private TypePrinter<GraphQLEnumType> enumPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } printComments(out, type, ""); out.format("enum %s%s {\n", type.getName(), directivesString(type.getDirectives())); List<GraphQLEnumValueDefinition> values = type.getValues() .stream() .sorted(Comparator.comparing(GraphQLEnumValueDefinition::getName)) .collect(toList()); for (GraphQLEnumValueDefinition enumValueDefinition : values) { printComments(out, enumValueDefinition, " "); out.format(" %s%s\n", enumValueDefinition.getName(), directivesString(enumValueDefinition.getDirectives())); } out.format("}\n\n"); }; }
@Test(expectedExceptions = InvalidDirectiveLocationException.class) public void wireGraphQLEnumType_invalidLocations_exceptionIsThrown() throws Exception { // Arrange AnnotationsDirectiveWiring upperWiring = mock(AnnotationsDirectiveWiring.class); GraphQLEnumType directiveContainer = GraphQLEnumType.newEnum().name("asdf").value("asdfasdf").build(); AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); when(upperWiring.onEnum(upperCaseEnv)).thenReturn(directiveContainer); HashMap<GraphQLDirective, AnnotationsDirectiveWiring> map = new HashMap<>(); GraphQLDirective upperCase = newDirective().name("upperCase").build(); map.put(upperCase, upperWiring); // Act directiveWirer.wire(directiveContainer, map); }
private static GraphQLEnumType getEnum(final graphql.schema.GraphQLEnumType type){ final String name = type.getName(); if(REFUSED_OBJECT_TYPES.contains(name)){ return null; } final String id = generateId(); final GraphQLEnumType graphQLEnumType = new GraphQLEnumType(); graphQLEnumType.setId(id); graphQLEnumType.setName(name); graphQLEnumType.setDescription(type.getDescription()); for(EnumValueDefinition definition : type.getDefinition().getEnumValueDefinitions()){ GraphQLEnumValueDefinition enumValueDefinition = new GraphQLEnumValueDefinition(); enumValueDefinition.setName(definition.getName()); graphQLEnumType.getDefinitions().add(enumValueDefinition); } return graphQLEnumType; }
@Override protected void handleEnumError(Value value, GraphQLEnumType type) { errorMessage = "is not a valid '%s'"; arguments.add(type.getName()); argumentValue = value; }
} else if (descriptionHolder instanceof GraphQLEnumType) { GraphQLEnumType type = (GraphQLEnumType) descriptionHolder; return descriptionAndComments(type::getDescription, type::getDefinition, () -> type.getDefinition().getDescription()); } else if (descriptionHolder instanceof GraphQLFieldDefinition) { GraphQLFieldDefinition type = (GraphQLFieldDefinition) descriptionHolder;
@Test public void enumeration() { ProcessingElementsContainer container = testedProcessingElementsContainer(); GraphQLType enumeration = container.getDefaultTypeFunction().buildType(A.class, null,container); assertTrue(enumeration instanceof GraphQLEnumType); List<GraphQLEnumValueDefinition> values = ((GraphQLEnumType) enumeration).getValues(); assertEquals(values.stream(). map(GraphQLEnumValueDefinition::getName).collect(Collectors.toList()), Arrays.asList("someA", "B")); assertEquals(values.stream(). map(GraphQLEnumValueDefinition::getDescription).collect(Collectors.toList()), Arrays.asList("a", "B")); }
@Test public void wireGraphQLEnumType_validLocations_correctMethodIsCalled() throws Exception { // Arrange AnnotationsDirectiveWiring upperWiring = mock(AnnotationsDirectiveWiring.class); GraphQLEnumType directiveContainer = GraphQLEnumType.newEnum().name("asdf").value("asdfasdf").build(); AnnotationsWiringEnvironmentImpl upperCaseEnv = new AnnotationsWiringEnvironmentImpl(directiveContainer, directiveContainer.getDirective("upperCase")); when(upperWiring.onEnum(upperCaseEnv)).thenReturn(directiveContainer); HashMap<GraphQLDirective, AnnotationsDirectiveWiring> map = new HashMap<>(); GraphQLDirective upperCase = newDirective().name("upperCase").validLocations(Introspection.DirectiveLocation.ENUM).build(); map.put(upperCase, upperWiring); // Act directiveWirer.wire(directiveContainer, map); // Assert verify(upperWiring).onEnum(upperCaseEnv); }
public Builder(GraphQLEnumType existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.definition = existing.getDefinition(); this.values.putAll(getByName(existing.getValues(), GraphQLEnumValueDefinition::getName)); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
private TypePrinter<GraphQLEnumType> enumPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } SchemaPrinterComparatorEnvironment environment = SchemaPrinterComparatorEnvironment.newEnvironment() .parentType(GraphQLEnumType.class) .elementType(GraphQLEnumValueDefinition.class) .build(); Comparator<? super GraphQLType> comparator = options.comparatorRegistry.getComparator(environment); printComments(out, type, ""); out.format("enum %s%s {\n", type.getName(), directivesString(GraphQLEnumType.class, type.getDirectives())); List<GraphQLEnumValueDefinition> values = type.getValues() .stream() .sorted(comparator) .collect(toList()); for (GraphQLEnumValueDefinition enumValueDefinition : values) { printComments(out, enumValueDefinition, " "); out.format(" %s%s\n", enumValueDefinition.getName(), directivesString(GraphQLEnumValueDefinition.class, enumValueDefinition.getDirectives())); } out.format("}\n\n"); }; }
@Override public TraversalControl visitGraphQLEnumType(GraphQLEnumType node, TraverserContext<GraphQLType> context) { assertTypeUniqueness(node, result); save(node.getName(), node); return super.visitGraphQLEnumType(node, context); }
} else if (descriptionHolder instanceof GraphQLEnumType) { GraphQLEnumType type = (GraphQLEnumType) descriptionHolder; return descriptionAndComments(type::getDescription, type::getDefinition, () -> type.getDefinition().getDescription()); } else if (descriptionHolder instanceof GraphQLFieldDefinition) { GraphQLFieldDefinition type = (GraphQLFieldDefinition) descriptionHolder;
void enumTypes() { GraphQLEnumType colorEnum = newEnum() .name("Color") .description("Supported colors.") .value("RED") .value("GREEN") .value("BLUE") .build(); }
private static Object serialize(GraphQLType type, Object value) { if (type instanceof GraphQLScalarType) { return ((GraphQLScalarType) type).getCoercing().serialize(value); } else { return ((GraphQLEnumType) type).getCoercing().serialize(value); } }
@Override protected void handleEnumError(Value value, GraphQLEnumType type) { errorMessage = "is not a valid '%s'"; arguments.add(type.getName()); argumentValue = value; }
public GraphQLEnumType build() { return new GraphQLEnumType(name, description, valuesToList(values), valuesToList(directives), definition); } }
private GraphQLEnumType buildEnumType(BuildContext buildCtx, EnumTypeDefinition typeDefinition) { buildCtx.enterNode(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, buildCtx.getDirectiveDefinitions()) ); GraphQLEnumType enumType = builder.build(); enumType = directiveBehaviour.onEnum(enumType, buildCtx.mkBehaviourParams()); return buildCtx.exitNode(enumType); }