问题描述:ApolloClient:无法将GraphQL查询作为JS变量传递?“不变违规:期望一个解析的GraphQL文档。”
解决方法:
const query = `
your GraphQL query here
`;
gql
)将查询字符串包装起来。这样可以告诉ApolloClient这是一个GraphQL查询。import { gql } from '@apollo/client';
const query = gql`
your GraphQL query here
`;
query
、mutate
等)。import { gql, ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
uri: 'your GraphQL API endpoint',
cache: new InMemoryCache(),
});
const GET_DATA = gql`
your GraphQL query here
`;
client.query({
query: GET_DATA,
})
.then(response => {
// 处理响应
})
.catch(error => {
// 处理错误
});
请注意,在以上代码示例中,你需要将your GraphQL query here
替换为你自己的GraphQL查询。确保查询的语法正确,并且与你的GraphQL API兼容。
希望这些解决方案能帮助到你解决这个问题!