Gqlgen: subscriptionEndpoint and gin integration.

Created on 8 Nov 2018  路  1Comment  路  Source: 99designs/gqlgen

I'm trying to use gqlgen with gin (https://github.com/gin-gonic/gin).

I'm using the code below but there is a question about the subscription endpoint.

package server

import (
    "myProject/graphql"
    "github.com/99designs/gqlgen/handler"
    "github.com/gin-gonic/gin"
)

func Start() {
    r := gin.Default()

    r.GET("/query", gin.WrapH(handler.Playground("GraphQL playground", "/api")))
    r.GET("/api", gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}}))))
    r.POST("/api", gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}}))))

    r.Run()
}

As you can see I need to repeat the lines:

r.GET("/api", gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}}))))
r.POST("/api", gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}}))))

Is this correct?

Is there a way to have two separate handlers: one for graphql engine and one for subscriptions engine?

Also because in the future I'll need two separate endpoint. And from what I can see here: https://github.com/99designs/gqlgen/blob/4bda3bc1291bdc0bc44e3057b5229d987eeecde2/handler/playground.go#L25-L38

Today there isn't a way to change the default Playground subscriptionEndpoint?

Maybe a config like this below?

r.GET("/query", gin.WrapH(handler.Playground("GraphQL playground", "/api", "/subscriptions")))

Most helpful comment

You probably want to create the handler once and pass the same instance to all of them, otherwise you making copies of a bunch of internal state like the query cache.

gql := gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}})))

r.GET("/api", gql)
r.POST("/api", gql)

Why do you need to split the subscription endpoint out?

>All comments

You probably want to create the handler once and pass the same instance to all of them, otherwise you making copies of a bunch of internal state like the query cache.

gql := gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}})))

r.GET("/api", gql)
r.POST("/api", gql)

Why do you need to split the subscription endpoint out?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bieber picture bieber  路  4Comments

lynntobing picture lynntobing  路  3Comments

msmedes picture msmedes  路  4Comments

itsbalamurali picture itsbalamurali  路  4Comments

bjm88 picture bjm88  路  4Comments