Skip to content

Commit

Permalink
feat(graphql): add experimental mode to optimize/cache schema union d…
Browse files Browse the repository at this point in the history
…efinitions
  • Loading branch information
sgulseth authored and rexxars committed Jan 9, 2024
1 parent 330c621 commit 52e4369
Show file tree
Hide file tree
Showing 6 changed files with 30,297 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default async function deployGraphQLApiAction(
playground: playgroundFlag,
generation: generationFlag,
'non-null-document-fields': nonNullDocumentFieldsFlag,
withUnionCache,
} = flags

const {apiClient, output, prompt} = context
Expand Down Expand Up @@ -200,6 +201,7 @@ export default async function deployGraphQLApiAction(
typeof nonNullDocumentFieldsFlag === 'undefined'
? nonNullDocumentFields
: nonNullDocumentFieldsFlag,
withUnionCache,
})

apiSpec = generateSchema(extracted, {filterSuffix: apiDef.filterSuffix})
Expand Down Expand Up @@ -404,6 +406,7 @@ function parseCliFlags(args: {argv?: string[]}) {
.option('generation', {type: 'string'})
.option('non-null-document-fields', {type: 'boolean'})
.option('playground', {type: 'boolean'})
.option('with-union-cache', {type: 'boolean'})
.option('force', {type: 'boolean'}).argv
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ function getCrossDatasetReferenceMetadata(

export function extractFromSanitySchema(
sanitySchema: CompiledSchema,
extractOptions: {nonNullDocumentFields?: boolean} = {},
extractOptions: {nonNullDocumentFields?: boolean; withUnionCache?: boolean} = {},
): ApiSpecification {
const {nonNullDocumentFields} = extractOptions
const {nonNullDocumentFields, withUnionCache} = extractOptions
const unionRecursionGuards = new Set<string>()
const unionDefinitionCache = new Map<string, any>()
const hasErrors =
sanitySchema._validation &&
sanitySchema._validation.some((group) =>
Expand Down Expand Up @@ -456,6 +457,13 @@ export function extractFromSanitySchema(
return {}
}

const unionCacheKey = `${options.grandParent}-${guardPathName}-${candidates
.map((c) => c.type?.name)
.join('-')}`
if (withUnionCache && unionDefinitionCache.has(unionCacheKey)) {
return unionDefinitionCache.get(unionCacheKey)
}

try {
unionRecursionGuards.add(guardPathName)

Expand Down Expand Up @@ -532,9 +540,13 @@ export function extractFromSanitySchema(

const references = refs.length > 0 ? refs : undefined
const inlineObjects = inlineObjs.length > 0 ? inlineObjs : undefined
return isReference(parent)

const unionDefinition = isReference(parent)
? {type: name, references}
: {type: name, references, inlineObjects}

unionDefinitionCache.set(unionCacheKey, unionDefinition)
return unionDefinition
} finally {
unionRecursionGuards.delete(guardPathName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ configuration file. Tread with caution!
--non-null-document-fields Use non-null document fields (_id, _type etc)
--playground Enable GraphQL playground for easier debugging
--no-playground Disable GraphQL playground
--with-union-cache *Experimental:* Enable union cache that optimizes schema generation for schemas with many self referencing types
Examples
# Deploy all defined GraphQL APIs
Expand Down
Loading

2 comments on commit 52e4369

@vercel
Copy link

@vercel vercel bot commented on 52e4369 Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

performance-studio – ./

performance-studio.sanity.build
performance-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 52e4369 Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.