Skip to content

Use codegen on non-input types #20

Description

@willd-mwp

It seems that with the current version of the plugin, validation schemas are only generated for graphql input types found in the schema. i was wondering if it would be possible to add an option to the plugin to generate validation schemas for graphql type as well as input?

Activity

  1. Code-Hex commented on Mar 3, 2022

    @Code-Hex
    Owner

    @willd-i2o I'm just curious.
    How do you think the usage if available code generation for type too.

  2. willd-mwp commented on Mar 3, 2022

    @willd-mwp
    Author

    @Code-Hex

    I use apollo client alongside graphql-codegen
    My form state is populated with data from useQuery, these queries utilise the type objects defined in the schema, so therefore my form state is typed against these type objects.
    My validation schema has to match the form state, since I'm running the validation schema via the zod resolver for react-hook-form.
    The input types are only used when I'm submitting the form and transforming my form state data into a mutation input type. The input types don't match 1-to-1 with the type types used in the query, so the codegen producing validation schema's for my input types doesn't help me with constructing a validation schema for my form.

  3. Code-Hex commented on Mar 4, 2022

    @Code-Hex
    Owner

    @willd-i2o Thanks for your explanation.
    I want to confirm my understanding is correct. for example, there is a schema.

    type Human {
      name: String!
      age: Int!
    }
    
    input HumanInput {
      name: String!
      age: Int!
    }

    Then you want to validate only for type Human. and convert any users who have been inputted type Human to input HumanInput when submitting to mutation, right?

  4. willd-mwp commented on Mar 4, 2022

    @willd-mwp
    Author

    @Code-Hex

    Yeah that sounds about right

  5. Code-Hex commented on Mar 4, 2022

    @Code-Hex
    Owner

    @willd-i2o Thanks. Don't you use fragment also?
    I think this a little bit difficult.

  6. willd-mwp commented on Mar 4, 2022

    @willd-mwp
    Author

    @Code-Hex

    I think I could do what I would like to do without the codegen supporting fragments, but not entirely sure.

  7. Code-Hex commented on Mar 14, 2022

    @Code-Hex
    Owner

    @willd-i2o I'm sorry.

    After due consideration, I did not feel this desperately needed to be addressed. If there are requests from others, I will revisit this issue.

  8. JarekToro commented on Apr 13, 2022

    @JarekToro

    Interest here

  9. Brian-McBride commented on Jun 30, 2022

    @Brian-McBride

    I use Zod to validate data from my database and other sources as well. Sure that incoming data should be in the model you expect. Of course, corrupted data or a service that broke their API contract would then cause errors in your app.

    I can write my own Zod schemas, of course. But I have to define my outgoing type anyway in graphQL.
    Let's take this example:

      type User {
        id: ID!
        email: String!
        disabled: Boolean
        createdAt: String
        updatedAt: String
      }
    
      input NewUser {
        id: ID!
        email: String!
      }
    
      type Query {
        user(id: ID!): User
      }

    At some point I am going to assemble that User JSON for my user() resolver. Let's say I am fetching the data from something like Firebase Firestore. Well, Firestore give you any as the base type. Sure, I can cast it in a few ways - but if I don't actually validate that data - it might not be valid.

    If I can auto-generate zod types from the graphql types, then I can further pick, omit, extend, etc... the zod schema for my DB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions