I have a largish dataset where there are many instances where date fields are null (because the date isn't known for some documents).
When I use the GraphiQL interface I can query the records and get the data back, and at the bottom of the returned data I get a an array of errors corresponding to these null date value. I didn't think this would be a problem as in the main returned structure the data looked good. That is to say, where there is a known date I get a valid date value, or if there is a null date I just get an empty string in the json.
However when I then try to access this data in code (nextjs in my case) data is missed out altogether.
I get the data via a fetch like this:
async function fetchAPI(query, { previewData, variables } = {}) {
const prismicAPI = await PrismicClient.getApi()
const res = await fetch(
`${GRAPHQL_API_URL}?query=${query}&variables=${JSON.stringify(variables)}`,
{
headers: {
'Prismic-Ref': previewData?.ref || prismicAPI.masterRef.ref,
'Content-Type': 'application/json',
'Accept-Language': API_LOCALE,
Authorization: `Token ${API_TOKEN}`,
},
}
)
if (res.status !== 200) {
console.log(await res.text())
throw new Error('Failed to fetch API')
}
const json = await res.json()
if (json.errors) {
console.error(json.errors)
throw new Error('Failed to fetch API')
}
return json.data
}
and my query looks like this (the start_date and end_date fields appear to be causing the issue):
export async function getFamilyAndMoreFamilies(slug, previewData) {
const data = await fetchAPI(
`
query FamilyBySlug($slug: String!, $lang: String!) {
family(uid: $slug, lang: $lang) {
name
family_members {
relationship
description
sequence
}
title_holder {
...on Person{
salutation
firstnames
lastname
lifeevents {
event_type
description
start_date
end_date
}
_meta {
uid
}
}
}
predecessors {
sequence
description
}
_meta {
uid
}
}
}
`,
{
previewData,
variables: {
slug,
lang: API_LOCALE,
},
}
)
return data
}
Am I even looking in the right place? Should I be using a different approach, perhaps with predicates?