【发布时间】:2017-12-11 06:38:46
【问题描述】:
早上好,伙计们,我目前正在编写基于 REST API 的 GraphQL。假设 JSON 响应是一个对象“用户”,其中包含 id、姓名、电子邮件和“地址”等元素,“地址”是一个由街道、套房、城市和邮政编码组成的对象。我应该如何在这个“地址”字段的架构中声明类型,其中包含多个元素。我已经看过文档,但无法理解我的场景的正确类型。
schema相关的sn-p如下:
export default new GraphQLObjectType({
name: 'User',
description: 'User(s) object in JSONPlaceholder Fake API',
fields: () => ({
id: {
type: GraphQLID
},
name: {
type: GraphQLString
},
username: {
type: GraphQLString
},
email: {
type: GraphQLString
},
address: {
type: (?)
}
})
})
下面是我的目标 JSON API。
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
我想创建单独的对象并创建解析器以仅获取该特定的“地址”对象,但这意味着我要查询两次。我认为可能有一些解决方法可以避免这种情况。
提前致谢!
【问题讨论】:
标签: javascript json rest graphql