1

I have below snippet to server the graphql server over http listener. Could you help me how I can do similar implementation with GRPC server? I mean serving graphQL server over grpc server?

func Run() {
    var cfg AppConfig
    cfg.ProductURL = "0.0.0.0:8001"
    err := envconfig.Process("", &cfg)
    if err != nil {
        log.Fatal(err)
    }
    s, err := NewGraphQLServer(cfg.ProductURL)
    if err != nil {
        log.Fatal(err)
    }

    http.Handle("/graphql", handler.GraphQL(gql.NewExecutableSchema(gql.Config{
        Resolvers: s,
    })))
    http.Handle("/playground", handler.Playground("test", "/graphql"))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

// NewGraphQLServer is to create get the connections to other services
func NewGraphQLServer(productURL string) (*resolvers.Resolver, error) {
    // Connect to Product Service
    productClient, err := service.NewProductClient(productURL)
    if err != nil {
        return nil, err
    }

    return &resolvers.Resolver{
        ProductClient: productClient,
    }, nil
}```
  • GraphQL & gRPC serve 2 different purposes. If you want to use gRPC as a medium of data communication then you have to define Proto schema which is equivalent to GraphQL schema. Most importantly what's the use-case you want to solve with this implementation? – Sougata Pal Aug 10 '20 at 08:11
  • I have several APIs built on gRPC server and those APIs communicates through protoc buffer. I am using gRPC as a medium between front end(mobile & desktop) and database. I chose gRPC to reduce the latency and for higher data compression. As number of backend APIs are increasing and front end needs different fields for different screens from a single schema, I would like to use graphQL for this scenario where user can query for the selective fields. I am trying to implement graphQL server in golang. Is it possible to implement graphQL functionality in a gRPC server? – SantoshVysyaraju Aug 11 '20 at 00:55
  • There's no way you can stitch both of them together as a single protocol, but ofcourse you can put a GraphQL gateway infront of the gRPC services, that way client can selectively pull the data as per requirement. Please let me know if that would be satisfy your use case. – Sougata Pal Aug 11 '20 at 04:11
  • If I keep GraphQL infront then do you think communication will be in protoc? – SantoshVysyaraju Aug 11 '20 at 13:35
  • Nope, It will be plaintext i.e in JSON. My suggestion would be to use gRPC based communications because for it's compressed binary nature. Your transmission delay problem will anyway be resolved with Protoc. From my experience, I can say, gRPC communication is atleast 3.5 times faster than standard REST/GraphQL. – Sougata Pal Aug 11 '20 at 17:23
  • Thank you very much Sougata for pitch in and helping me. I will plan on take up gRPC. – SantoshVysyaraju Aug 12 '20 at 22:20
  • Please keep me posted if you need further help in the implementation architecture. Good luck. – Sougata Pal Aug 13 '20 at 02:50

0 Answers0