使用ApolloClient,在向标头添加基本凭证时遇到麻烦

时间:2019-11-24 21:02:58

标签: apollo react-apollo

我有工作代码来创建ApolloClient,该ApolloClient基于URL上的代码:https://www.apollographql.com/docs/tutorial/client/#create-an-apollo-client。是:

const httpLink = new HttpLink({
    uri: graphqlBase,
    credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
    fetch
});

return new ApolloClient({
    link: httpLink,
    cache: new InMemoryCache().restore(initialState)
});

添加我认为应该是有效的中间件时,将代码link: httpLink替换为:middlewareLink.concat(httpLink)middlewarelink,定义如下:

const middlewareLink = new ApolloLink((operation, forward) => {
    operation.setContext({
        headers: {
            authorization: 'xxx' || null
        }
    });
    return forward(operation);
});

我的工作查询现在因此错误而出错

"GraphQL.ExecutionError: A query is required. ---> GraphQL.ExecutionError: A query is required.\r\n   at GraphQL.DocumentExecuter.ValidateOptions

我正在查询

import { ApolloProvider } from '@apollo/react-hooks';

<ApolloProvider client={client}>
            <PageComponent {...pageProps} />
</ApolloProvider>

似乎我的查询本身已被删除,但我不明白为什么。

0 个答案:

没有答案