0

I'm not really sure how to even title this but I'm having an issue getting a non-null value back... I'm hoping someone can help me out and tell me what I'm doing wrong...

The api I'm pulling from returns the following format...

{
    "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
            }
         }
     ]
}

My issue is that I cannot pull anything within the timelineitems array with what I have in my schema

My schema is the following

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!
  }
`;

I hope this is the right place to ask this, and I'm sorry if I am not understanding something basic.

Is there something better I should be using?

Thank you in advance

Chris King
  • 37
  • 5

1 Answers1

1

GraphQL doesn't support returning objects with dynamic keys, so there's no way to represent that same data structure in your schema without using a custom scalar. The issue with using a custom scalar, though, is that you lose the data type validation that GraphQL provides. You're better off just transforming the data returned by the API into a format that can be expressed in your schema.

type CountryData {
  timelineItems: [TimelineItemsByDate!]!
}

type TimelineItemsByDate {
  date: String!
  newDailyCases: Int!
  newDailyDeaths: Int!
  totalCases: Int!
  totalRecoveries: Int!
  totalDeaths: Int!
}

Note that I've transformed the type and field names in the example above to reflect naming conventions. Also, if the API for some reason returns some data as an array but it only ever returns a single item in the array, I would just convert that to an object rather than keeping it as a List in your schema.

Daniel Rearden
  • 58,313
  • 8
  • 105
  • 113