-
Notifications
You must be signed in to change notification settings - Fork 431
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: add cmd to generate a JSON representation of schema #5919
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
No changes to documentation |
Component Testing Report Updated Mar 12, 2024 10:48 AM (UTC)
|
39b0521
to
ef0c637
Compare
New dependencies detected. Learn more about Socket for GitHub ↗︎
|
ef0c637
to
494c46a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave the bulk of the review up to the tagged reviewers, but I have some thoughts/questions:
- A bit reluctant to tie
@sanity/schema
togroq-js
- A schema extract command makes total sense to me, but the resulting format should preferably be generic enough that it can be used to generate all kinds of things; GraphQL types, TypeScript types, tailored API clients etc.
- Generally I would favor using a more established output type, say JSONSchema, as long as it is able to represent our schema completely.
- Perhaps we could introduce a format parameter/flag? We don't have to implement any other formats right now, but I think requiring the flag for now makes it more future-compatible if we want to default to (say) JSONSchema in the future.
- Using the compiled schema (
Schema.compile()
/createSchema()
) would be safer- We shouldn't have to special-case image/file fields/types etc - and defining their properties are already done for you with this approach. It feels brittle/unnecessary to have to maintain two code paths if we add new fields.
- The generated type output may be missing types such as image and file asset documents, geopoints, slugs and other non-user-defined types
- Block types shouldn't have to be special-cased - they (should) just be an object type like any other. Given you can create named block types, this is important - as the
name
property may not beblock
- Without having looked deeply at the code, I am curious if the current approach handles aliased types, or if that is left to whatever acts on these types to figure out
packages/sanity/src/_internal/cli/actions/schema/extractAction.ts
Outdated
Show resolved
Hide resolved
packages/sanity/src/_internal/cli/actions/schema/extractAction.ts
Outdated
Show resolved
Hide resolved
packages/sanity/src/_internal/cli/actions/schema/extractAction.ts
Outdated
Show resolved
Hide resolved
packages/sanity/src/_internal/cli/actions/schema/extractAction.ts
Outdated
Show resolved
Hide resolved
packages/sanity/src/_internal/cli/commands/schema/extractSchemaCommand.ts
Outdated
Show resolved
Hide resolved
Options | ||
--workspace <name> The name of the workspace to generate a schema for | ||
--path Optional path to specify destination of the schema file | ||
--enforce-required-fields Makes the schema generated respect required fields in the schema. This can be used when generating types if you are only querying for published documents. Defaults to false. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- This seems broader than just for querying - I would phrase this to be "treat fields marked as required as non-optional", as what it impacts is the resulting
optional
attributes - "querying for published documents" doesn't really encompass this - both drafts and published documents can be incomplete/not follow schema (documents that were published prior to a field being introduced, for instance)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems broader than just for querying - I would phrase this to be "treat fields marked as required as non-optional", as what it impacts is the resulting optional attributes
True, and agree on your phrasing 👍
This seems broader than just for querying - I would phrase this to be "treat fields marked as required as non-optional", as what it impacts is the resulting optional attributes
"querying for published documents" doesn't really encompass this - both drafts and published documents can be incomplete/not follow schema (documents that were published prior to a field being introduced, for instance).
Yeah, we do however consider the schema the source of truth so if anything diverges from your schema we don't give any guarantees with the types at least 🤔
packages/sanity/src/_internal/cli/commands/schema/extractSchemaCommand.ts
Show resolved
Hide resolved
👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎ This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored. |
Where would you prefer these types to live? I don't see any reasons this couldn't be used with our graphql generator as well, would potentially need to extend ObjectAttribute to include more data, but that's doable. We did talk about using jsonschema, but the ecosystem is a bit fragmented with different specification and support, so we opted for our own implementation here. cc. @judofyr for additional feedback.
We could. Would then this be a required flag to set? ie
Ah, nice. I'll look into this!
It does, they are referred to as |
494c46a
to
0d38a52
Compare
Any reason extractSchema couldn't live with the cli command for now? |
@bjoerge then we either need to have the types in two places, guess that works with how ts infers types. Or groq-js would need to depend on the cli, which isn't ideal either |
Not sure, honestly - probably fine for now.
Would be interested in learning more about this, but I don't need a full write-up here - if there's any TLDR or links I can use to understand more that'd be great.
Ideally I would default to something that is a defined standard, such as JSONSchema, and make other formats a flag. I understand that it's an improved DX when we only have a single format, but it would also be a worse DX when we change the default (breaking change). |
0fcd3cf
to
b2f30bb
Compare
b2f30bb
to
86f94d4
Compare
86f94d4
to
4afe4ab
Compare
I have adressed the feedback and we are now processing the compiled types. OK to move the format discussion to slack? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this looks good to me, but would love if @bjoerge who knows the schema better could have a look.
One curiosity; seems to me like Portable Text block and span types do not have a _type
property? Span should always be span
(the type cannot be changed in any way), and the block type should either be block
or the name given by the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Left a few comments for your consideration, only nits/nice-to-haves, so good to merge from my side!
packages/@sanity/schema/test/extractSchema/extractSchema.test.ts
Outdated
Show resolved
Hide resolved
75d0de2
to
b9dd36d
Compare
* feat: add cmd to generate a JSON representation of schema * feat: refactor to use compiled schema * fix: append _type to objects * chore(schema): clean up and describe execution * chore(schema): lock groq-js to minor canary
Description
Adds a new command that can be executed within a sanity context to extract a JSON representation of the studios schema. This makes it easier for other tools(TM) to consume it without depending on browser environments etc. I tried to base most of the logic on
sanity schema validate
to keep consistency in the repo/package.What to review
Correctness: Do we parse the schema correctly?
Naming things: It's hard! Can we name the command line argument or flags differently?
Note that we consider this feature beta, anything more than in the help text it should be indicated?
Testing
Notes for release
Beta: Extract sanity schema as JSON, to be consumed by other tools