Skip to content

Commit

Permalink
Adding Java "scalar" return types
Browse files Browse the repository at this point in the history
  • Loading branch information
siderakis committed Mar 27, 2018
1 parent 2e2052e commit 763bde0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
Expand Up @@ -29,6 +29,7 @@
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Message;
import graphql.Scalars;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentBuilder;
Expand Down Expand Up @@ -81,12 +82,15 @@ protected ImmutableList<GraphQLFieldDefinition> extraMutations() {
protected void addQuery(GraphQLFieldDefinition query) {
allQueriesInModule.add(query);
}

protected void addMutation(GraphQLFieldDefinition mutation) {
allMutationsInModule.add(mutation);
}

protected void addQueryList(List<GraphQLFieldDefinition> queries) {
allQueriesInModule.addAll(queries);
}

protected void addMutationList(List<GraphQLFieldDefinition> mutations) {
allMutationsInModule.addAll(mutations);
}
Expand Down Expand Up @@ -411,22 +415,39 @@ private GraphQLFieldDefinition methodToFieldDefinition(
}
}

ImmutableMap<java.lang.reflect.Type, GraphQLOutputType> javaTypeToScalarMap =
ImmutableMap.of(
String.class, Scalars.GraphQLString,
Integer.class, Scalars.GraphQLInt,
Boolean.class, Scalars.GraphQLBoolean,
Long.class, Scalars.GraphQLLong,
Float.class, Scalars.GraphQLFloat);

private GraphQLOutputType getReturnType(Method method)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// Currently it's assumed the response is of type Message or ListenableFuture<? extends
// Message>.
// Currently it's assumed the response is of type Message, ListenableFuture<? extends
// Message>, ImmutableList<Message>, ListenableFuture<ImmutableList<? extend Message>>, oe
// any Scalar type.

// Assume Message
// Assume Message or Scalar
if (!(method.getGenericReturnType() instanceof ParameterizedType)) {
@SuppressWarnings("unchecked")
Class<? extends Message> responseClass = (Class<? extends Message>) method.getReturnType();
Descriptor responseDescriptor =
(Descriptor) responseClass.getMethod("getDescriptor").invoke(null);
referencedDescriptors.add(responseDescriptor);
return ProtoToGql.getReference(responseDescriptor);
java.lang.reflect.Type returnType = method.getReturnType();
if (returnType instanceof Message) {
@SuppressWarnings("unchecked")
Class<? extends Message> responseClass = (Class<? extends Message>) method.getReturnType();
Descriptor responseDescriptor =
(Descriptor) responseClass.getMethod("getDescriptor").invoke(null);
referencedDescriptors.add(responseDescriptor);
return ProtoToGql.getReference(responseDescriptor);
}
if (javaTypeToScalarMap.containsKey(returnType)) {
return javaTypeToScalarMap.get(returnType);
}
throw new RuntimeException("Unknown scalar type: " + returnType.getTypeName());
}

ParameterizedType genericReturnType = (ParameterizedType) method.getGenericReturnType();
// TODO: handle collections of Java Scalars

// Assume ListenableFuture<ImmutableList<? extends Message>>
java.lang.reflect.Type genericTypeValue = genericReturnType.getActualTypeArguments()[0];
Expand Down
Expand Up @@ -14,27 +14,28 @@

package com.google.api.graphql.schema.protobuf;

import com.google.api.graphql.rejoiner.Arg;
import com.google.api.graphql.rejoiner.SchemaModification;
import com.google.api.graphql.rejoiner.SchemaModule;
import com.google.api.graphql.rejoiner.Type;
import com.google.api.graphql.rejoiner.TypeModification;
import com.google.protobuf.Timestamp;
import graphql.Scalars;
import graphql.schema.GraphQLFieldDefinition;

import java.time.Instant;
import java.time.ZoneId;

public final class TimestampSchemaModule extends SchemaModule {

@SchemaModification
TypeModification isoString =
Type.find(Timestamp.getDescriptor())
.addField(
GraphQLFieldDefinition.newFieldDefinition()
.name("iso")
.type(Scalars.GraphQLString)
.dataFetcher(
env ->
Instant.ofEpochSecond(env.<Timestamp>getSource().getSeconds()).toString())
.build());
@SchemaModification(addField = "iso", onType = Timestamp.class)
String isoString(Timestamp timestamp) {
return Instant.ofEpochSecond(timestamp.getSeconds()).toString();
}

@SchemaModification(addField = "afterNow", onType = Timestamp.class)
Boolean isAfterNow(Timestamp timestamp) {
return Instant.ofEpochSecond(timestamp.getSeconds()).isAfter(Instant.now());
}

@SchemaModification(addField = "afterNow", onType = Timestamp.class)
String localTime(Timestamp timestamp, @Arg("timezone") String timezone) {
return Instant.ofEpochSecond(timestamp.getSeconds()).atZone(ZoneId.of(timezone)).toString();
}
}

0 comments on commit 763bde0

Please sign in to comment.