Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add CDR metadata to GraphQL json schema #4975

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?: {
binoy14 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading