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

Use codegen on non-input types #20

Closed
willd-mwp opened this issue Mar 2, 2022 · 9 comments
Closed

Use codegen on non-input types #20

willd-mwp opened this issue Mar 2, 2022 · 9 comments
Labels
good first issue Good for newcomers

Comments

@willd-mwp
Copy link

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?

@github-actions github-actions bot added the good first issue Good for newcomers label Mar 2, 2022
@Code-Hex
Copy link
Owner

Code-Hex commented Mar 3, 2022

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

@willd-mwp
Copy link
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.

@Code-Hex
Copy link
Owner

Code-Hex commented Mar 4, 2022

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

@willd-mwp
Copy link
Author

@Code-Hex

Yeah that sounds about right

@Code-Hex
Copy link
Owner

Code-Hex commented Mar 4, 2022

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

@willd-mwp
Copy link
Author

@Code-Hex

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

@Code-Hex
Copy link
Owner

Code-Hex commented Mar 14, 2022

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

@JarekToro
Copy link

Interest here

@Brian-McBride
Copy link

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
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants