graphql.schema.GraphQLEnumValueDefinition Java Examples

The following examples show how to use graphql.schema.GraphQLEnumValueDefinition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnumValues_Reflection.java    From graphql-java-type-generator with MIT License 6 votes vote down vote up
@Override
public List<GraphQLEnumValueDefinition> getEnumValueDefinitions(Object object) {
    Class<?> clazz = ReflectionUtils.extractClassFromSupportedObject(object);
    if (clazz == null) return null;
    if (!clazz.isEnum()) {
        return null;
    }
    
    List<GraphQLEnumValueDefinition> valueObjects =
            new ArrayList<GraphQLEnumValueDefinition>();
    for (Object value : clazz.getEnumConstants()) {
        valueObjects.add(new GraphQLEnumValueDefinition(
                value.toString(), "Autogen " + value.toString(), value));
    }
    return valueObjects;
}
 
Example #2
Source File: TypeGeneratorWithFieldsGenIntegrationTest.java    From graphql-java-type-generator with MIT License 6 votes vote down vote up
@Test
public void testEnum() {
    logger.debug("testEnum");
    Object enumObj = testContext.getOutputType(graphql.java.generator.Enum.class);
    Assert.assertThat(enumObj, instanceOf(GraphQLEnumType.class));
    Matcher<Iterable<GraphQLEnumValueDefinition>> hasItemsMatcher =
            hasItems(
                    hasProperty("name", is("A")),
                    hasProperty("name", is("B")),
                    hasProperty("name", is("C")));
    assertThat(((GraphQLEnumType)enumObj).getValues(), hasItemsMatcher);
    
    enumObj = testContext.getOutputType(graphql.java.generator.EmptyEnum.class);
    Assert.assertThat(enumObj, instanceOf(GraphQLEnumType.class));
    assertThat(((GraphQLEnumType)enumObj).getValues(),
            instanceOf(List.class));
    assertThat(((GraphQLEnumType)enumObj).getValues().size(),
            is(0));
}
 
Example #3
Source File: DefaultTypeInfoGenerator.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
@Override
public GraphqlTypeComparatorRegistry generateComparatorRegistry(AnnotatedType type, MessageBundle messageBundle) {
    if (!isOrdered(type)) {
        return DEFAULT_REGISTRY;
    }
    return new GraphqlTypeComparatorRegistry() {
        @Override
        public <T extends GraphQLSchemaElement> Comparator<? super T> getComparator(GraphqlTypeComparatorEnvironment env) {
            if (env.getElementType().equals(GraphQLFieldDefinition.class)) {
                return comparator(getFieldOrder(type, messageBundle), env);
            }
            if (env.getElementType().equals(GraphQLInputObjectField.class)
                    || env.getElementType().equals(GraphQLEnumValueDefinition.class)) {
                return comparator(getInputFieldOrder(type, messageBundle), env);
            }
            return DEFAULT_REGISTRY.getComparator(env);
        }
    };
}
 
Example #4
Source File: GraphQLDocumentationProvider.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@Nullable
private String getEnumValueDocumentation(GraphQLSchema schema, GraphQLEnumValue parent) {
    final String enumName = GraphQLPsiUtil.getTypeName(parent, null);
    if (enumName != null) {
        graphql.schema.GraphQLType schemaType = schema.getType(enumName);
        if (schemaType instanceof GraphQLEnumType) {
            final String enumValueName = parent.getName();
            final StringBuilder result = new StringBuilder().append(DEFINITION_START);
            result.append(enumName).append(".").append(enumValueName);
            result.append(DEFINITION_END);
            for (GraphQLEnumValueDefinition enumValueDefinition : ((GraphQLEnumType) schemaType).getValues()) {
                if (Objects.equals(enumValueDefinition.getName(), enumValueName)) {
                    final String description = enumValueDefinition.getDescription();
                    if (description != null) {
                        result.append(CONTENT_START);
                        result.append(GraphQLDocumentationMarkdownRenderer.getDescriptionAsHTML(description));
                        result.append(CONTENT_END);
                        return result.toString();
                    }
                }
            }
            return result.toString();
        }
    }
    return null;
}
 
Example #5
Source File: FederationSdlPrinter.java    From federation-jvm with MIT License 5 votes vote down vote up
private TypePrinter<GraphQLEnumType> enumPrinter() {
    return (out, type, visibility) -> {
        if (isIntrospectionType(type)) {
            return;
        }

        GraphqlTypeComparatorEnvironment environment = GraphqlTypeComparatorEnvironment.newEnvironment()
                .parentType(GraphQLEnumType.class)
                .elementType(GraphQLEnumValueDefinition.class)
                .build();
        Comparator<? super GraphQLSchemaElement> comparator = options.comparatorRegistry.getComparator(environment);

        if (shouldPrintAsAst(type.getDefinition())) {
            printAsAst(out, type.getDefinition(), type.getExtensionDefinitions());
        } else {
            printComments(out, type, "");
            out.format("enum %s%s", type.getName(), directivesString(GraphQLEnumType.class, type.getDirectives()));
            List<GraphQLEnumValueDefinition> values = type.getValues()
                    .stream()
                    .sorted(comparator)
                    .collect(toList());
            if (values.size() > 0) {
                out.format(" {\n");
                for (GraphQLEnumValueDefinition enumValueDefinition : values) {
                    printComments(out, enumValueDefinition, "  ");
                    List<GraphQLDirective> enumValueDirectives = enumValueDefinition.getDirectives();
                    if (enumValueDefinition.isDeprecated()) {
                        enumValueDirectives = addDeprecatedDirectiveIfNeeded(enumValueDirectives);
                    }
                    out.format("  %s%s\n", enumValueDefinition.getName(), directivesString(GraphQLEnumValueDefinition.class, enumValueDirectives));
                }
                out.format("}");
            }
            out.format("\n\n");
        }
    };
}
 
Example #6
Source File: SchemaToProto.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
private static String toEnum(GraphQLEnumType type) {
  ArrayList<String> values = new ArrayList<>();
  int i = 0;
  for (GraphQLEnumValueDefinition value : type.getValues()) {
    if (value.getName().equals("UNRECOGNIZED")) {
      continue;
    }
    values.add(String.format("%s = %d;", value.getName(), i));
    i++;
  }
  return String.format(
      "message %s {\n %s\n enum Enum {\n%s\n}\n}",
      type.getName(), getJspb(type), Joiner.on("\n").join(values));
}
 
Example #7
Source File: ProtoToGqlTest.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
@Test
public void convertShouldWorkForEnums() {
  GraphQLEnumType result =
      ProtoToGql.convert(TestEnum.getDescriptor(), SchemaOptions.defaultOptions());
  assertThat(result.getName())
      .isEqualTo("javatests_com_google_api_graphql_rejoiner_proto_Proto2_TestEnum");
  assertThat(result.getValues()).hasSize(3);
  assertThat(result.getValues().stream().map(GraphQLEnumValueDefinition::getName).toArray())
      .asList()
      .containsExactly("UNKNOWN", "FOO", "BAR");
}
 
Example #8
Source File: FullTypeGenerator.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
protected GraphQLEnumType generateEnumType(Object object) {
    String typeName = getGraphQLTypeNameOrIdentityCode(object);

    List<GraphQLEnumValueDefinition> enumValues = getEnumValues(object);
    if (enumValues == null) {
        return null;
    }
    GraphQLEnumType.Builder builder = newEnum();
    builder.name(typeName);
    builder.description(getTypeDescription(object));
    for (GraphQLEnumValueDefinition value : enumValues) {
        builder.value(value.getName(), value.getValue(), value.getDescription());
    }
    return builder.build();
}
 
Example #9
Source File: EnumMapper.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private void addOptions(GraphQLEnumType.Builder enumBuilder, AnnotatedType javaType, OperationMapper operationMapper, BuildContext buildContext) {
    MessageBundle messageBundle = buildContext.messageBundle;
    Arrays.stream((Enum[]) ClassUtils.getRawType(javaType.getType()).getEnumConstants())
            .map(enumConst -> (Enum<?>) enumConst)
            .forEach(enumConst -> enumBuilder.value(GraphQLEnumValueDefinition.newEnumValueDefinition()
                    .name(getValueName(enumConst, messageBundle))
                    .value(enumConst)
                    .description(getValueDescription(enumConst, messageBundle))
                    .deprecationReason(getValueDeprecationReason(enumConst, messageBundle))
                    .withDirectives(getValueDirectives(enumConst, operationMapper, buildContext))
                    .build()));
}
 
Example #10
Source File: ProtoToGqlTest.java    From rejoiner with Apache License 2.0 4 votes vote down vote up
@Test
public void checkComments() {

  String DEFAULT_DESCRIPTOR_SET_FILE_LOCATION = "META-INF/proto/descriptor_set.desc";

  ImmutableMap<String, String> comments =
      DescriptorSet.getCommentsFromDescriptorFile(
          DescriptorSet.class
              .getClassLoader()
              .getResourceAsStream(DEFAULT_DESCRIPTOR_SET_FILE_LOCATION));

  SchemaOptions.Builder schemaOptionsBuilder = SchemaOptions.builder();
  schemaOptionsBuilder.commentsMapBuilder().putAll(comments);
  GraphQLObjectType result =
      ProtoToGql.convert(Proto1.getDescriptor(), null, schemaOptionsBuilder.build());

  GraphQLFieldDefinition intFieldGraphQLFieldDefinition = result.getFieldDefinition("intField");
  assertThat(intFieldGraphQLFieldDefinition).isNotNull();
  assertThat(
          intFieldGraphQLFieldDefinition
              .getDescription()
              .equals("Some leading comment. Some trailing comment"))
      .isTrue();

  GraphQLFieldDefinition camelCaseNameGraphQLFieldDefinition =
      result.getFieldDefinition("camelCaseName");
  assertThat(camelCaseNameGraphQLFieldDefinition).isNotNull();
  assertThat(camelCaseNameGraphQLFieldDefinition.getDescription().equals("Some leading comment"))
      .isTrue();

  GraphQLFieldDefinition testProtoNameGraphQLFieldDefinition =
      result.getFieldDefinition("testProto");
  assertThat(testProtoNameGraphQLFieldDefinition).isNotNull();
  assertThat(testProtoNameGraphQLFieldDefinition.getDescription().equals("Some trailing comment"))
      .isTrue();

  GraphQLEnumType nestedEnumType = ProtoToGql.convert(TestEnum.getDescriptor(), schemaOptionsBuilder.build());

  GraphQLEnumValueDefinition graphQLFieldDefinition = nestedEnumType.getValue("UNKNOWN");
  assertThat(graphQLFieldDefinition.getDescription().equals("Some trailing comment")).isTrue();

  GraphQLObjectType nestedMessageType =
      ProtoToGql.convert(Proto2.NestedProto.getDescriptor(), null, schemaOptionsBuilder.build());

  assertThat(nestedMessageType.getDescription().equals("Nested type comment")).isTrue();

  GraphQLFieldDefinition nestedFieldGraphQLFieldDefinition =
      nestedMessageType.getFieldDefinition("nestedId");
  assertThat(nestedFieldGraphQLFieldDefinition).isNotNull();
  assertThat(nestedFieldGraphQLFieldDefinition.getDescription().equals("Some nested id"))
      .isTrue();

  GraphQLEnumType enumType =
      ProtoToGql.convert(TestProto.TestEnumWithComments.getDescriptor(), schemaOptionsBuilder.build());

  graphQLFieldDefinition = enumType.getValue("FOO");
  assertThat(graphQLFieldDefinition.getDescription().equals("Some trailing comment")).isTrue();
}
 
Example #11
Source File: TypeGenerator.java    From graphql-java-type-generator with MIT License 4 votes vote down vote up
protected List<GraphQLEnumValueDefinition> getEnumValues(Object object) {
    return getStrategies().getEnumValuesStrategy().getEnumValueDefinitions(object);
}
 
Example #12
Source File: EnumValuesStrategy.java    From graphql-java-type-generator with MIT License 2 votes vote down vote up
/**
 * 
 * @param object A representative "type" object, the exact type of which is contextual
 * @return Must return null if not an enum.
 */
List<GraphQLEnumValueDefinition> getEnumValueDefinitions(Object object);