From 4c286d7ab5ae5d78bf294f6a449429716eb010a5 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:09:54 +1100 Subject: [PATCH] Fix dataloaders not loading in js plugins (#3014) * Include dataloaders in js plugin gql calls * Handle gql errors correctly in js plugins --- internal/api/server.go | 4 +++- pkg/plugin/js/gql.go | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/api/server.go b/internal/api/server.go index 93189afcc..5f7c96c85 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -122,7 +122,9 @@ func Start() error { // register GQL handler with plugin cache // chain the visited plugin handler - manager.GetInstance().PluginCache.RegisterGQLHandler(visitedPluginHandler(http.HandlerFunc(gqlHandlerFunc))) + // also requires the dataloader middleware + gqlHandler := visitedPluginHandler(dataloaders.Middleware(http.HandlerFunc(gqlHandlerFunc))) + manager.GetInstance().PluginCache.RegisterGQLHandler(gqlHandler) r.HandleFunc("/graphql", gqlHandlerFunc) r.HandleFunc("/playground", gqlPlayground.Handler("GraphQL playground", "/graphql")) diff --git a/pkg/plugin/js/gql.go b/pkg/plugin/js/gql.go index 9c8461177..7a240fbb5 100644 --- a/pkg/plugin/js/gql.go +++ b/pkg/plugin/js/gql.go @@ -89,10 +89,11 @@ func gqlRequestFunc(ctx context.Context, vm *otto.Otto, cookie *http.Cookie, gql throw(vm, fmt.Sprintf("could not unmarshal object %s: %s", output, err.Error())) } - retErr, hasErr := obj["error"] + retErr, hasErr := obj["errors"] if hasErr { - throw(vm, fmt.Sprintf("graphql error: %v", retErr)) + errOut, _ := json.Marshal(retErr) + throw(vm, fmt.Sprintf("graphql error: %s", string(errOut))) } v, err := vm.ToValue(obj["data"])