Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

[Bug] apollo-link-context "setContext" not sending context #327

Closed
Kisepro opened this issue Dec 11, 2017 · 4 comments
Closed

[Bug] apollo-link-context "setContext" not sending context #327

Kisepro opened this issue Dec 11, 2017 · 4 comments

Comments

@Kisepro
Copy link

Kisepro commented Dec 11, 2017

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'
}))
@imranolas
Copy link
Contributor

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.

@Kisepro
Copy link
Author

Kisepro commented Dec 11, 2017

@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 :/

@evans
Copy link
Contributor

evans commented Feb 15, 2018

Thank you @imranolas for the help!

@evans evans closed this as completed Feb 15, 2018
@oklas
Copy link

oklas commented Apr 20, 2018

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants