apollo-client 在 node.js 上工作吗? [英] Does apollo-client work on node.js?
问题描述
我需要一个 graphql 客户端库在 node.js 上运行 进行一些测试和一些数据混搭 - 不是在生产能力.我在其他任何地方都使用 apollo(react-apollo
,apollo 的 graphql-server-express
).我的需求很简单.
I need a graphql client lib to run on node.js for some testing and some data mashup - not in a production capacity. I'm using apollo everywhere else (react-apollo
, apollo's graphql-server-express
). My needs are pretty simple.
apollo-client
是一个可行的选择吗?我找不到在节点上使用它的示例或文档 - 如果您知道任何,请分享.
Is apollo-client
a viable choice? I can find no examples or docs on using it on node - if you're aware of any, please share.
或者也许我应该/可以在节点上使用参考 graphql 客户端?
Or maybe I should/can use the reference graphql client on node?
推荐答案
Apollo Client 在 Node.js 上应该可以正常工作.您只需要安装 cross-fetch 因为它假定 fetch
存在.
Apollo Client should work just fine on Node. You only have to install cross-fetch because it assumes fetch
exists.
这是在 Node.js 上运行的 Apollo Client 的完整 TypeScript 实现.
Here is a complete TypeScript implementation of Apollo Client working on Node.js.
import ApolloClient, { gql } from "apollo-boost";
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));
这篇关于apollo-client 在 node.js 上工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!