Skip to content

Commit

Permalink
feat(cli): add CDR metadata to GraphQL json schema (#4975)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler authored Oct 17, 2023
1 parent 9b1cfed commit cb80240
Show file tree
Hide file tree
Showing 6 changed files with 872 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
ObjectSchemaType,
ArraySchemaType,
IntrinsicTypeName,
CrossDatasetReferenceSchemaType,
} from '@sanity/types'
import {generateHelpUrl} from '@sanity/generate-help-url'
import {Schema} from '@sanity/schema'
Expand Down Expand Up @@ -110,10 +111,43 @@ function isReference(
return isType(typeDef, 'reference')
}

function isCrossDatasetReference(typeDef: SchemaType) {
function isCrossDatasetReference(
typeDef: SchemaType | ObjectField | ObjectFieldType | CrossDatasetReferenceSchemaType,
) {
return isType(typeDef, 'crossDatasetReference')
}

function getCrossDatasetReferenceMetadata(
typeDef: SchemaType | ObjectField | ObjectFieldType | CrossDatasetReferenceSchemaType,
) {
if (!isCrossDatasetReference(typeDef)) return undefined

function getTypeNames(
type: SchemaType | ObjectField | ObjectFieldType | CrossDatasetReferenceSchemaType | undefined,
) {
if (!type) return undefined
if (!('to' in type)) return getTypeNames(type.type)
return type.to.map((t) => t.type).filter((t): t is string => typeof t === 'string')
}

function getDataset(
type: SchemaType | ObjectField | ObjectFieldType | CrossDatasetReferenceSchemaType | undefined,
) {
if (!type) return undefined
if ('dataset' in type && typeof type.dataset === 'string') return type.dataset
if (type.type) return getDataset(type.type)
return undefined
}

const typeNames = getTypeNames(typeDef)
if (!typeNames) return undefined

const dataset = getDataset(typeDef)
if (typeof dataset !== 'string') return undefined

return {typeNames, dataset}
}

export function extractFromSanitySchema(
sanitySchema: CompiledSchema,
extractOptions: {nonNullDocumentFields?: boolean} = {},
Expand Down Expand Up @@ -228,7 +262,14 @@ export function extractFromSanitySchema(
const gqlName = props.fieldName || mapped.name
const originalName = type.name
const original = gqlName === originalName ? {} : {originalName: originalName}
return {...props, ...mapped, ...original}
const crossDatasetReferenceMetadata = getCrossDatasetReferenceMetadata(type)

return {
...props,
...mapped,
...original,
...(crossDatasetReferenceMetadata && {crossDatasetReferenceMetadata}),
}
}

function isField(def: SchemaType | ObjectField): def is ObjectField {
Expand Down
4 changes: 4 additions & 0 deletions packages/sanity/src/_internal/cli/actions/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export interface ConvertedType {
interfaces?: string[]
originalName?: string
isReference?: boolean
crossDatasetReferenceMetadata?: {
dataset: string
typeNames: string[]
}
}

export interface ConvertedDocumentType extends ConvertedType {
Expand Down
113 changes: 113 additions & 0 deletions packages/sanity/test/cli/graphql/__snapshots__/extract.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,51 @@ Object {
"originalName": "block",
"type": "Object",
},
Object {
"crossDatasetReferenceMetadata": Object {
"dataset": "production",
"typeNames": Array [
"person",
],
},
"description": undefined,
"fields": Array [
Object {
"description": undefined,
"fieldName": "_dataset",
"type": "String",
},
Object {
"description": undefined,
"fieldName": "_key",
"type": "String",
},
Object {
"description": undefined,
"fieldName": "_projectId",
"type": "String",
},
Object {
"description": undefined,
"fieldName": "_ref",
"type": "String",
},
Object {
"description": undefined,
"fieldName": "_type",
"type": "String",
},
Object {
"description": undefined,
"fieldName": "_weak",
"type": "Boolean",
},
],
"kind": "Type",
"name": "CdrPersonReference",
"originalName": "cdrPersonReference",
"type": "Object",
},
Object {
"description": undefined,
"fields": Array [
Expand Down Expand Up @@ -339,6 +384,74 @@ Object {
"originalName": "documentActionsTest",
"type": "Object",
},
Object {
"description": undefined,
"fields": Array [
Object {
"description": "Date the document was created",
"fieldName": "_createdAt",
"isNullable": true,
"type": "Datetime",
},
Object {
"description": "Document ID",
"fieldName": "_id",
"isNullable": true,
"type": "ID",
},
Object {
"description": undefined,
"fieldName": "_key",
"type": "String",
},
Object {
"description": "Current document revision",
"fieldName": "_rev",
"isNullable": true,
"type": "String",
},
Object {
"description": "Document type",
"fieldName": "_type",
"isNullable": true,
"type": "String",
},
Object {
"description": "Date the document was last modified",
"fieldName": "_updatedAt",
"isNullable": true,
"type": "Datetime",
},
Object {
"crossDatasetReferenceMetadata": Object {
"dataset": "production",
"typeNames": Array [
"person",
"place",
],
},
"fieldName": "cdrFieldInline",
"type": "CrossDatasetReference",
},
Object {
"crossDatasetReferenceMetadata": Object {
"dataset": "production",
"typeNames": Array [
"person",
],
},
"fieldName": "cdrFieldNamed",
"type": "CdrPersonReference",
},
],
"interfaces": Array [
"Document",
],
"kind": "Type",
"name": "DocumentWithCdrField",
"originalName": "documentWithCdrField",
"type": "Object",
},
Object {
"description": undefined,
"fields": Array [
Expand Down
Loading

2 comments on commit cb80240

@vercel
Copy link

@vercel vercel bot commented on cb80240 Oct 17, 2023

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-git-next.sanity.build
performance-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on cb80240 Oct 17, 2023

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.sanity.build
test-studio-git-next.sanity.build

Please sign in to comment.