Skip to content

Commit

Permalink
Merge 4db9a81 into a2b747b
Browse files Browse the repository at this point in the history
  • Loading branch information
richard1122 committed Jun 11, 2019
2 parents a2b747b + 4db9a81 commit df7c655
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
Expand Up @@ -152,6 +152,10 @@ static GraphQLArgument createArgument(Descriptor descriptor, String name) {
return GraphQLArgument.newArgument().name(name).type(getInputTypeReference(descriptor)).build();
}

static GraphQLArgument createArgument(EnumDescriptor descriptor, String name) {
return GraphQLArgument.newArgument().name(name).type(ProtoToGql.getReference(descriptor)).build();
}

static String getReferenceName(GenericDescriptor descriptor) {
return "Input_" + ProtoToGql.getReferenceName(descriptor);
}
Expand Down
Expand Up @@ -15,15 +15,19 @@
package com.google.api.graphql.rejoiner;

import com.google.auto.value.AutoValue;
import com.google.common.base.Converter;
import com.google.common.base.Enums;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Message;
import com.google.protobuf.ProtocolMessageEnum;
import graphql.Scalars;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
Expand Down Expand Up @@ -521,8 +525,8 @@ private ImmutableList<MethodMetadata> getMethodMetadata(
listBuilder.add(MethodMetadata.create(function, argument));
}
} else if (isArg(method.getParameterAnnotations()[i])) {
String argName = getArgName(method.getParameterAnnotations()[i]);
if (javaTypeToScalarMap.containsKey(parameterType)) {
String argName = getArgName(method.getParameterAnnotations()[i]);
Function<DataFetchingEnvironment, ?> function =
environment -> environment.getArgument(argName);
GraphQLArgument argument =
Expand All @@ -531,6 +535,18 @@ private ImmutableList<MethodMetadata> getMethodMetadata(
.type(javaTypeToScalarMap.get(parameterType))
.build();
listBuilder.add(MethodMetadata.create(function, argument));
} else if (ProtocolMessageEnum.class.isAssignableFrom(parameterType)) {
Class<? extends Enum> requestClass = (Class<? extends Enum>) parameterType;
Descriptors.EnumDescriptor requestDescriptor =
(Descriptors.EnumDescriptor) requestClass.getMethod("getDescriptor").invoke(null);
Converter converter = Enums.stringConverter(requestClass);
Function<DataFetchingEnvironment, ?> function =
environment -> {
String enumValue = environment.getArgument(argName);
return converter.convert(enumValue);
};
GraphQLArgument argument = GqlInputConverter.createArgument(requestDescriptor, argName);
listBuilder.add(MethodMetadata.create(function, argument));
} else {
throw new RuntimeException("Unknown arg type: " + parameterType.getName());
}
Expand Down
Expand Up @@ -32,6 +32,8 @@
import graphql.GraphQLError;
import graphql.language.SourceLocation;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
Expand Down Expand Up @@ -97,6 +99,11 @@ ListenableFuture<GreetingsResponse> greetings(GreetingsRequest request) {
return Futures.immediateFuture(GreetingsResponse.newBuilder().setId(request.getId()).build());
}

@Query("getAccountWithLanguages")
Greetings.AccountValue accounts(@Arg("language") Greetings.Languages languages) {
return Greetings.AccountValue.newBuilder().setAnEnum(languages).build();
}

@SchemaModification(addField = "extraField", onType = GreetingsResponse.class)
ListenableFuture<ExtraProto> greetingsResponseToExtraProto(
ExtraProto request, GreetingsResponse source) {
Expand Down Expand Up @@ -129,7 +136,7 @@ static class GreetingsAddonSchemaModule extends SchemaModule {

@Test
public void schemaShouldHaveOneQuery() {
assertThat(schema.getQueryType().getFieldDefinitions()).hasSize(6);
assertThat(schema.getQueryType().getFieldDefinitions()).hasSize(7);
}

@Test
Expand All @@ -148,6 +155,14 @@ public void schemaShouldListSync() {
assertThat(((GraphQLList) listOfStuff).getWrappedType()).isInstanceOf(GraphQLNonNull.class);
}

@Test
public void schemaShouldGetAccountWithEnumArgs() {
GraphQLFieldDefinition getAccount =
schema.getQueryType().getFieldDefinition("getAccountWithLanguages");
assertThat(getAccount.getArgument("language").getType()).isInstanceOf(GraphQLEnumType.class);
assertThat(getAccount.getType()).isInstanceOf(GraphQLObjectType.class);
}

@Test
public void schemaShouldHaveNoMutations() {
assertThat(schema.getMutationType()).isNull();
Expand All @@ -164,6 +179,17 @@ public void schemaModificationsShouldBeApplied() {
assertThat(obj.getFieldDefinition("greeting")).isNull();
}

@Test
public void executionQueryWithEnumArgs() {
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
ExecutionInput executionInput =
ExecutionInput.newExecutionInput()
.query("query { getAccountWithLanguages(language: EO) { anEnum } }")
.build();
ExecutionResult executionResult = graphQL.execute(executionInput);
assertThat(executionResult.getErrors()).hasSize(0);
}

@Test
public void handlesRuntimeExceptionMessage() {
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
Expand Down
Expand Up @@ -141,6 +141,33 @@ ListenableFuture<GreetingsResponse> mutationMethod(GreetingsRequest request) {
assertThat(schemaBundle.modifications()).isEmpty();
}

@Test
public void schemaModuleShouldApplyProtoEnumArgs() {
Injector injector =
Guice.createInjector(
new SchemaModule() {

@Mutation("mutationMethodWithArgs")
ListenableFuture<GreetingsResponse> mutationMethod(
GreetingsRequest request, @Arg("myLanguage") Greetings.Languages myLanguage) {
return Futures.immediateFuture(
GreetingsResponse.newBuilder().setId(request.getId()).build());
}
});
SchemaBundle schemaBundle = SchemaBundle.combine(injector.getInstance(KEY));
assertThat(schemaBundle.mutationFields()).hasSize(1);

List<GraphQLArgument> arguments =
schemaBundle.mutationFields().iterator().next().getArguments();
assertThat(arguments).hasSize(2);
assertThat(
arguments
.stream()
.map(argument -> argument.getName())
.collect(ImmutableList.toImmutableList()))
.containsExactly("input", "myLanguage");
}

@Test
public void schemaModuleShouldApplyArgs() {

Expand Down

0 comments on commit df7c655

Please sign in to comment.