[英]customize json response in graphQL
我使用Express-js和express graphQL模塊創建端點和Web服務;
我正在尋找在graphQL中創建自定義響應的方法,我的端點很簡單
從數據庫中選擇書籍,我的回答是
{
"data": {
"books": [
{
"id": "5b5c02beab8dc1182b2e0a03",
"name": "dasta"
},
{
"id": "5b5c02c0ab8dc1182b2e0a04",
"name": "dasta"
}
]
}
}
但需要這樣的東西
{
"result": "success",
"msg" : "list ...",
"data": [
{
"id": "5b5c02beab8dc1182b2e0a03",
"name": "dasta"
},
{
"id": "5b5c02c0ab8dc1182b2e0a04",
"name": "dasta"
}
]
}
這是我的書類型
const BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: {type: GraphQLID},
name: {type: GraphQLString},
genre: {type: GraphQLString},
author_id: {type: GraphQLString},
author: {
type: AuthorType,
resolve(parent, args) {
return Author.findById(parent.author_id);
}
}
})
});
這不是合法的GraphQL響應。 根據規范的7.1節 ,在描述了data
, errors
和extensions
:頂級鍵:
...頂層響應圖不得包含上述三個條目以外的任何條目。
您可以將這些數據放入extensions
; 或使其成為GraphQL API的顯式部分; 或者簡單地讓“成功”隱含在結果的存在和錯誤的缺失中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.