在阿波罗客户端中设置标题

时间:2019-06-01 02:30:31

标签: react-native http-headers apollo-client

我正在尝试在apollo客户端中发送和授权标头,并且我总是在apollo服务器后端中得到一个空标头

客户代码

var foo = {};

foo.method = function(name, cb){
  this[name] = cb;
};

这是我尝试访问标头值的服务器代码

import React, { Component } from "react";
import { I18nManager } from "react-native";
// import {  } from "apollo-client";
import { ApolloClient, HttpLink, InMemoryCache } from "apollo-boost";
import { ApolloProvider } from "react-apollo";
import { AsyncStorage } from "react-native";

import Navigator from "./Navigator";
import { DATABASE_URL } from "./constants";

const App = () => {
    const client = new ApolloClient({
        link: new HttpLink({ uri: DATABASE_URL }),
        cache: new InMemoryCache(),
        request: async operation => {
            const token = await AsyncStorage.getItem("auth-token");
            operation.setContext({
                headers: {
                    authorization: token
                }
            });
        },
        onError: ({ networkError, graphQLErrors }) => {
            console.log("graphQL Error", graphQLErrors);
            console.log("network Error", networkError);
        }
    });
    I18nManager.forceRTL(true);

    return (
        <ApolloProvider client={client}>
            <Navigator />
        </ApolloProvider>
    );
};

export default App;

当我尝试运行需要身份验证标头的突变时,我总是会收到此错误消息   const context = ({ req }) => { const token = req.headers.authorization || ""; console.log("auth token", token); return token; }; module.exports = new ApolloServer({ typeDefs, resolvers, playground: true, introspection: true, context });

编辑: 这是我要获取令牌的解析器代码之一

GraphQL error: jwt must be provided

0 个答案:

没有答案