ysugimoto / grpc-graphql-gateway

A protoc plugin that generates graphql execution code from Protocol Buffers.
MIT License
382 stars 50 forks source link

Implement nested query #24

Closed ysugimoto closed 4 years ago

ysugimoto commented 4 years ago

This PR implement nested query resolving.

New graphql.proto option

message GraphqlField {
  bool required = 1;
  string name = 2;
  string default = 3;
  bool omit = 4;
+  string resolver = 5;
}

A new field of resolve which resolves as a nested query.

Example usage

syntax = "proto3";

import "graphql.proto";

option go_package = ".;echo";

service Echo {
  option (graphql.service) = {
    host: "localhost:50051"
    insecure: true
  };

  rpc Echo (EchoRequest) returns (EchoResponse) {
    option (graphql.schema) = {
      type: QUERY
      name: "echo"
    };
  }

  rpc GetMember(GetMessageRequest) returns (Member) {
    option (graphql.schema) = {
      type: RESOLVER  // when define as "RESOLVER", it is used for nested query resolving, not in public query
      name: "getMember"
    };
  }
}

message EchoRequest {
}

message EchoResponse {
  int64 id = 1;
   // Define field with "resolve" field option. The value should be query name which is defined as "RESOLVER".
  Member member = 2 [(graphql.field).resolve = "getMember"];
}

message GetMessageRequest {
  // This field value is passed via EchoResponse.id
  int64 id = 1;
}

message Member {
  string name = 1;
  int64 id = 2;
}

And then GraphQL query should be:

curl http://localhost:8000 -d '
{
  echo {
    member {
      id
      name
    }
  }
}'

For example, the response should be:

{"data":{"echo":{"member":{"id":4,"name":"foobar"}}}}