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

GET and POST together, the same handler. #1633

Closed
frederikhors opened this issue Nov 8, 2018 · 3 comments
Closed

GET and POST together, the same handler. #1633

frederikhors opened this issue Nov 8, 2018 · 3 comments

Comments

@frederikhors
Copy link

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

I'm using the code below but I have doubts.

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?

Can I just have GET and POST both with the same handler function?

(I opened 99designs/gqlgen#418 for this problem).

@olegsobchuk
Copy link

olegsobchuk commented Nov 8, 2018

Gin uses httprouter router and according to source code from THIS STRING it is impossible.

BTW
I can't even imagine what are you going to do with GET and POST on the same action

@kirides
Copy link

kirides commented Dec 7, 2018

Why don't you just

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

yes, this still repeats the line, but makes modifiying it easier.

I have to agree with @olegsobchuk , in a normal WebAPI GET and POST would do different things, and therefor have different handlers - but people do what they do

@thinkerou
Copy link
Member

	router.GET("/a/b", h)
	router.POST("/a/b", h)

it's ok!

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

4 participants