aV
04/21/2023, 5:11 AMgo
// RegisterRoutes registers the routes
func (r *Router) RegisterRoutes() {
r.App.All("/query", gqlQueryHandler(r.DB.Ent))
r.App.Get("/playground", gqlPlaygroundHandler("Playground", "/query"))
}
// gqlQueryHandler handles graphql queries
func gqlQueryHandler(ent *ent.Client) fiber.Handler {
return func(ctx *fiber.Ctx) error {
h := handler.NewDefaultServer(graph.NewSchema(ctx, ent))
adaptor.HTTPHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
})(ctx)
return nil
}
}
// gqlPlaygroundHandler handles graphql playground
func gqlPlaygroundHandler(title string, endpoint string) fiber.Handler {
return adaptor.HTTPHandlerFunc(playground.Handler(title, endpoint))
}
here is my createAccount mutation resolver:
go
// CreateAccount is the resolver for the createAccount field.
func (r *mutationResolver) CreateAccount(ctx context.Context, input ent.CreateAccountInput) (*ent.Account, error) {
// Only authenticated user's can send request
if !supertokens.IsAuthed(ctx) {
return nil, gqlerror.Errorf("Unauthorized")
}
// TODO: validate params
// Create account
acc, err := r.ent.Account.Create().SetInput(input).Save(ctx)
if err != nil {
log.Error().Err(err).Msg("Error creating account")
if ent.IsConstraintError(err) {
return nil, gqlerror.Errorf("Account exists")
}
return nil, err
}
log.Debug().Msg("Account created")
return acc, nil
}
and this is my IsAuthed function
go
// IsAuthed returns whether the request is being made by an authenticated user
func IsAuthed(ctx context.Context) bool {
return session.GetSessionFromRequestContext(ctx) != nil
}
rp
04/21/2023, 5:27 AMaV
04/21/2023, 5:49 AMr.App.All("/query", supertokens.VerifySession(nil), gqlQueryHandler(r.DB.Ent))
rp
04/21/2023, 5:50 AMrp
04/21/2023, 5:52 AMaV
04/21/2023, 5:55 AMrp
04/21/2023, 6:12 AM