Closed numToStr closed 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
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! 🎉
Is there any documentation for using official graphql library (https://github.com/graphql-go/graphql) with fiber?
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.
TnX
@kockok are you able to fix it?
@kockok are you able to fix it?
No, switched back to gin
.
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?
Could you show us what gqlgen subscriptions are?
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
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)
}
To use @ftomza 's code on fiber/v2, instead of ctx.Fasthttp
you have to use ctx.Context()
.
@ftomza have you solved for subscriptions?
To use @ftomza 's code on fiber/v2, instead of
ctx.Fasthttp
you have to usectx.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.
@ftomza gqlgen is terrible, it s better to use graphql-go for code first
@kamalkech Got an example using GoFiber?
@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\"}}"
@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
@kamalkech what libs in rust did you use for this case?
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