将服务器端的代码升级了一下:
var GraphQLSchema = require('graphql').GraphQLSchema; var GraphQLObjectType = require('graphql').GraphQLObjectType; var GraphQLString = require('graphql').GraphQLString; var GraphQLList = require('graphql').GraphQLList; var fetch = require('node-fetch'); require("babel-polyfill");
var BASE_URL = 'http://localhost:3000';
var persons = [ { id: "1", first_name: "Jack", last_name: "Zhang" , department: "Depart1", friends: [1] }, { id: "2", first_name: "Tom", last_name: "Wang" , department: "Depart2", friends: [1, 2] } ];
function getPersonByUrl(args, relativeURL) { var person = persons.find(function (item) { if (args.id) { return item.id == args.id; }
if (args.department) { return item.department == args.department; }
});
return person;
//fetch('${BASE_URL}${relativeURL}') // .then(function (res) { return res.json() }) // .then(function (json) { return json.person }) }
function getFriendByPersonId(friendID) {
var person = persons.find(function (item) { return item.id == friendID; });
return person;
//fetch('${BASE_URL}${relativeURL}') // .then(function (res) { return res.json() }) // .then(function (json) { return json.person }) }
var PersonType = new GraphQLObjectType( { name: 'Person', description: '...', fields: ()=>({ id: { type: GraphQLString, resolve : function (person) { return person.first_name; } }, firstName: { type: GraphQLString, resolve : function (person) { return person.first_name; } }, lastName: { type: GraphQLString, resolve : function (person) { return person.last_name; } }, department: { type: GraphQLString, resolve : function (person) { return person.department; } }, //email: { type: GraphQLString }, //userName: { type: GraphQLString }, //id: { type: GraphQLString }, friends: { type: new GraphQLList(PersonType), resolve: function (person) { return person.friends.map(getFriendByPersonId); } } }) });
var QueryType = new GraphQLObjectType({ name: 'Query', desription: '...', fields: { person: { type: PersonType, args: { id: { type: GraphQLString }, department: { type: GraphQLString } }, resolve: function (obj, args, context, info) { return getPersonByUrl(args, null); } } }
});
var GraphQLSchemaObj = new GraphQLSchema({ query: QueryType });
module.exports = GraphQLSchemaObj; |
主要有以下几处更改:
- 将模拟的数据源persons单独提出来成为一个全局变量.
- 数据源中增加了department等几个属性.
- getPersonByUrl函数支持id和department参数.
- 增加了getFriendByPersonId函数用来解析friends属性.
- PersonType的fields属性使用了()=>来解决friends属性中使用本类型时本类型尚未初始化的问题.
下面是客户端的测试代码:
app.js
console.log('Hello world');
////Arguments //var Test1 = require('./Test1'); //Test1.Execute();
////Alias //var Test2 = require('./Test2'); //Test2.Execute();
////Alias with sub-selection //var Test3 = require('./Test3'); //Test3.Execute();
////Fragments //var Test4 = require('./Test4'); //Test4.Execute();
//Variblies //var Test5 = require('./Test5'); //Test5.Execute();
//Directives //var Test6 = require('./Test6'); //Test6.Execute(); |
具体的测试类:
Alias:
//Test2: Aliases
var gRequest = require('graphql-request').request;
exports.Execute = function () {
const query = '{' + ' Depart1Person: person(department: "Depart1") {' + ' firstName,' + ' lastName,' + ' department' + ' }' + ' Depart2Person: person(department: "Depart2") {' + ' firstName,' + ' lastName,' + ' department' + ' }' + '}';
gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) });
}; |
运行结果如下:
{ Depart1Person: { firstName: 'Jack', lastName: 'Zhang', department: 'Depart1' }, Depart2Person: { firstName: 'Tom', lastName: 'Wang', department: 'Depart2' } } |
Aliases -- sub selection
//Test3: Aliases -- sub selection
var gRequest = require('graphql-request').request; var util = require('util');
exports.Execute = function () {
const query = '{' + ' Depart1Person: person(department: "Depart1") {' + ' firstName,' + ' lastName,' + ' department,' + ' friends' + ' {' + ' firstName,' + ' lastName' + ' }' + ' }' + ' Depart2Person: person(department: "Depart2") {' + ' firstName,' + ' lastName,' + ' department,' + ' friends' + ' {' + ' firstName,' + ' lastName' + ' }' + ' }' + '}';
gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(util.inspect(data, { showHidden: false, depth: null })) });
}; |
运行结果如下:
{ Depart1Person: { firstName: 'Jack', lastName: 'Zhang', department: 'Depart1', friends: [ { firstName: 'Jack', lastName: 'Zhang' } ] }, Depart2Person: { firstName: 'Tom', lastName: 'Wang', department: 'Depart2', friends: [ { firstName: 'Jack', lastName: 'Zhang' }, { firstName: 'Tom', lastName: 'Wang' } ] } } |
Fragements:
//Test4: Fragements
var gRequest = require('graphql-request').request; var util = require('util');
exports.Execute = function () {
var query = '{' + ' Depart1Person: person(department: "Depart1") {' + ' ...personFields' + ' }' + ' Depart2Person: person(department: "Depart2") {' + ' ...personFields' + ' }' + '}' + '' + 'fragment personFields on Person {' + ' firstName,' + ' lastName,' + ' department,' + ' friends{' + ' firstName,' + ' lastName' + ' }' + '}';
//gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) }); gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(util.inspect(data, { showHidden: false, depth: null })) });
}; |
运行结果如下:
{ Depart1Person: { firstName: 'Jack', lastName: 'Zhang', department: 'Depart1', friends: [ { firstName: 'Jack', lastName: 'Zhang' } ] }, Depart2Person: { firstName: 'Tom', lastName: 'Wang', department: 'Depart2', friends: [ { firstName: 'Jack', lastName: 'Zhang' }, { firstName: 'Tom', lastName: 'Wang' } ] } } |
Varibles:
//Test5: Variables
var gRequest = require('graphql-request').request; var util = require('util');
exports.Execute = function () {
var query = 'query PersonWithDept($dept: String) {' + ' person(department: $dept) {' + ' ...personFields' + ' }' + '}' + '' + 'fragment personFields on Person {' + ' firstName,' + ' lastName,' + ' department,' + ' friends{' + ' firstName,' + ' lastName' + ' }' + '}';
var varibles = { "dept": "Depart1" };
//gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) }); gRequest('http://localhost:1337/graphql/graphql', query, varibles).then(function (data) { console.log(util.inspect(data, { showHidden: false, depth: null })) });
}; |
运行结果如下:
{ person: { firstName: 'Jack', lastName: 'Zhang', department: 'Depart1', friends: [ { firstName: 'Jack', lastName: 'Zhang' } ] } } |
Directives:
//Test6: Directives
var gRequest = require('graphql-request').request; var util = require('util');
exports.Execute = function () {
var query = 'query PersonWithDept($dept: String, $withFriends: Boolean!) {' + ' person(department: $dept) {' + ' ...personFields' + ' }' + '}' + '' + 'fragment personFields on Person {' + ' firstName,' + ' lastName,' + ' department,' + ' friends @include(if: $withFriends){' + ' firstName,' + ' lastName' + ' }' + '}' ;
var varibles1 = { "dept": "Depart1", "withFriends": true };
var varibles2 = { "dept": "Depart1", "withFriends": false };
//gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) }); gRequest('http://localhost:1337/graphql/graphql', query, varibles1).then(function (data) { console.log(util.inspect(data, { showHidden: false, depth: null })) });
gRequest('http://localhost:1337/graphql/graphql', query, varibles2).then(function (data) { console.log(util.inspect(data, { showHidden: false, depth: null })) });
}; |
运行结果如下:
{ person: { firstName: 'Jack', lastName: 'Zhang', department: 'Depart1' } } { person: { firstName: 'Jack', lastName: 'Zhang', department: 'Depart1', friends: [ { firstName: 'Jack', lastName: 'Zhang' } ] } } |
注意客户端代码中使用了,是为了打印出json的子对象,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2016-07-13 C++ RegCreateKeyEx成功了,但是注册表并没有这一项
2016-07-13 导出DLLRegisterServer接口遇到的问题