Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriptionEndpoint and gin integration. #418

Closed
frederikhors opened this issue Nov 8, 2018 · 1 comment
Closed

subscriptionEndpoint and gin integration. #418

frederikhors opened this issue Nov 8, 2018 · 1 comment

Comments

@frederikhors
Copy link
Collaborator

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:

<script type="text/javascript">
window.addEventListener('load', function (event) {
const root = document.getElementById('root');
root.classList.add('playgroundIn');
const wsProto = location.protocol == 'https:' ? 'wss:' : 'ws:'
GraphQLPlayground.init(root, {
endpoint: location.protocol + '//' + location.host + '{{.endpoint}}',
subscriptionsEndpoint: wsProto + '//' + location.host + '{{.endpoint }}',
settings: {
'request.credentials': 'same-origin'
}
})
})
</script>

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")))
@vektah
Copy link
Collaborator

vektah commented Nov 8, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants