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)
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)
如果有人正在寻找 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)
您可以使 apollo-client 工作,但这不是此用例的最佳选择。
支持脚本或简单应用程序的 Node 和浏览器的最小 GraphQL 客户端
每个 npmjs 的功能:
例子:
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)
我与这个包没有任何关系。
| 归档时间: |
|
| 查看次数: |
4516 次 |
| 最近记录: |