Open struct78 opened 2 years ago
Managed to fix it by patching a couple of functions in @sanity/core/lib/actions/graphql/extractFromSanitySchema.js
Just needs null checks on the type.
function isBaseType(type) { return type != null && type.name !== null && type.name !== type.jsonType && allowedJsonTypes.includes(type.jsonType) && !skipTypes.includes(type.name) && !isReference(type) && !isCrossDatasetReference(type); }
and
const flattened = converted.reduce((acc, candidate) => { const union = unionTypes.find(item => item.name === candidate.type); return union ? acc.concat(union.types.map(type => ({ type, isReference: candidate.isReference }))) : acc.concat(candidate); }, []).filter(def => def != null && def.type != null);
Managed to fix it by patching a couple of functions in @sanity/core/lib/actions/graphql/extractFromSanitySchema.js
Just needs null checks on the type.
function isBaseType(type) { return type != null && type.name !== null && type.name !== type.jsonType && allowedJsonTypes.includes(type.jsonType) && !skipTypes.includes(type.name) && !isReference(type) && !isCrossDatasetReference(type); }
and
const flattened = converted.reduce((acc, candidate) => { const union = unionTypes.find(item => item.name === candidate.type); return union ? acc.concat(union.types.map(type => ({ type, isReference: candidate.isReference }))) : acc.concat(candidate); }, []).filter(def => def != null && def.type != null);
Do you think making a PR for this is worth it? I see myself running into the same issue sometime soon 😬
Describe the bug
I am attempting to update one of our flexible content components to support inserting inline documents, as well as document references so authors can create content components once, and use them on multiple pages/in multiple contexts.
To Reproduce
For simplicity's sake, I've not included all of the different document types we are using, but save to say they all follow the same kind of structure:
This is how the file used to look - this deploys perfectly fine!
When I update this to include references to those types:
I get the following error message:
Error: All members of union type "SectionAccordionOrSectionBannerOrSectionHeroOrSectionImageOrSectionRichtextOrSectionTitleOrSectionVideoOr" must be strings
The trailing or at the end of the type grabbed my attention.
After dumping out the schema that is being posted to Sanity's API, I noticed that there are a number of references (i.e. in fields) to the union type
SectionAccordionOrSectionBannerOrSectionHeroOrSectionImageOrSectionRichtextOrSectionTitleOrSectionVideo
in the schema, which is correct, however the union type created under the "types" node is calledSectionAccordionOrSectionBannerOrSectionHeroOrSectionImageOrSectionRichtextOrSectionTitleOrSectionVideoOr
This is what the type looks like in the schema:
It seems like it's ignoring the references altogether? If I switch the order and put the references first:
The schema isn't even generated, and I get the following error:
Is there something I'm missing? The setup works fine in Sanity Studio, and I can add documents and document references, and it works on the front end preview locally.
References:
Mixing documents and references:
Which versions of Sanity are you using?
What operating system are you using?
MacOS 11.6.4
Which versions of Node.js / npm are you running?