创建具有不同嵌套键值的架构



我真的不知道如何命名它,但我在获取非null值时遇到了问题。。。我希望有人能帮我,告诉我我做错了什么。。。

我从中提取的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不支持使用动态键返回对象,因此如果不使用自定义标量,就无法在模式中表示相同的数据结构。不过,使用自定义标量的问题是,您将失去GraphQL提供的数据类型验证。您最好将API返回的数据转换为可以在模式中表示的格式。

type CountryData {
timelineItems: [TimelineItemsByDate!]!
}
type TimelineItemsByDate {
date: String!
newDailyCases: Int!
newDailyDeaths: Int!
totalCases: Int!
totalRecoveries: Int!
totalDeaths: Int!
}

请注意,我已经在上面的示例中转换了类型和字段名称,以反映命名约定。此外,如果API出于某种原因将某些数据作为数组返回,但它只返回数组中的单个项,我会将其转换为对象,而不是将其作为List保留在架构中。

最新更新