google / rejoiner

Generates a unified GraphQL schema from gRPC microservices and other Protobuf sources
https://google.github.io/rejoiner/
Apache License 2.0
3.67k stars 139 forks source link

google_protobuf_Any value string #101

Closed DianaSuvorova closed 4 years ago

DianaSuvorova commented 4 years ago

The data transferred for the google_protobuf_Any looks like the following:

     "reuslt": {
          "typeUrl": "correctTypeURL",
          "value": "<ByteString@756fc97d size=9>"
        }

typeUrl string is correct while the value is in the format that can't be decoded. This looks like a result of ByteString.toString().

Is there a way to do actual byte array to string decoding on rejoiner side and transfer it as a value.

DianaSuvorova commented 4 years ago

Here is what I ended up doing. Not sure if it is the most optimal solution. But this works. Thanks @siderakis.

  DataFetcher anyValueHexFetcher =
      new DataFetcher<String>() {
        @Override
        public String get(DataFetchingEnvironment environment) {
          return Hex.encodeHexString(
              ((Any) ((DataFetchingEnvironmentImpl) environment).getSource())
                  .getValue()
                  .toByteArray());
        }
      };

  @SchemaModification
  TypeModification typeModification =
      Type.find("google_protobuf_Any")
          .addField(
              GraphQLFieldDefinition.newFieldDefinition()
                  .name("valueHEX")
                  .type(Scalars.GraphQLString)
                  .dataFetcher(anyValueHexFetcher)
                  .build());