
前一篇文章介绍了怎么使用 ApolloServer 搭建 GraphQL server,今天看看怎么使用 ApolloClient 来执行查询。
npm install @apollo/client graphql react# 导入依赖库
const { ApolloClient, InMemoryCache, gql } = require('@apollo/client');
# 创建ApolloClient实例
const client = new ApolloClient({
    uri: 'http://localhost:4000/',
    cache: new InMemoryCache(),
});创建实例的时候使用 uri 和 cache 参数:
# 执行查询
client.query({
    query: gql`
        query {
            hello
        }
    `,
}).then((result) => {
    console.log(result);
});const { ApolloClient, InMemoryCache, gql } = require('@apollo/client');
const client = new ApolloClient({
    uri: 'http://localhost:4000/',
    cache: new InMemoryCache(),
});
client.query({
    query: gql`
        query {
            hello
        }
    `,
}).then((result) => {
    console.log(result);
});将上面代码保存到 test.js 文件中,然后运行
node test.js
{ data: { hello: 'Hello World!' }, loading: false, networkStatus: 7 }