Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql-yoga integration example breaks for mutations #480

Closed
RMHonor opened this issue Jun 1, 2023 · 2 comments
Closed

graphql-yoga integration example breaks for mutations #480

RMHonor opened this issue Jun 1, 2023 · 2 comments
Labels
unrelated Not relevant, related or caused by the lib

Comments

@RMHonor
Copy link

RMHonor commented Jun 1, 2023

Overview

When integrating our existing websocket server into graphql-yoga, we used the example recipe for integration, which wraps the request with envelop. This forwards the request object from the initial connection, which is a GET request, and so the usePreventMutationViaGET kicks in and prevents any mutation operations over the websocket.

Expected Behaviour
Mutations should be possible across the websocket.

Actual Behaviour
Mutations fail with an HTTP error 405 (method not allowed).

Debug Information
Example stack:

GraphQLError: Can only perform a mutation operation from a POST request.    
  at createGraphQLError (/Users/richardhonor/uw/crm-graphql/node_modules/graphql-yoga/node_modules/@graphql-tools/utils/cjs/errors.js:9:12)    
  at assertMutationViaGet (/Users/richardhonor/uw/crm-graphql/node_modules/graphql-yoga/cjs/plugins/requestValidation/usePreventMutationViaGET.js:21:46)    
  at /Users/richardhonor/uw/crm-graphql/node_modules/graphql-yoga/cjs/plugins/requestValidation/usePreventMutationViaGET.js:57:17    
  at /Users/richardhonor/uw/crm-graphql/node_modules/@envelop/core/cjs/orchestrator.js:110:17    
  at onSubscribe (/Users/richardhonor/uw/crm-graphql/src/main.ts:106:27)    
  at onMessage (/Users/richardhonor/uw/crm-graphql/node_modules/graphql-ws/lib/server.js:147:124)    
  at WebSocket.<anonymous> (/Users/richardhonor/uw/crm-graphql/src/websocket.ts:149:21)    
  at WebSocket.emit (node:events:513:28)    
  at WebSocket.emit (node:domain:489:12)    
  at Receiver.receiverOnMessage (/Users/richardhonor/uw/crm-graphql/node_modules/ws/lib/websocket.js:1180:20)

Further Information
As a workaround, I modified the request object so all operations are passed through as a POST:

useServer(
  {
    execute: (args: any) => args.rootValue.execute(args),
    subscribe: (args: any) => args.rootValue.subscribe(args),
    onSubscribe: async (ctx, msg) => {
      const { schema, execute, subscribe, contextFactory, parse, validate } =
        yoga.getEnveloped({
          ...ctx,
          req: {
            ...ctx.extra.request,
            method: "POST",
          },
          socket: ctx.extra.socket,
          params: msg.payload,
        });
 
      const args = {
        schema,
        operationName: msg.payload.operationName,
        document: parse(msg.payload.query),
        variableValues: msg.payload.variables,
        contextValue: await contextFactory(),
        rootValue: {
          execute,
          subscribe,
        },
      };
 
      const errors = validate(args.schema, args.document);
      if (errors.length) return errors;
      return args;
    },
  },
  wsServer,
);

Related previous issue: dotansimha/graphql-yoga#1704

@enisdenjo
Copy link
Owner

Hmm, which version of Yoga are you using? Can you create a repro because the example recipe works for me?

Furthermore, there's a graphql-ws example in Yoga with tests, and one of them ensures mutations work.

@enisdenjo enisdenjo added documentation Improvements or additions to documentation and removed documentation Improvements or additions to documentation labels Jun 1, 2023
@RMHonor
Copy link
Author

RMHonor commented Jun 2, 2023

It was very much my mistake:

        yoga.getEnveloped({
          ...ctx,
+         req: ctx.extra.request,
-         request: ctx.extra.request,
          socket: ctx.extra.socket,
          params: msg.payload,
        });

Sorry @enisdenjo

@RMHonor RMHonor closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2023
@enisdenjo enisdenjo added the unrelated Not relevant, related or caused by the lib label Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unrelated Not relevant, related or caused by the lib
Projects
None yet
Development

No branches or pull requests

2 participants