graphql-go-tools icon indicating copy to clipboard operation
graphql-go-tools copied to clipboard

Pass WS initial payload from gateway to federated services

Open chedom opened this issue 4 years ago • 4 comments

Hi, for my use case it's required to pass user related information (token/id/roles) from gateway to federated services. There are two possible options:

  1. Use payload in connection_init message (receiver must handle initPayload from connection_init message) (I prefer this option)
  2. Use Header field from FetchConfiguration (receiver must handle http header (before upgrading))

Both options aren't ready to use, they require some contribution.

@jensneuse which option would you prefer ?

chedom avatar Oct 11 '21 14:10 chedom

Can you expand a bit on option 1)? How will it work? I think I'd like to have a generic solution that allows users (like you) to find their own ways of using the library. So I'm preferring to keep things generic.

jensneuse avatar Oct 12 '21 09:10 jensneuse

@jensneuse I checked all my cases, and it turned out that in all cases ws initial payload is almost exactly the same as Header for the same upstream. In my fork, I just added sending Header as ws initial Payload. If necessary, I can create a PR, otherwise theissue can be closed.

chedom avatar Nov 01 '21 10:11 chedom

I'm not sure I understand 100%. How would Header as ws initial payload look like?

jensneuse avatar Nov 01 '21 16:11 jensneuse

I'm not sure I understand 100%. How would Header as ws initial payload look like?

According to gqlgen interface, designed to handle WS auth https://gqlgen.com/recipes/authentication/#websockets, it expects type InitPayload map[string]interface{} (https://github.com/99designs/gqlgen/blob/master/graphql/handler/transport/websocket_init.go) and provides safe getters for strings (defaulting to "", same as http.Header, but with after type-checking for string.

I've checked how Playground works also, it sends {"type":"connection_init","payload":{"Authorization":"test"}} if you define some headers at the bottom.

Apollo Link has connectionParams option and also sends it as payload. (https://www.apollographql.com/docs/react/data/subscriptions/#5-authenticate-over-websocket-optional)

import { WebSocketLink } from '@apollo/client/link/ws';

const wsLink = new WebSocketLink({
  uri: 'ws://localhost:4000/subscriptions',
  options: {
    reconnect: true,
    connectionParams: {
      authToken: user.authToken,
    },
  },
});

v0id3r avatar Feb 17 '22 10:02 v0id3r

closed with https://github.com/wundergraph/graphql-go-tools/pull/425

devsergiy avatar Oct 19 '22 07:10 devsergiy