Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

fix(v1): handle _key and _type in objects #294

Open
wants to merge 3 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/core/src/generate-schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function generateSchemaTypes({
const structure = transformSchemaNodeToStructure({
node,
normalizedSchema,
ancestors: [],
});

// TODO: allow customizing this?
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/transform-schema-to-structure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ describe('transformSchemaToStructure', () => {
type: 'Array',
of: {
type: 'Object',
properties: [{ key: 'name', value: { type: 'String' } }],
properties: [
{ key: '_type', value: { type: 'String', value: 'author' } },
{ key: '_key', value: { type: 'String' } },
{ key: 'name', value: { type: 'String' } },
],
},
},
},
Expand Down
45 changes: 38 additions & 7 deletions packages/core/src/transform-schema-to-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export function transformSchemaToStructure({
children: normalizedSchema.documents
.map((node) =>
removeOptional(
transformSchemaNodeToStructure({ node, normalizedSchema }),
transformSchemaNodeToStructure({
node,
normalizedSchema,
ancestors: [],
}),
),
)
// for consistent output
Expand All @@ -50,6 +54,10 @@ interface TransformSchemaNodeToStructureOptions {
* `@sanity-codegen/extractor` package.
*/
normalizedSchema: Sanity.SchemaDef.Schema;
/**
* The ancestors of the current schema node (extracted from `@sanity-codegen/extractor`)
*/
ancestors: Sanity.SchemaDef.SchemaNode[];
}

/**
Expand All @@ -58,7 +66,13 @@ interface TransformSchemaNodeToStructureOptions {
export function transformSchemaNodeToStructure({
node,
normalizedSchema,
ancestors,
}: TransformSchemaNodeToStructureOptions): Sanity.GroqCodegen.StructureNode {
const parent = ancestors[ancestors.length - 1] || undefined;
const options = {
ancestors: [...ancestors, node],
normalizedSchema,
};
switch (node.type) {
case 'RegistryReference': {
const referencedType = [
Expand All @@ -79,8 +93,8 @@ export function transformSchemaNodeToStructure({
hashInput: `${objectHash(normalizedSchema)}:${referencedType.name}`,
get: () =>
transformSchemaNodeToStructure({
...options,
node: referencedType,
normalizedSchema,
}),
});
}
Expand All @@ -92,7 +106,7 @@ export function transformSchemaNodeToStructure({
of: createStructure({
type: 'Or',
children: node.of.map((node) =>
transformSchemaNodeToStructure({ node, normalizedSchema }),
transformSchemaNodeToStructure({ ...options, node }),
),
}),
});
Expand Down Expand Up @@ -178,8 +192,8 @@ export function transformSchemaNodeToStructure({
// the rest
...(node.of || []).map((child) =>
transformSchemaNodeToStructure({
...options,
node: child,
normalizedSchema,
}),
),
],
Expand Down Expand Up @@ -240,7 +254,10 @@ export function transformSchemaNodeToStructure({

const properties: ObjectProperties = [];

if (node.type === 'Document') {
if (
node.type === 'Document' ||
(node.type === 'Object' && parent?.type === 'Array')
) {
properties.push({
key: '_type',
value: createStructure({
Expand All @@ -250,7 +267,21 @@ export function transformSchemaNodeToStructure({
value: node.name,
}),
});
}

if (node.type === 'Object' && parent?.type === 'Array') {
properties.push({
key: '_key',
value: createStructure({
type: 'String',
canBeNull: false,
canBeOptional: false,
value: null,
}),
});
}

if (node.type === 'Document') {
properties.push({
key: '_id',
value: createStructure({
Expand Down Expand Up @@ -292,8 +323,8 @@ export function transformSchemaNodeToStructure({
const fieldProperties = node.fields?.map((field) => ({
key: field.name,
value: transformSchemaNodeToStructure({
...options,
node: field.definition,
normalizedSchema,
}),
}));

Expand Down Expand Up @@ -333,7 +364,7 @@ export function transformSchemaNodeToStructure({
to: createStructure({
type: 'Or',
children: node.to.map((node) =>
transformSchemaNodeToStructure({ node, normalizedSchema }),
transformSchemaNodeToStructure({ ...options, node }),
),
}),
canBeNull: false,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/transform-structure-to-ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ describe('transformStructureToTs', () => {
| string
| number
| {
_key: string;
_type: "objectLike";
properties?: {
_key: string;
_type: "propertyPair";
Comment on lines +147 to +151
Copy link
Contributor Author

@ochicf ochicf May 11, 2023

Choose a reason for hiding this comment

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

@ricokahler the tests failed here because these properties are being generated now, so I added them aswell to make the tests pass. I think it makes sense but I'm not fully sure these properties are there in this case?

key?: string;
value?: Sanity.Ref.Ref_vO3XB5BrB0AvbYEU;
}[];
Expand Down