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 18, 2024
1 parent cf7afaf commit 8f6799f
Show file tree
Hide file tree
Showing 4 changed files with 497 additions and 2 deletions.
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()

Check failure on line 24 in packages/sanity/test/cli/graphql/extract.test.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest / node 20)

test/cli/graphql/extract.test.ts > GraphQL - Schema extraction > Should be able to extract schema 1

Error: Snapshot `GraphQL - Schema extraction > Should be able to extract schema 1 1` mismatched ❯ test/cli/graphql/extract.test.ts:24:38

Check failure on line 24 in packages/sanity/test/cli/graphql/extract.test.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest / node 18)

test/cli/graphql/extract.test.ts > GraphQL - Schema extraction > Should be able to extract schema 1

Error: Snapshot `GraphQL - Schema extraction > Should be able to extract schema 1 1` mismatched ❯ test/cli/graphql/extract.test.ts:24:38
})

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

expect(sortExtracted(extracted)).toMatchSnapshot()

Check failure on line 32 in packages/sanity/test/cli/graphql/extract.test.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest / node 20)

test/cli/graphql/extract.test.ts > GraphQL - Schema extraction > Should be able to extract schema 2

Error: Snapshot `GraphQL - Schema extraction > Should be able to extract schema 2 1` mismatched ❯ test/cli/graphql/extract.test.ts:32:38

Check failure on line 32 in packages/sanity/test/cli/graphql/extract.test.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest / node 18)

test/cli/graphql/extract.test.ts > GraphQL - Schema extraction > Should be able to extract schema 2

Error: Snapshot `GraphQL - Schema extraction > Should be able to extract schema 2 1` mismatched ❯ test/cli/graphql/extract.test.ts:32:38
})
})

function sortExtracted(schema: ApiSpecification) {
Expand Down
Loading

0 comments on commit 8f6799f

Please sign in to comment.