vic / apollo-phoenix-websocket

An Apollo networkInterface for executing GraphQL queries via Phoenix Channels
Apache License 2.0
91 stars 9 forks source link

can't set context #22

Closed G3z closed 7 years ago

G3z commented 7 years ago

i tried adding a context as showed in the readme

applyMiddleware({ request, options }, next) {
    // get the authentication token from local storage if it exists
    const store = require('libs/store').default;
    const token = store.getState().credentials && store.getState().credentials.token;
    request.context = token ? { guardian_token: token } : null;

    next();
},

but on phoenix this is what i get %{"vsn" => "1.0.0"}

def connect(params, socket) do
    IO.inspect params
    # ...
end

am I doing something wrong ?

richeterre commented 7 years ago

I have exactly the same problem. Couldn't find the answer anywhere, maybe @vic can help?

richeterre commented 7 years ago

Got the answer on Slack today! You can pass the token to your connect params like so:

networkInterface.use([{
  applyMiddleware({ request, options }, next) {
    const token = 'my-secret-token'
    options.params = {
      ...options.params,
      guardian_token: token,
    }

    next()
  },
}])
G3z commented 7 years ago

thanks @richeterre I tried and it's working. @vic should I edit the readme with this example ?

joshuataylor commented 7 years ago

Now that absinthe_phoenix supports context, the above snippet changing networkInterface is perfect. Maybe replace the auth bit in the readme with that?

vic commented 7 years ago

@joshuataylor , @G3z yes please if you are using an updated version of absinthe_phoenix, please send a PR updating the README with a proper example.

Thank you.

G3z commented 7 years ago

done https://github.com/vic/apollo-phoenix-websocket/pull/28