gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
34.05k stars 1.68k forks source link

🔥 Official Graphql support #403

Closed numToStr closed 4 years ago

numToStr commented 4 years ago

Is your feature request related to a problem? Thanks for your all effort for making this 🔥 and ♥️ framework. This is my goto framework when I work with go ✌🏻. And I also work extensively with graphql and It would be cool to have a official graphql plugin 🔥.

Describe the solution you'd like An official graphql plugin.

Additional context Official Javascript implemetation: https://github.com/graphql/graphql-js Fastify.js Implementation: https://github.com/mcollina/fastify-gql

welcome[bot] commented 4 years ago

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

thomasvvugt commented 4 years ago

Hi @numToStr , thanks for bringing up this issue!

We have however discussed this feature request in the past, however we do not think a GraphQL library should be official but rather something the end-user can import into their application.

I do recommend checking out https://github.com/arsmn/gqlgen and https://github.com/Just4Ease/fiber-gqlgen for importing GraphQL with Fiber.

Thank you for opening your first issue! 🎉

rijine commented 4 years ago

Is there any documentation for using official graphql library (https://github.com/graphql-go/graphql) with fiber?

kockok commented 4 years ago

Hi @numToStr , thanks for bringing up this issue!

We have however discussed this feature request in the past, however we do not think a GraphQL library should be official but rather something the end-user can import into their application.

I do recommend checking out https://github.com/arsmn/gqlgen and https://github.com/Just4Ease/fiber-gqlgen for importing GraphQL with Fiber.

Thank you for opening your first issue! 🎉

Can you create an official recipe as to integrate with gqlgen? Especially for the context integration.

https://github.com/arsmn/gqlgen raises errors.

Screenshot 2020-07-17 at 5 35 32 PM

TnX

rijine commented 4 years ago

@kockok are you able to fix it?

kockok commented 4 years ago

@kockok are you able to fix it?

No, switched back to gin.

RobertoOrtis commented 4 years ago

I have implemented gofiber with gqlgen and it is working for queries and mutations; however, subscriptions won't work because I need websockets. I tried to use gofiber websocket but it just not work. Has anyone successfully implemented gqlgen subscriptions with gofiber?

Fenny commented 4 years ago

Could you show us what gqlgen subscriptions are?

RobertoOrtis commented 4 years ago

Could you show us what gqlgen subscriptions are?

Got it working with arsmn's fork for fiber. I have a new issue on: https://github.com/99designs/gqlgen/issues/1281

ftomza commented 4 years ago

Hi! How I solved the problem without subscriptions yet

package main
import (
    "net/http"
    "github.com/99designs/gqlgen/graphql/playground"
    "github.com/99designs/gqlgen/graphql/handler"
    "github.com/gofiber/fiber"
    "github.com/valyala/fasthttp/fasthttpadaptor"
)

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

    app.Post("/query", func(ctx *fiber.Ctx) {
        h := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: &Resolver{}}))
        fasthttpadaptor.NewFastHTTPHandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
            h.ServeHTTP(writer, request)
        })(ctx.Fasthttp)
    })
    app.Get("/", func(ctx *fiber.Ctx) {
        h := playground.Handler("GraphQL", "/query")
        fasthttpadaptor.NewFastHTTPHandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
            h.ServeHTTP(writer, request)
        })(ctx.Fasthttp)
    })
    app.Listen(3000)
}
water-a commented 3 years ago

To use @ftomza 's code on fiber/v2, instead of ctx.Fasthttp you have to use ctx.Context().

frederikhors commented 3 years ago

@ftomza have you solved for subscriptions?

frederikhors commented 3 years ago

https://github.com/99designs/gqlgen/issues/1664

drnocam commented 1 year ago

To use @ftomza 's code on fiber/v2, instead of ctx.Fasthttp you have to use ctx.Context().

app.All("/query", func(ctx *fiber.Ctx) error {
        h := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))

        fasthttpadaptor.NewFastHTTPHandlerFunc(func(writer http.ResponseWriter, request *http.Request) {

            h.ServeHTTP(writer, request)

        })(ctx.Context())

        return nil

    })

worked great. Thank you.

kamalkech commented 1 year ago

@ftomza gqlgen is terrible, it s better to use graphql-go for code first

gaby commented 1 year ago

@kamalkech Got an example using GoFiber?

ghost commented 1 year ago

@kamalkech Got an example using GoFiber?

@gaby The official graphql-go repo has a pretty good example This is what I hacked together quickly:

package main

import (
    "encoding/json"
    "log"

    "github.com/gofiber/fiber/v2"
    "github.com/graphql-go/graphql"
)

func main() {
    fields := graphql.Fields{
        "hello": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                return "world", nil
            },
        },
    }
    rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
    schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
    schema, err := graphql.NewSchema(schemaConfig)
    if err != nil {
        log.Fatalf("failed to create new schema, error: %v", err)
    }

    app := fiber.New()

    app.Post("/", func(ctx *fiber.Ctx) error {
        query := string(ctx.Body())
        params := graphql.Params{Schema: schema, RequestString: query}
        r := graphql.Do(params)
        if len(r.Errors) > 0 {
            ctx.SendString("failed to execute graphql operation")
        }
        rJSON, err := json.Marshal(r)
        if err != nil {
            ctx.SendString("cannot marshal json")
        }
        return ctx.JSON(string(rJSON))
    })

    log.Fatal(app.Listen(":9090"))
}
curl -X POST -d "{hello}" http://localhost:9090

"{\"data\":{\"hello\":\"world\"}}"
kamalkech commented 1 year ago

@cmd777 for my case and my project i switch from golang to rust lang because really community of golang is terrible and most of modules not improved correctly and graphql is one of them

steinathan commented 1 year ago

@kamalkech what libs in rust did you use for this case?