Apollo-link: [Bug] apollo-link-context "setContext" not sending context

Created on 11 Dec 2017  ·  4Comments  ·  Source: apollographql/apollo-link

Intended outcome:
I'm trying to use the "setContext" function to set a token asynchronously.
I'm expecting to find the token set in"setContext" in the header of my request or somewhere else.

( See the code in how to reproduce the issue ).

Actual outcome:
The request never contains my token or any context I'm sending.
Not working in a synchronous way either.

this is the request I get :

{ data: {},\n context: \n { request: { sourceIp: '110.169.68.32', headers: {}, httpMethod: 'post' },\n auth: null,\n sessionCache: {},\n environment: {},\n graphcool: \n { rootToken: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MTI5NzkxNzEsImlhdCI6MTUxMjk3ODg3MSwiY2xpZW50SWQiOiJfIiwicHJvamVjdElkIjoiY2o5dnAwMzloMTl2bjAxNDhlc3UzbnYzMSIsInBlcm1hbmVudEF1dGhUb2tlbklkIjoiY2piMXdrMHR2NGExNDAxNTRxMXBrMnh0diJ9.WcqQchOGD-BwwwM4Nmh0zYoOpPk7YcGk1qzRcutDxmY',\n endpoints: [Object],\n projectId: 'cj9vp039h19vn0148esu3nv31',\n alias: null,\n pat: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MTI5NzkxNzEsImlhdCI6MTUxMjk3ODg3MSwiY2xpZW50SWQiOiJfIiwicHJvamVjdElkIjoiY2o5dnAwMzloMTl2bjAxNDhlc3UzbnYzMSIsInBlcm1hbmVudEF1dGhUb2tlbklkIjoiY2piMXdrMHR2NGExNDAxNTRxMXBrMnh0diJ9.WcqQchOGD-BwwwM4Nmh0zYoOpPk7YcGk1qzRcutDxmY',\n serviceId: 'cj9vp039h19vn0148esu3nv31' } } }"

How to reproduce the issue:

import ApolloClient from "apollo-client";
import { HttpLink } from "apollo-link-http";
import { ApolloLink, concat } from "apollo-link";
import { InMemoryCache } from "apollo-cache-inmemory";
import { setContext } from "apollo-link-context";

const authMiddleware = setContext(request => {
  new Promise((success, fail) => {
    AsyncStorage.getItem(ProjectSettings.graphcoolKey).then(token => {
      success({
        headers: {
          authorization: 'Bearer ${token}'
        }
      });
    });
  });
});

const httpLink = new HttpLink({
  uri: ProjectSettings.simpleGraphCoolEndPoint
});

const client = new ApolloClient({
  link: concat(authMiddleware , httpLink),
  cache: new InMemoryCache()
});

I tried a lot of different combinaison :

success({ token: 'async found token' })
success({ auth: 'async found token' })
success({ headers:  { auth: 'async found token' }})
...

Even the simpliest way doesn't send anything

const middleware = setContext((request, previousContext) => ({
  authorization: '1234'
}))

Most helpful comment

At least one issue in your repro steps is that you're not returning the promise but that doesn't explain why the simplest example isn't working for you.

All 4 comments

At least one issue in your repro steps is that you're not returning the promise but that doesn't explain why the simplest example isn't working for you.

@imranolas Thanks a lot, it fixed my problem :)
I don't know either for the simple example, but it's probably a small mistake from me.

Actually, I copy/pasted the code from https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-context and added by mistake some brackets :/

Thank you @imranolas for the help!

Doc shows:

success({ token: "async found token" })
if (token) return { token };
{  headers: { authorization: "1234"} }

and so on.

But only

{  headers: { authorization: "1234"} }

is worked for me. I mean that header field with object must be. That object will be mapped to request header.

Another examples is confising. Most probably it is pseudo-code or may be supposed that it has special processing. It confuses and does not save time.

Was this page helpful?
0 / 5 - 0 ratings