apollo-client是否可以在node.js上运行?

Ed *_*aub 24 graphql apollostack

我需要一个graphql客户端lib 来运行node.js进行一些测试和一些数据混搭 - 而不是生产能力.我在其他地方使用阿波罗(react-apollo阿波罗graphql-server-express).我的需求非常简单.

apollo-client一个可行的选择吗?我找不到关于在节点上使用它的示例或文档 - 如果您知道任何,请分享.

或者我应该/可以在节点上使用参考graphql客户端?

And*_*ena 16

Apollo Client应该可以在Node上正常工作。您只需安装交叉提取,因为它假定fetch存在。

这是在Node.js上运行的Apollo Client的完整TypeScript实现。

import ApolloClient from "apollo-boost";
import gql from "graphql-tag";
import { InsertJob } from "./graphql-types";
import 'cross-fetch/polyfill';

const client = new ApolloClient({
  uri: "http://localhost:3000/graphql"
});


client.mutate<InsertJob.AddCompany, InsertJob.Variables>({
  mutation: gql`mutation insertJob($companyName: String!) {
      addCompany(input: { displayName: $companyName } ) {
          id
      }
  }`,
  variables: {
    companyName: "aaa"
  }
})
  .then(result => console.log(result));
Run Code Online (Sandbox Code Playgroud)

  • 谢谢您的帮助。如果您不希望使用全局polyfill,则可以将fetch注入ApolloClient中:`import from'cross-fetch'; const client = new ApolloClient({fetch,uri:...` (3认同)

kas*_*tti 13

较新的 Apollo 版本提供了一种更简单的方法来执行此操作,如Apollo 文档中所述,请查看“独立”部分。基本上可以简单地使用ApolloLink以执行查询或突变。

以下是撰写本文时文档中示例代码的副本,node-fetch用作 config to createHttpLink. 查看文档以获取有关如何使用这些工具的更多详细信息。

import { execute, makePromise } from 'apollo-link';
import { createHttpLink } from 'apollo-link-http';
import gql from 'graphql-tag';
import fetch from 'node-fetch';

const uri = 'http://localhost:4000/graphql';
const link = createHttpLink({ uri, fetch });

const operation = {
  query: gql`query { hello }`,
  variables: {} //optional
  operationName: {} //optional
  context: {} //optional
  extensions: {} //optional
};

// execute returns an Observable so it can be subscribed to
execute(link, operation).subscribe({
  next: data => console.log(`received data: ${JSON.stringify(data, null, 2)}`),
  error: error => console.log(`received error ${error}`),
  complete: () => console.log(`complete`),
})

// For single execution operations, a Promise can be used
makePromise(execute(link, operation))
  .then(data => console.log(`received data ${JSON.stringify(data, null, 2)}`))
  .catch(error => console.log(`received error ${error}`))
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢这个轻量级的“apollo-link”解决方案。我在使用 Typescript 进行节点获取时遇到问题,请参阅 [#513](https://github.com/apollographql/apollo-link/issues/513),因此我使用交叉获取。 (2认同)

Raf*_*ira 8

如果有人正在寻找 JavaScript 版本:

require('dotenv').config();
const gql = require('graphql-tag');
const ApolloClient = require('apollo-boost').ApolloClient;
const fetch = require('cross-fetch/polyfill').fetch;
const createHttpLink = require('apollo-link-http').createHttpLink;
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;
const client = new ApolloClient({
    link: createHttpLink({
        uri: process.env.API,
        fetch: fetch
    }),
    cache: new InMemoryCache()
});

client.mutate({
    mutation: gql`
    mutation popJob {
        popJob {
            id
            type
            param
            status
            progress
            creation_date
            expiration_date
        }
    }
    `,
}).then(job => {
    console.log(job);
})
Run Code Online (Sandbox Code Playgroud)


Mr5*_*5o1 6

您可以使 apollo-client 工作,但这不是此用例的最佳选择。

试试graphql-request

支持脚本或简单应用程序的 Node 和浏览器的最小 GraphQL 客户端

每个 npmjs 的功能:

  • 最简单轻量级的 GraphQL 客户端
  • 基于 Promise 的 API(适用于 async/await)
  • 打字稿支持
  • 同构(适用于 Node/浏览器)

例子:

    import { request, gql } from 'graphql-request'
 
    const query = gql`
      {
        Movie(title: "Inception") {
          releaseDate
          actors {
            name
          }
        }
      }
`
 
request('https://api.graph.cool/simple/v1/movies', query).then((data) => console.log(data))
Run Code Online (Sandbox Code Playgroud)

我与这个包没有任何关系。