99designs / gqlgen

go generate based graphql server library
https://gqlgen.com
MIT License
9.99k stars 1.17k forks source link

Add support for gofiber #1664

Open frederikhors opened 3 years ago

frederikhors commented 3 years ago

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.

efectn commented 3 years ago

I think gqlgen should support fasthttp as offically.

numToStr commented 3 years ago

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

mtibben commented 3 years ago

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.

frederikhors commented 3 years ago

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.

flintforge commented 3 years ago

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

frederikhors commented 3 years ago

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.

efectn commented 3 years ago

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

shinebayar-g commented 1 year ago

This is a final missing piece of fiber x gqlgen.

ghost commented 1 year ago

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

gaby commented 1 year ago

Any news on this?

shyaaam commented 1 year ago

@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")
}
oSethoum commented 11 months ago

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