可能的原因是在 Apollo useQuery 中存在一些问题,可能是缺少设置或解析响应数据时出现问题。以下是可能的解决方案:
import { useQuery, gql } from '@apollo/client';
const GET_USERS = gql`
query ($id: ID!) {
user(id: $id) {
name
email
}
}
`;
function UserComponent(props) {
const { id } = props;
const { loading, error, data } = useQuery(GET_USERS, { variables: { id } });
if (loading) return 'Loading...';
if (error) return `Error! ${error.message}`;
return (
{data.user.name}
{data.user.email}
);
}
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
const client = new ApolloClient({
uri: 'https://graphql.example.com',
headers: {
Authorization: `Bearer ${token}`,
},
cache: new InMemoryCache(),
});
ReactDOM.render(
,
document.getElementById('root')
);
import { ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
uri: 'https://graphql.example.com',
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
users: {
keyArgs: ['id'], // customize keyArgs as needed
merge(existing, incoming) {
// customize merge logic as needed
},
},
},
},
},
}),
});
通过对应用程序和 Apollo useQuery