Ошибки operation.setContext с Apollo Boost

Я использую заголовки для аутентификации с помощью Apollo Client. Следующее работало нормально:

const middlewareAuthLink = new ApolloLink((operation, forward) => {
    const token = localStorage.getItem('auth-token');
    const authorizationHeader = token ? `Bearer ${token}` : null;
    operation.setContext({
        headers: {
            authorization: authorizationHeader,
        },
    });
    return forward(operation);
});

Я перехожу на Apollo Boost: https://dev-blog.apollodata.com/zero-config-graphql-state-management-27b1f1b3c2c3

const client = new ApolloClient({
    uri: 'MY-GRAPHCOOL-API',
    fetchOptions: {
        credentials: 'include',
    },
    request: async operation => {
        operation.setContext({
            headers: {
                authorization: 'sadfjadsfsd',
            },
        });
    },
    clientState: {
        defaults: {
            CurrentUserIsLoggedIn: {
                __typename: 'CurrentUserIsLoggedIn',
                value: false,
            },
        },
        resolvers: {
            Mutation: {
                CurrentUserIsLoggedIn: (_, args, { cache }) => {
                    const data = {
                        CurrentUserIsLoggedIn: {
                            __typename: 'CurrentUserIsLoggedIn',
                            value: args.value,
                        },
                    };
                    cache.writeData({ data });
                },
            },
        },
    },
});

Теперь я получаю сообщение об ошибке, и мой токен не добавляется:

[Network error]: TypeError: operation.setContext is not a function

person Evanss    schedule 19.02.2018    source источник
comment
Я получаю ту же ошибку   -  person Matteo Frana    schedule 23.02.2018


Ответы (1)


На GitHub есть открытый запрос на вытягивание, который решает эту проблему: в config.request

Пока он не будет объединен, вы можете просто применить изменения самостоятельно в пакете apollo-boost в папке node_modules и запустить его скрипт сборки с помощью yarn build. Быстрое исправление, которое сработало для меня.

person rppld    schedule 02.03.2018