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

Schema extract fails if "flat" custom type #7801

Open
heggemsnes opened this issue Nov 12, 2024 · 2 comments
Open

Schema extract fails if "flat" custom type #7801

heggemsnes opened this issue Nov 12, 2024 · 2 comments
Labels

Comments

@heggemsnes
Copy link

Describe the bug

Using a field like

defineType({
    name: 'externalLink',
    type: 'url',
    title: 'External Link',
  }),

Somewhere directly like:

defineType({
    name: 'pageWithExternalLink',
    type: 'document',
    title: 'Page With External Link',
    fields: [
      {
        name: 'title',
        type: 'string',
        title: 'Title',
      },
      {
        name: 'link',
        type: 'externalLink',
        title: 'External Link',
      },
    ],
  }),
  

Causes error on schema extract

> npm run extract-types


> [email protected] extract-types
> sanity schema extract --enforce-required-fields

❌️ Failed to extract schema, with enforced required fields

Error: Type "externalLink" not found
    at convertSchemaType (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:120:11)
    at createObject (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:125:63)
    at convertBaseType (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:64:83)
    at ~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:58:18
    at Array.forEach (<anonymous>)
    at Object.extractSchema (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:54:33)
    at main (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/sanity/lib/_internal/cli/threads/extractSchema.js:10:195)

To Reproduce

Steps to reproduce the behavior:

Check my forked repo from the clean next.js template: https://github.com/heggemsnes/sanity-template-nextjs-clean

I've changed the schemaTypes to the examples before.

Add a valid project ID and run npm run extract-types

Expected behavior

This is valid Sanity code (I think?) and works fine in the studio. Useful for reusable validations etc.

Which versions of Sanity are you using?

@sanity/cli (global) 3.62.0 (latest: 3.63.0)
@sanity/assist 3.0.8 (up to date)
@sanity/eslint-config-studio 4.0.0 (up to date)
@sanity/icons 3.4.0 (up to date)
@sanity/vision 3.61.0 (latest: 3.63.0)
sanity 3.62.2 (latest: 3.63.0)

What operating system are you using?

MacOS 14.3.1

Which versions of Node.js / npm are you running?

10.2.4
v20.11.0

@basharalidreesi
Copy link

basharalidreesi commented Nov 13, 2024

I've encountered this problem as well, but with a boolean type. To make TypeGen work, I had to comment out all instances of this custom type, run the TypeGen commands, then manually add the correct type definitions to the output file (which defeats the purpose).

@rexxars rexxars added the typegen Issues related to TypeScript types generation label Nov 16, 2024
@sgulseth
Copy link
Member

sgulseth commented Dec 5, 2024

We attempt to sort all types by dependencies, but this is a bug where we apparently aren't picking up the dependency between pageWithExternalLink and externalLink.

To mitigate for now you can just switch the types, while we work on a fix:

scratch/sanity-7801/studio ‹main*› » git --no-pager diff -- src                                                                                                                                               130 ↵
diff --git a/studio/src/schemaTypes/index.ts b/studio/src/schemaTypes/index.ts
index e3862f9..5939c64 100644
--- a/studio/src/schemaTypes/index.ts
+++ b/studio/src/schemaTypes/index.ts
@@ -2,12 +2,6 @@
 import {defineType} from 'sanity'
 
 export const schemaTypes = [
-  defineType({
-    name: 'externalLink',
-    type: 'url',
-    title: 'External Link',
-  }),
-
   defineType({
     name: 'pageWithExternalLink',
     type: 'document',
@@ -25,4 +19,10 @@ export const schemaTypes = [
       },
     ],
   }),
+
+  defineType({
+    name: 'externalLink',
+    type: 'url',
+    title: 'External Link',
+  }),
 ]
scratch/sanity-7801/studio ‹main*› » pnpm sanity schema extract --enforce-required-fields
✅ Extracted schema to /Users/sgulseth/workspace/src/sindre.dev/scratch/sanity-7801/studio/schema.json with enforced required fields

@sgulseth sgulseth added the bug label Dec 5, 2024
@linear linear bot removed the typegen Issues related to TypeScript types generation label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants