Skip to content

Commit

Permalink
fix(graphql): do not send references to union type function
Browse files Browse the repository at this point in the history
  • Loading branch information
cngonzalez committed Jan 21, 2025
1 parent 5376783 commit d5450d0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,24 @@ export function extractFromSanitySchema(
return {type: getTypeName(candidates[0].type.name), ...base}
}

const unionDefinition = getUnionDefinition(candidates, def, {grandParent: parent})
return {...unionDefinition, ...base}
const allTypeNames = candidates.map((c) => getTypeName(c.type.name))
const targetTypes = [...new Set(allTypeNames)].sort()
const name = targetTypes.join('Or')

// Register the union type if we haven't seen it before
if (!unionTypes.some((item) => item.name === name)) {
unionTypes.push({
kind: 'Union',
name,
types: targetTypes,
})
}

return {
type: name,
// references: targetTypes,
...base,
}
}

function getArrayDefinition(
Expand Down Expand Up @@ -481,9 +497,7 @@ export function extractFromSanitySchema(
}

try {
if (guardPathName !== 'reference') {
unionRecursionGuards.add(guardPathName)
}
unionRecursionGuards.add(guardPathName)

candidates.forEach((def, i) => {
if (typeNeedsHoisting(def)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1747,17 +1747,16 @@ exports[`GraphQL - Schema extraction > Should be able to extract schema with uni
},
{
"fields": [],
"interfaces": undefined,
"kind": "Union",
"name": "AOrB",
"name": "AOrBOrUnion",
"types": [
"A",
"B",
"Union",
],
},
{
"fields": [],
"interfaces": undefined,
"kind": "Union",
"name": "AOrUnion",
"types": [
Expand Down Expand Up @@ -2556,7 +2555,6 @@ exports[`GraphQL - Schema extraction > Should be able to extract schema with uni
"children": {
"description": undefined,
"isReference": true,
"references": undefined,
"type": "AOrUnion",
},
"description": undefined,
Expand Down Expand Up @@ -2609,8 +2607,9 @@ exports[`GraphQL - Schema extraction > Should be able to extract schema with uni
"references": [
"A",
"B",
"Union",
],
"type": "AOrB",
"type": "AOrBOrUnion",
},
"description": undefined,
"fieldName": "body",
Expand Down
Loading

0 comments on commit d5450d0

Please sign in to comment.