【发布时间】:2017-01-07 08:32:53
【问题描述】:
我正在尝试返回一个带有列表中一些嵌套属性对象的 graphQL 查询。一切似乎都检查过了,但得到的是空值。
预期的数据结构响应(有关结构here 的更多详细信息):
//Return a GraphQLList type of all objects :
[
{
"subscription" : {
},
"customer" : {
},
},
]
查询:{ subscriptions { subscription { id customer_id } } }
GraphQL 查询对象:
var Query = new graphQL.GraphQLObjectType({
name: 'Query',
description: 'Root of the Schema',
fields: {
subscriptions: {
type: new graphQL.GraphQLList(SubscriptionsList),
args : {
customer_name: { type: graphQL.GraphQLString },
customer_id: { type: graphQL.GraphQLString }
},
resolve : function (source, args) {
return getSubscriptions().then(function (res) {
// Shows that data is returned
console.log('Data: ', res.list);
return res.list;
}).catch(function (err) {
return err;
});
}
}
}
});
期望返回类型 GraphQLList of SubscriptionsList:
var SubscriptionsList = new graphQL.GraphQLObjectType({
name : 'SubscriptionObj',
description : 'stuff',
fields : function () {
return {
subscription : {
type : Subscription,
resolve : function (subscription) {
return subscription;
}
}
}
}
});
应该解析嵌套字段属性:
var Subscription = new graphQL.GraphQLObjectType({
name : 'Subscription',
description : 'stuff',
fields : function () {
return {
id: {type: graphQL.GraphQLString},
customer_id: {type: graphQL.GraphQLString}
}
}
});
Console.log(res.list) 输出(来自查询):我肯定得到了我期望的数据和结构:
[ { subscription:
{ id: 'cbdemo_lloyd-sub2',
customer_id: 'cbdemo_lloyd',
plan_id: 'cbdemo_nuts',
addons: [Object],
due_invoices_count: 0,
shipping_address: [Object] },
customer: {
...
}
},
{...}
]
GraphQL 查询输出:
{
"data": {
"subscriptions": [
{
"subscription": {
"id": null,
"customer_id": null
}
},
{
"subscription": {
"id": null,
"customer_id": null
}
},
EDIT 不是很必要,但这里是返回承诺的getSubscriptions() API 调用:
function getSubscriptions() {
return new Promise(function (resolve, reject) {
chargebee.subscription.list({
limit : 5,
"plan_id[is_not]" : "basic",
"status[is]" : "active",
"sort_by[asc]" : "created_at"
}).request(function(error,result){
if (error) return reject(error);
return resolve(result);
});
});
}
【问题讨论】:
标签: javascript json ajax graphql