Javascript apollo客户端是否在node.js上工作?
我需要一个graphql客户端库在node.js上运行,以进行一些测试和数据mashup,而不是在生产能力中运行。我在其他任何地方都在使用apollo(Javascript apollo客户端是否在node.js上工作?,javascript,node.js,graphql,apollostack,Javascript,Node.js,Graphql,Apollostack,我需要一个graphql客户端库在node.js上运行,以进行一些测试和数据mashup,而不是在生产能力中运行。我在其他任何地方都在使用apollo(react apollo,apollo的graphql server express)。我的需求很简单 阿波罗客户机是可行的选择吗?我找不到在node上使用它的示例或文档-如果您知道任何示例或文档,请共享 或者我应该/可以在节点上使用参考graphql客户端?这里是简单的节点js实现 “graphiql”客户端对于开发活动来说已经足够好了 1.
react apollo
,apollo的graphql server express
)。我的需求很简单
阿波罗客户机是可行的选择吗?我找不到在node上使用它的示例或文档-如果您知道任何示例或文档,请共享
或者我应该/可以在节点上使用参考graphql客户端?这里是简单的节点js实现 “graphiql”客户端对于开发活动来说已经足够好了
1. run npm install
2. start server with "node server.js"
3. hit "http://localhost:8080/graphiql" for graphiql client
server.js
var graphql = require ('graphql').graphql
var express = require('express')
var graphQLHTTP = require('express-graphql')
var Schema = require('./schema')
// This is just an internal test
var query = 'query{starwar{name, gender,gender}}'
graphql(Schema, query).then( function(result) {
console.log(JSON.stringify(result,null," "));
});
var app = express()
.use('/', graphQLHTTP({ schema: Schema, pretty: true, graphiql: true }))
.listen(8080, function (err) {
console.log('GraphQL Server is now running on localhost:8080');
});
schema.js
//schema.js
var graphql = require ('graphql');
var http = require('http');
var StarWar = [
{
"name": "default",
"gender": "default",
"mass": "default"
}
];
var TodoType = new graphql.GraphQLObjectType({
name: 'starwar',
fields: function () {
return {
name: {
type: graphql.GraphQLString
},
gender: {
type: graphql.GraphQLString
},
mass: {
type: graphql.GraphQLString
}
}
}
});
var QueryType = new graphql.GraphQLObjectType({
name: 'Query',
fields: function () {
return {
starwar: {
type: new graphql.GraphQLList(TodoType),
resolve: function () {
return new Promise(function (resolve, reject) {
var request = http.get({
hostname: 'swapi.co',
path: '/api/people/1/',
method: 'GET'
}, function(res){
res.setEncoding('utf8');
res.on('data', function(response){
StarWar = [JSON.parse(response)];
resolve(StarWar)
console.log('On response success:' , StarWar);
});
});
request.on('error', function(response){
console.log('On error' , response.message);
});
request.end();
});
}
}
}
}
});
module.exports = new graphql.GraphQLSchema({
query: QueryType
});
针对@YakirNa的评论: 我无法描述我描述的其他需求,但我已经做了大量的测试。我最终完成了我所有的测试过程 大多数测试最终都是解析器测试,我通过一个jig进行测试,该jig使用测试查询调用graphql库的
graphql
函数,然后验证响应
我还有一个(几乎)端到端的测试层,它在express的http处理级别上工作。它创建一个假的HTTP请求,并在过程中验证响应。这都在服务器进程内;没有什么能越过电线。我很少使用它,主要用于测试JWT身份验证和其他独立于graphql请求主体的请求级别行为。Apollo客户端应该可以在节点上正常工作。您只需安装,因为它假定存在
fetch
下面是Apollo客户端在Node.js上的完整TypeScript实现
从“apollo boost”导入apollo客户端,{gql};
从“/graphql类型”导入{InsertJob};
导入“交叉提取/多边形填充”;
const客户端=新客户端({
uri:“http://localhost:3000/graphql"
});
变异({
突变:gql`mutation insertJob($companyName:String!){
addCompany(输入:{displayName:$companyName}){
身份证件
}
}`,
变量:{
公司名称:“aaa”
}
})
.then(result=>console.log(result));
如果有人正在寻找JavaScript版本:
require('dotenv').config();
const gql=require('graphql-tag');
const apollo客户端=需要('apollo-boost')。apollo客户端;
const fetch=require('cross-fetch/polyfill')。fetch;
const createHttpLink=require('apollo-link-http')。createHttpLink;
const InMemoryCache=require('apollo-cache-inmemory')。InMemoryCache;
const客户端=新客户端({
链接:createHttpLink({
uri:process.env.API,
取回
}),
缓存:新的InMemoryCache()
});
变异({
突变:gql`
突变popJob{
波普乔{
身份证件
类型
param
地位
进步
创建日期
有效期
}
}
`,
}).然后(作业=>{
控制台日志(作业);
})
我遇到了你同样的问题,因为我想创建一个中间件服务来准备从graphQL到最终前端应用程序的数据,
拥有:
- 优化的数据表示(和标准输出数据接口)
- 更快的响应时间
let response = await this.apolloClient.query({ query: gql`${query}` });
正如你可能已经注意到的:
- 我需要在Httplink上注入fetch
- 我必须设置授权头才能访问外部提供程序graphQL端点
- 我使用了内省FragmentMatcher,以便在查询中使用片段,以及构建模式类型(“fragmentTypes.json”和init脚本)
还期待着对该包装器的评论和改进点。较新的Apollo版本提供了一种更简单的方法来执行此操作,如“独立”部分所述。基本上,人们可以简单地使用ApolloLink来执行查询或变异 下面是撰写本文时文档中示例代码的副本,其中
节点获取
作为配置使用到createHttpLink
。有关如何使用这些工具的更多详细信息,请查看文档
从“阿波罗链接”导入{execute,makePromise};
从'apollo link http'导入{createHttpLink};
从“graphql标签”导入gql;
从“节点获取”导入获取;
常量uri=http://localhost:4000/graphql';
const link=createHttpLink({uri,fetch});
常量运算={
query:gql`query{hello}`,
变量:{}//可选
操作名称:{}//可选
上下文:{}//可选
扩展:{}//可选
};
//execute返回一个可观察对象,以便订阅它
执行(链接、操作)。订阅({
下一步:data=>console.log(`received data:${JSON.stringify(data,null,2)}`),
错误:error=>console.log(`received error${error}`),
complete:()=>console.log(`complete`),
})
//对于单次执行操作,可以使用承诺
承诺(执行(链接、操作))
.then(data=>console.log(`received data${JSON.stringify(data,null,2)}`)
.catch(error=>console.log(`received error${error}`)
您可以让apollo客户端工作,但它不是此用例的最佳选项
试试看
支持脚本或简单应用程序的节点和浏览器的最小GraphQL客户端
每个NPMJ的功能:
- 最简单、最轻量级的GraphQL客户端
- 基于承诺的API(与异步/等待一起使用)
- 类型脚本支持
- 同构(适用于节点/浏览器)
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))
我
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))