gqlgen icon indicating copy to clipboard operation
gqlgen copied to clipboard

Add support for gofiber

Open frederikhors opened this issue 2 years ago • 12 comments

I think we should add support for framework like GoFiber.

Someone has already tried: https://github.com/Just4Ease/fiber-gqlgen/blob/master/handler.go.

Here a discussion on gofiber: https://github.com/gofiber/fiber/issues/403.

frederikhors avatar Oct 16 '21 17:10 frederikhors

I think gqlgen should support fasthttp as offically.

efectn avatar Oct 16 '21 17:10 efectn

I originally requested the support and if gofiber gets the official from gqlgen then it will just make gofiber more powerful.

numToStr avatar Oct 16 '21 18:10 numToStr

I'm not a fan of fasthttp/gofiber - any application using a datastore for a backend will not see the performance gains over the stdlib this project claims.

However, I note there's a forked version of gqlgen with support at https://github.com/arsmn/fastgql.

And if someone wanted to implement, gqlgen's plugin system should make this possible.

mtibben avatar Oct 17 '21 23:10 mtibben

However, I note there's a forked version of gqlgen with support at https://github.com/arsmn/fastgql.

Is old, doesn't work and there is no docs.

frederikhors avatar Oct 18 '21 07:10 frederikhors

And not to confuse with the other fastgql, whose filtering, pagination, sorting and aggregation model is fine.

flintforge avatar Oct 20 '21 03:10 flintforge

And not to confuse with the other fastgql, whose filtering, pagination, sorting and aggregation model is fine.

I think this is still not fully "supported". The only hope is to have something official, perhaps among the plugins officially supported by gqlgen.

frederikhors avatar Oct 20 '21 12:10 frederikhors

It seems adding partial fasthttp support is really hard if we dont think fork. Code has too many hardcoded net/http connections.

efectn avatar Oct 20 '21 13:10 efectn

This is a final missing piece of fiber x gqlgen.

shinebayar-g avatar Nov 28 '22 19:11 shinebayar-g

It will be very nice to have official support of Fiber/fasthttp.

ghost avatar Mar 03 '23 11:03 ghost

Any news on this?

gaby avatar Jun 12 '23 11:06 gaby

@gaby It worked for me using the following code. This boilerplate code was written by ChatGPT (while i slightly edited the code to include the wrapHandler func to make the bridge b/w the graphql handler and fiber with fasthttpadaptor).

This works for me, at least I didn't run into any complications so far. You can try and let me know. Also, I didn't test this with subscriptions.

package main

import (
	"your-module/graph" // Import the generated code from gqlgen

	"net/http"

	"github.com/gofiber/fiber/v2"
	"github.com/99designs/gqlgen/graphql/handler"
	"github.com/99designs/gqlgen/graphql/playground"
	"github.com/valyala/fasthttp/fasthttpadaptor"
)

func wrapHandler(f func(http.ResponseWriter, *http.Request)) func(ctx *fiber.Ctx) {
	return func(ctx *fiber.Ctx) {
		fasthttpadaptor.NewFastHTTPHandler(http.HandlerFunc(f))(ctx.Context())
	}
}

func main() {
	app := fiber.New()

	// Create a gqlgen handler
	h := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))


	// Serve GraphQL API
	app.Post("/graphql", func(c *fiber.Ctx) error {
		wrapHandler(h.ServeHTTP)(c)
		return nil
	})

	// Serve GraphQL Playground
	app.Get("/playground", func(c *fiber.Ctx) error {
		wrapHandler(playground.Handler("GraphQL", "/graphql"))(c)
		return nil
	})

	// Start the server
	app.Listen(":3000")
}

shyaaam avatar Jul 02 '23 12:07 shyaaam

@shyaaam when using the adapter, does it mean the speed decreases from gofiber to net/http?

oSethoum avatar Dec 15 '23 16:12 oSethoum