Giter Club home page Giter Club logo

Comments (6)

davidhousedev avatar davidhousedev commented on July 30, 2024 2

@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.

davidhousedev avatar davidhousedev commented on July 30, 2024

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.

stevenpetryk avatar stevenpetryk commented on July 30, 2024

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.

davidhousedev avatar davidhousedev commented on July 30, 2024

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.

stevenpetryk avatar stevenpetryk commented on July 30, 2024

Glad you found the right tool for the job! And I super appreciate that you opened a PR :)

from contentful-typescript-codegen.

sonia-y avatar sonia-y commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.