ardatan / graphql-mesh

🕸️ GraphQL Federation Framework for any API services such as REST, OpenAPI, Swagger, SOAP, gRPC and more...
https://the-guild.dev/graphql/mesh
MIT License
3.26k stars 332 forks source link

`connectionParams` for Subscriptions are not resolved #5774

Closed roeb closed 11 months ago

roeb commented 1 year ago

Issue workflow progress

Progress of the issue based on the Contributor Workflow

Make sure to fork this template and run yarn generate in the terminal.

Please make sure Mesh package versions under package.json matches yours.


Describe the bug

We want to use the GraphQL mesh to forward a subscription request to an Apollo GraphQL server behind it. The forwarding works and the subscription arrives successfully in Apollo.

However, we are missing the passed connection parameters. Here the interpolation does not seem to work. We could check with an additional resolver that the connection parameters sent by the client arrive successfully in the GraphQL mesh, but are not passed on to the Apollo.

The following arrives at the Apollo:

{
  "Authorization": "{context.connectionParams['authorization']}",
  "Tenant": "{context.connectionParams['tenant']}"
}

The headers for queries and mutations arrive successfully and correctly resolved at Apollo. The problem exists only for the connection parameters of subscriptions.

To Reproduce Steps to reproduce the behavior:

.meshrc.yml

sources:
  - name: internal
    handler:
      graphql:
        endpoint: '${INTERNAL_GRAPHQL_ENDPOINT}'
        source: ../../Apollo/src/schema.graphql
        operationHeaders:
          Authorization: "{context.headers['authorization']}"
          Tenant: "{context.headers['tenant']}"
        connectionParams:
          Authorization: "{context.connectionParams['authorization']}"
          Tenant: "{context.connectionParams['tenant']}"

...

Expected behavior

The GraphQL mesh should correctly resolve the connection parameters passed by the client and forward them to the Apollo server.

The following SHOULD be arrived in Apollo:

{
  "Authorization": "MY_TOKEN",
  "Tenant": "MY_TENANT"
}

Environment:

Additional context

ardatan commented 1 year ago

Could you provide a reproduction on CodeSandbox or StackBlitz? Thanks!

roeb commented 1 year ago

Hi @ardatan, I have added a link to a GitHub repo and a CodeSandbox link in my post above. Unfortunately there is a problem with Yoga GraphiQL. But in every other GraphQL client it works. I hope this helps?

ardatan commented 1 year ago

Hmm YogaGraphiQL uses SSE not WS so there is no connectionParams in the context while connecting via SSE since it is still HTTP

roeb commented 1 year ago

When I mount an AdditionalResolver in GraphQL Mesh that processes subscriptions, I have the connectionParams there. Also, I don't use YogaGraphiQL at all but Banana Cake Pop or Apollo Studio as a client. They all go through WS.

ardatan commented 1 year ago

I see. Currently Mesh's GraphiQL uses SSE only. This should be configurable in Mesh's configration file and GraphiQL needs to have a tab for connection params.

roeb commented 1 year ago

I'm sorry, but I think we misunderstand each other.

I don't use YogaGraphiQL and I don't want to. I have the problem with any client lib or app. Even from a React application. There I go via websockets (graphl-ws) to our GraphQL mesh http://localhost:4000 and it should pass the subscription to the Apollo http://localhost:3500. It does this without any problems and the subscriptions work.

Only the connection params passed by the client arrive at the GraphQL Mesh, but are not passed on to the Apollo.

The values from the connection params (Authorization: "TOKEN") that arrive in the GraphQL mesh are not resolved, but only the path of the connection params arrives as a string in Apollo "(Authorization": "{context.connectionParams['authorization']}").

ardatan commented 1 year ago

Ok I understand. The GraphQL source doesn't receive the connection parameters. Is it possible for you to create a small reproduction on CodeSandbox or StackBlitz?

roeb commented 1 year ago

I have added a React app to my example from above, which provides the correct connection parameters. Unfortunately they are not resolved correctly in GraphQL Mesh (see console log output from Apollo after the client connects).

Unfortunately, I don't get Apollo, GraphQL Mesh and a Vite React app to launch in the CodeSandbox. But Apollo and GraphQL Mesh launch. If you copy the URL with port 4000 and give it locally in a client, you will see the problem too (instructions in the README).

Of course you can also clone the repo and start everything locally including the React app.

CodeSandbox: https://codesandbox.io/p/sandbox/github/roeb/graphql-mesh-apollo-sample/tree/main GitHub Repo: https://github.com/roeb/graphql-mesh-apollo-sample

roeb commented 1 year ago

@ardatan any news?

ThomasMoritz commented 11 months ago

@roeb I have exactly the same problem! Did you found a solution?

ThomasMoritz commented 11 months ago

@roeb After hours of debugging and trying to understand how everything works together I hopefully found the solution. It's in my case a problem with the graphql-ws-executor in the https://github.com/ardatan/graphql-tools repository. . The configuration of the graphql-ws initialization get's overwritten. After the fix in the PR below the connectionParamsgetting resolved.

PR: https://github.com/ardatan/graphql-tools/pull/5666