Comments (6)
@sonia-y we've since migrated to use graphql-code-generator too and I really like that everything is nullable because of preview environments. We found that we were encountering many type errors in preview environments (where validation hasn't run yet). I suppose if you only read published data and you require everything, the utility is lower :).
from contentful-typescript-codegen.
I'm going to take a crack on this in a fork, which I'm happy to contribute back upstream. Presently, I plan to add a new renderer
and an accompanying --props-only
flag which will render all interfaces without the top-level field
.
Let me know if taking a different approach would make it easier to accept my contribution!
from contentful-typescript-codegen.
I think I understand the use case—you may want to query just fields for everything, including references, but this library only gives you this structure:
BlogPostFields
author: Author
fields: { ... }
sys: { ... }
Whereas you want:
BlogPostFields
author: AuthorFields
{ ... }
I'm hesitant to support this use case, because opening this library up to the world of GraphQL means that we'll have to shape the types based on the query, rather than Contentful's content model.
For the sake of describing the problem, forget Contentful exists for a moment. Imagine an API has a post
type with the following fields:
type Post {
author: Author!
title: String!
body: String!
permalink: String!
}
And imagine we generate the following TS declaration:
interface IPost {
author: IAuthor
title: string
body: string
permalink: string
}
But then we run the following query:
posts(id: 4) {
title
body
}
We can't say that the result here is an IPost
, because it doesn't have author
or permalink
. So we need a new type to be generated based on the query, not the schema, which this library is in no position to do without significant modifications.
For your use case, do you think something like Apollo's TypeScript codegen could work better for you? You can point it at Contentful's GraphQL endpoint, and it will analyze your code and generate TypeScript interfaces that match your queries:
https://github.com/apollographql/apollo-tooling#apollo-clientcodegen-output
from contentful-typescript-codegen.
Thank you @stevenpetryk , I totally see your hesitation and it makes sense to me. Thank you for pointing me in the direction of the Apollo tool! I don't think I'll be using that since we're using Gatsby, but I think that https://www.gatsbyjs.org/packages/gatsby-plugin-graphql-codegen/ could meet our needs far better than what would be partial support from this library.
Thanks again!
from contentful-typescript-codegen.
Glad you found the right tool for the job! And I super appreciate that you opened a PR :)
from contentful-typescript-codegen.
Actually facing a similar issue... originally was using graphql-code-generator to generate all of the contentful types, but the issue with Contentful's GraphQL schema is that all fields are nullable/optional, which doesn't allow us to really leverage TS very well since we need to do null-checks everywhere.
from contentful-typescript-codegen.
Related Issues (20)
- The automated release is failing 🚨 HOT 2
- Smooth integration with next.js , env.local and alternative to getContentfulEnvironment HOT 5
- TypeError: Cannot read property 'getContentTypes' of undefined HOT 6
- Imports { Document } when it is not used HOT 2
- Feature Request: Export union of all entries HOT 1
- Fields are incorrectly extending Entry HOT 7
- ERR_REQUIRE_ESM in Node 16.6 HOT 5
- Not using prettierrc.json
- Missing collection types in generated file HOT 3
- Type narrowing not working with current setup HOT 1
- generating typescript models fails for specific space with TypeError: contentTypeValidation.linkContentType.map is not a function
- Syntax error from code generation
- TypeError: functionMap[field.type] is not a function HOT 2
- Update to work with Contentful v10 HOT 8
- Non-required attributes are not automatically generated to have `| null` appended to type
- getContenfulEnvironment.ts HOT 1
- Prettier config not resolved correctly
- contentful^8.1.9 is required HOT 1
- Top level id, contentType and type on Entry HOT 1
- have option to add namespace and own commenting to generated types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from contentful-typescript-codegen.