创建具有不同嵌套键值的架构
Creating a schema with varying nested key values
我什至不确定如何给这个标题,但我在获取 non-null 值时遇到问题...我希望有人能帮助我并告诉我我的想法我做错了...
我从 returns 中提取的 api 以下格式...
{
"countrytimelinedata": [
{
"info": {
"ourid": 167,
"title": "USA",
"code": "US",
"source": "https://thevirustracker.com/usa-coronavirus-information-us"
}
}
],
"timelineitems": [
{
"1/22/20": {
"new_daily_cases": 1,
"new_daily_deaths": 0,
"total_cases": 1,
"total_recoveries": 0,
"total_deaths": 0
},
"1/23/20": {
"new_daily_cases": 0,
"new_daily_deaths": 0,
"total_cases": 1,
"total_recoveries": 0,
"total_deaths": 0
}
}
]
}
我的问题是我无法使用模式中的内容提取 timelineitems 数组中的任何内容
我的架构如下
gql`
extend type Query {
getCountryData: getCountryData
}
type getCountryData {
countrytimelinedata: [countrytimelinedata]
timelineitems: [timelineitems]
}
type countrytimelinedata {
info: Info
}
type Info {
ourid: String!
title: String!
code: String!
source: String!
}
type timelineitems {
timelineitem: [timelineitem]
}
type timelineitem {
new_daily_cases: Int!
new_daily_deaths: Int!
total_cases: Int!
total_recoveries: Int!
total_deaths: Int!
}
`;
我希望这是问这个问题的正确地方,如果我不理解一些基本的东西,我很抱歉。
我应该使用更好的东西吗?
提前致谢
GraphQL 不支持返回带有动态键的对象,因此无法在您的模式中表示相同的数据结构 without using a custom scalar。但是,使用自定义标量的问题是您失去了 GraphQL 提供的数据类型验证。您最好将 API 返回的数据转换为 可以 在您的架构中表达的格式。
type CountryData {
timelineItems: [TimelineItemsByDate!]!
}
type TimelineItemsByDate {
date: String!
newDailyCases: Int!
newDailyDeaths: Int!
totalCases: Int!
totalRecoveries: Int!
totalDeaths: Int!
}
请注意,我已经转换了上面示例中的类型和字段名称以反映命名约定。此外,如果 API 由于某种原因 returns 一些数据作为数组但它只 returns 数组中的一个项目,我只会将其转换为对象而不是保留它作为架构中的列表。
我什至不确定如何给这个标题,但我在获取 non-null 值时遇到问题...我希望有人能帮助我并告诉我我的想法我做错了...
我从 returns 中提取的 api 以下格式...
{
"countrytimelinedata": [
{
"info": {
"ourid": 167,
"title": "USA",
"code": "US",
"source": "https://thevirustracker.com/usa-coronavirus-information-us"
}
}
],
"timelineitems": [
{
"1/22/20": {
"new_daily_cases": 1,
"new_daily_deaths": 0,
"total_cases": 1,
"total_recoveries": 0,
"total_deaths": 0
},
"1/23/20": {
"new_daily_cases": 0,
"new_daily_deaths": 0,
"total_cases": 1,
"total_recoveries": 0,
"total_deaths": 0
}
}
]
}
我的问题是我无法使用模式中的内容提取 timelineitems 数组中的任何内容
我的架构如下
gql`
extend type Query {
getCountryData: getCountryData
}
type getCountryData {
countrytimelinedata: [countrytimelinedata]
timelineitems: [timelineitems]
}
type countrytimelinedata {
info: Info
}
type Info {
ourid: String!
title: String!
code: String!
source: String!
}
type timelineitems {
timelineitem: [timelineitem]
}
type timelineitem {
new_daily_cases: Int!
new_daily_deaths: Int!
total_cases: Int!
total_recoveries: Int!
total_deaths: Int!
}
`;
我希望这是问这个问题的正确地方,如果我不理解一些基本的东西,我很抱歉。
我应该使用更好的东西吗?
提前致谢
GraphQL 不支持返回带有动态键的对象,因此无法在您的模式中表示相同的数据结构 without using a custom scalar。但是,使用自定义标量的问题是您失去了 GraphQL 提供的数据类型验证。您最好将 API 返回的数据转换为 可以 在您的架构中表达的格式。
type CountryData {
timelineItems: [TimelineItemsByDate!]!
}
type TimelineItemsByDate {
date: String!
newDailyCases: Int!
newDailyDeaths: Int!
totalCases: Int!
totalRecoveries: Int!
totalDeaths: Int!
}
请注意,我已经转换了上面示例中的类型和字段名称以反映命名约定。此外,如果 API 由于某种原因 returns 一些数据作为数组但它只 returns 数组中的一个项目,我只会将其转换为对象而不是保留它作为架构中的列表。