Skip to content

Commit

Permalink
Allowing flexible location of the descriptor sets file and other sour…
Browse files Browse the repository at this point in the history
…ces of the comments map.
  • Loading branch information
siderakis committed Aug 16, 2019
1 parent a01f73f commit 6e3c006
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 68 deletions.
Expand Up @@ -25,40 +25,29 @@
import java.util.stream.Collectors;

final class DescriptorSet {
private static final String DEFAULT_DESCRIPTOR_SET_FILE_LOCATION =
"META-INF/proto/descriptor_set.desc";

static final ImmutableMap<String, String> COMMENTS = getCommentsFromDescriptorFile();

private DescriptorSet() {}

private static ImmutableMap<String, String> getCommentsFromDescriptorFile() {
/** Returns a map containing the comments for types and fields. */
public static ImmutableMap<String, String> getCommentsFromDescriptorFile(
InputStream descriptorSetInputStream) {
try {
InputStream is =
DescriptorSet.class
.getClassLoader()
.getResourceAsStream(DEFAULT_DESCRIPTOR_SET_FILE_LOCATION);
DescriptorProtos.FileDescriptorSet descriptors =
DescriptorProtos.FileDescriptorSet.parseFrom(is);
return descriptors
.getFileList()
.stream()
DescriptorProtos.FileDescriptorSet.parseFrom(descriptorSetInputStream);
return descriptors.getFileList().stream()
.flatMap(
fileDescriptorProto -> parseDescriptorFile(fileDescriptorProto).entrySet().stream())
.collect(
ImmutableMap.toImmutableMap(
Map.Entry::getKey, Map.Entry::getValue, (entry, value) -> entry));
Map.Entry::getKey, Map.Entry::getValue, (value1, value2) -> value1));
} catch (IOException ignored) {
}
return ImmutableMap.of();
}

private static Map<String, String> parseDescriptorFile(
DescriptorProtos.FileDescriptorProto descriptor) {
return descriptor
.getSourceCodeInfo()
.getLocationList()
.stream()
return descriptor.getSourceCodeInfo().getLocationList().stream()
.filter(
location ->
!(location.getLeadingComments().isEmpty()
Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
Expand Down Expand Up @@ -99,7 +100,7 @@ public Message createProtoBuf(
return builder.build();
}

GraphQLType getInputType(Descriptor descriptor) {
GraphQLType getInputType(Descriptor descriptor, ImmutableMap<String, String> commentsMap) {
GraphQLInputObjectType.Builder builder =
GraphQLInputObjectType.newInputObject().name(getReferenceName(descriptor));

Expand All @@ -116,11 +117,11 @@ GraphQLType getInputType(Descriptor descriptor) {
inputBuilder.type((GraphQLInputType) fieldType);
}

inputBuilder.description(DescriptorSet.COMMENTS.get(field.getFullName()));
inputBuilder.description(commentsMap.get(field.getFullName()));

builder.field(inputBuilder.build());
}
builder.description(DescriptorSet.COMMENTS.get(descriptor.getFullName()));
builder.description(commentsMap.get(descriptor.getFullName()));
return builder.build();
}

Expand Down
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
Expand All @@ -29,9 +30,11 @@
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -54,9 +57,7 @@ Set<GraphQLType> listTypes() {
}

boolean hasRelayNode() {
return mapping
.values()
.stream()
return mapping.values().stream()
.anyMatch(
type ->
type instanceof GraphQLObjectType
Expand Down Expand Up @@ -89,6 +90,7 @@ public static class Builder {
private final ArrayList<Descriptor> descriptors = new ArrayList<>();
private final ArrayList<EnumDescriptor> enumDescriptors = new ArrayList<>();
private final Set<TypeModification> typeModifications = new HashSet<>();
private ImmutableMap<String, String> commentsMap = ImmutableMap.of();

Builder add(FileDescriptor fileDescriptor) {
fileDescriptors.add(fileDescriptor);
Expand Down Expand Up @@ -118,8 +120,7 @@ Builder add(Collection<TypeModification> modifications) {
ProtoRegistry build() {
ImmutableListMultimap<String, TypeModification> modificationsMap =
ImmutableListMultimap.copyOf(
this.typeModifications
.stream()
this.typeModifications.stream()
.map(
modification ->
new SimpleImmutableEntry<>(modification.getTypeName(), modification))
Expand All @@ -138,7 +139,7 @@ ProtoRegistry build() {

mapping.putAll(
modifyTypes(
getMap(fileDescriptors, descriptors, enumDescriptors, nodeInterface),
getMap(fileDescriptors, descriptors, enumDescriptors, nodeInterface, commentsMap),
modificationsMap));

return new ProtoRegistry(mapping, nodeInterface);
Expand All @@ -148,41 +149,45 @@ private static BiMap<String, GraphQLType> getMap(
List<FileDescriptor> fileDescriptors,
List<Descriptor> descriptors,
List<EnumDescriptor> enumDescriptors,
GraphQLInterfaceType nodeInterface) {
HashBiMap<String, GraphQLType> mapping = HashBiMap.create(getEnumMap(enumDescriptors));
GraphQLInterfaceType nodeInterface,
ImmutableMap<String, String> commentsMap) {
HashBiMap<String, GraphQLType> mapping =
HashBiMap.create(getEnumMap(enumDescriptors, commentsMap));
LinkedList<Descriptor> loop = new LinkedList<>(descriptors);

Set<FileDescriptor> fileDescriptorSet = extractDependencies(fileDescriptors);

for (FileDescriptor fileDescriptor : fileDescriptorSet) {
loop.addAll(fileDescriptor.getMessageTypes());
mapping.putAll(getEnumMap(fileDescriptor.getEnumTypes()));
mapping.putAll(getEnumMap(fileDescriptor.getEnumTypes(), commentsMap));
}

while (!loop.isEmpty()) {
Descriptor descriptor = loop.pop();
if (!mapping.containsKey(descriptor.getFullName())) {
mapping.put(
ProtoToGql.getReferenceName(descriptor),
ProtoToGql.convert(descriptor, nodeInterface));
ProtoToGql.convert(descriptor, nodeInterface, commentsMap));
GqlInputConverter inputConverter =
GqlInputConverter.newBuilder().add(descriptor.getFile()).build();
mapping.put(
GqlInputConverter.getReferenceName(descriptor),
inputConverter.getInputType(descriptor));
inputConverter.getInputType(descriptor, commentsMap));
loop.addAll(descriptor.getNestedTypes());

mapping.putAll(getEnumMap(descriptor.getEnumTypes()));
mapping.putAll(getEnumMap(descriptor.getEnumTypes(), commentsMap));
}
}
return ImmutableBiMap.copyOf(mapping);
}

private static BiMap<String, GraphQLType> getEnumMap(Iterable<EnumDescriptor> descriptors) {
private static BiMap<String, GraphQLType> getEnumMap(
Iterable<EnumDescriptor> descriptors, ImmutableMap<String, String> commentsMap) {
HashBiMap<String, GraphQLType> mapping = HashBiMap.create();
for (EnumDescriptor enumDescriptor : descriptors) {
mapping.put(
ProtoToGql.getReferenceName(enumDescriptor), ProtoToGql.convert(enumDescriptor));
ProtoToGql.getReferenceName(enumDescriptor),
ProtoToGql.convert(enumDescriptor, commentsMap));
}
return mapping;
}
Expand All @@ -207,5 +212,10 @@ private static BiMap<String, GraphQLType> modifyTypes(
}
return result;
}

public Builder setComments(ImmutableMap<String, String> commentsMap) {
this.commentsMap = commentsMap;
return this;
}
}
}
Expand Up @@ -82,7 +82,8 @@ private ProtoToGql() {}
private static final ImmutableList<GraphQLFieldDefinition> STATIC_FIELD =
ImmutableList.of(newFieldDefinition().type(GraphQLString).name("_").staticValue("-").build());

private static GraphQLFieldDefinition convertField(FieldDescriptor fieldDescriptor) {
private static GraphQLFieldDefinition convertField(
FieldDescriptor fieldDescriptor, ImmutableMap<String, String> commentsMap) {
final String fieldName = fieldDescriptor.getName();
final String convertedFieldName =
fieldName.contains("_") ? UNDERSCORE_TO_CAMEL.convert(fieldName) : fieldName;
Expand All @@ -105,7 +106,7 @@ private static GraphQLFieldDefinition convertField(FieldDescriptor fieldDescript
.type(convertType(fieldDescriptor))
.dataFetcher(dataFetcher)
.name(fieldDescriptor.getJsonName());
builder.description(DescriptorSet.COMMENTS.get(fieldDescriptor.getFullName()));
builder.description(commentsMap.get(fieldDescriptor.getFullName()));
if (fieldDescriptor.getOptions().hasDeprecated()
&& fieldDescriptor.getOptions().getDeprecated()) {
builder.deprecate("deprecated in proto");
Expand Down Expand Up @@ -157,9 +158,14 @@ static GraphQLOutputType convertType(FieldDescriptor fieldDescriptor) {
}
}

static GraphQLObjectType convert(Descriptor descriptor, GraphQLInterfaceType nodeInterface) {
static GraphQLObjectType convert(
Descriptor descriptor,
GraphQLInterfaceType nodeInterface,
ImmutableMap<String, String> commentsMap) {
ImmutableList<GraphQLFieldDefinition> graphQLFieldDefinitions =
descriptor.getFields().stream().map(ProtoToGql::convertField).collect(toImmutableList());
descriptor.getFields().stream()
.map(field -> ProtoToGql.convertField(field, commentsMap))
.collect(toImmutableList());

Optional<GraphQLFieldDefinition> relayId =
descriptor.getFields().stream()
Expand Down Expand Up @@ -203,18 +209,19 @@ static GraphQLObjectType convert(Descriptor descriptor, GraphQLInterfaceType nod

return GraphQLObjectType.newObject()
.name(getReferenceName(descriptor))
.description(DescriptorSet.COMMENTS.get(descriptor.getFullName()))
.description(commentsMap.get(descriptor.getFullName()))
.fields(graphQLFieldDefinitions.isEmpty() ? STATIC_FIELD : graphQLFieldDefinitions)
.build();
}

static GraphQLEnumType convert(EnumDescriptor descriptor) {
static GraphQLEnumType convert(
EnumDescriptor descriptor, ImmutableMap<String, String> commentsMap) {
GraphQLEnumType.Builder builder = GraphQLEnumType.newEnum().name(getReferenceName(descriptor));
for (EnumValueDescriptor value : descriptor.getValues()) {
builder.value(
value.getName(),
value.getName(),
DescriptorSet.COMMENTS.get(value.getFullName()),
commentsMap.get(value.getFullName()),
value.getOptions().getDeprecated() ? "deprecated in proto" : null);
}
return builder.build();
Expand Down
Expand Up @@ -4,6 +4,7 @@

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Descriptors;
import graphql.relay.Relay;
Expand All @@ -20,14 +21,17 @@ public abstract class SchemaBundle {

public GraphQLSchema toSchema() {
Map<String, ? extends Function<String, Object>> nodeDataFetchers =
nodeDataFetchers()
.stream()
nodeDataFetchers().stream()
.collect(Collectors.toMap(e -> e.getClassName(), Function.identity()));

GraphQLObjectType.Builder queryType = newObject().name("QueryType").fields(queryFields());

ProtoRegistry protoRegistry =
ProtoRegistry.newBuilder().addAll(fileDescriptors()).add(modifications()).build();
ProtoRegistry.newBuilder()
.setComments(commentsMap())
.addAll(fileDescriptors())
.add(modifications())
.build();

if (protoRegistry.hasRelayNode()) {
queryType.field(
Expand Down Expand Up @@ -74,6 +78,8 @@ public GraphQLSchema toSchema() {

public abstract ImmutableList<NodeDataFetcher> nodeDataFetchers();

public abstract ImmutableMap<String, String> commentsMap();

public static Builder builder() {
return new AutoValue_SchemaBundle.Builder();
}
Expand All @@ -87,6 +93,7 @@ public static SchemaBundle combine(Collection<SchemaBundle> schemaBundles) {
builder.modificationsBuilder().addAll(schemaBundle.modifications());
builder.fileDescriptorsBuilder().addAll(schemaBundle.fileDescriptors());
builder.nodeDataFetchersBuilder().addAll(schemaBundle.nodeDataFetchers());
builder.commentsMapBuilder().putAll(schemaBundle.commentsMap());
});
return builder.build();
}
Expand All @@ -103,6 +110,8 @@ public abstract static class Builder {

public abstract ImmutableList.Builder<NodeDataFetcher> nodeDataFetchersBuilder();

public abstract ImmutableMap.Builder<String, String> commentsMapBuilder();

public abstract SchemaBundle build();
}
}

0 comments on commit 6e3c006

Please sign in to comment.