Skip to content

Commit

Permalink
fix: fixes schema extraction with nested union refs
Browse files Browse the repository at this point in the history
This was seen in a schema produced by automated tooling. The recursion
guard was too strict in this case as the two occurrences of a
reference ending up having the same guard path name and extraction
would therefore abort before looking inside the second one, instead
returning `{}` which would fail when attempting to inspect the name
field a little lower down.
  • Loading branch information
rneatherway committed Dec 19, 2024
1 parent cf7afaf commit 3005a89
Show file tree
Hide file tree
Showing 7 changed files with 10,468 additions and 13,145 deletions.
4 changes: 2 additions & 2 deletions dev/test-studio/sanity.cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {type UserConfig} from 'vite'

export default defineCliConfig({
api: {
projectId: 'ppsg7ml5',
dataset: 'test',
projectId: '2378iql5',
dataset: 'production',
},

// Can be overriden by:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ export function extractFromSanitySchema(
}

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

candidates.forEach((def, i) => {
if (typeNeedsHoisting(def)) {
Expand Down
11 changes: 10 additions & 1 deletion packages/sanity/test/cli/graphql/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
import {extractFromSanitySchema} from '../../../src/_internal/cli/actions/graphql/extractFromSanitySchema'
import {type ApiSpecification} from '../../../src/_internal/cli/actions/graphql/types'
import testStudioSchema from './fixtures/test-studio'
import unionRefsSchema from './fixtures/union-refs'

describe('GraphQL - Schema extraction', () => {
beforeEach(() => {
Expand All @@ -15,13 +16,21 @@ describe('GraphQL - Schema extraction', () => {
vi.runAllTimers()
})

it('Should be able to extract schema', () => {
it('Should be able to extract schema 1', () => {
const extracted = extractFromSanitySchema(testStudioSchema, {
nonNullDocumentFields: false,
})

expect(sortExtracted(extracted)).toMatchSnapshot()
})

it('Should be able to extract schema 2', () => {
const extracted = extractFromSanitySchema(unionRefsSchema, {
nonNullDocumentFields: false,
})

expect(sortExtracted(extracted)).toMatchSnapshot()
})
})

function sortExtracted(schema: ApiSpecification) {
Expand Down
Loading

0 comments on commit 3005a89

Please sign in to comment.