GET and POST together, the same handler.

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).

1 possible answer(s) on “GET and POST together, the same handler.

  1. 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