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

Add option for generate original type too #107

Closed
anhdd-kuro opened this issue Jul 3, 2022 · 6 comments
Closed

Add option for generate original type too #107

anhdd-kuro opened this issue Jul 3, 2022 · 6 comments
Labels
good first issue Good for newcomers

Comments

@anhdd-kuro
Copy link
Contributor

anhdd-kuro commented Jul 3, 2022

I know this plugin produces mutation validators in the first place, but can we have an option for original type as well?
Nowadays we are not only uses zod for mutations, but also validates the data received from the server after fetching.
I know I can just add the ID to the schema using zod's extend, but it would be even more convenient if this plugin could do that.

type Member {
  id: ID!
  firstName: String
  lastName: String
}

type MemberInput {
  firstName: String
  lastName: String
}

// ->
// add this
export function MemberSchema(): z.ZodObject<Properties<Member>> {
  return z.object({
    id: z.string(),
    firstName: z.string().nullish(),
    lastName: z.string().nullish(),
  })
}

export function MemberInputSchema(): z.ZodObject<Properties<MemberInput>> {
  return z.object({
    firstName: z.string().nullish(),
    lastName: z.string().nullish(),
  })
}

so I can

import { graphQLClient, Member, MemberSchema } from "@/infra/graphql";

const getMembers = async (query = defaultQuery): Promise<Member[]> => {
  const { members } = await graphQLClient.request(query);

  const validatedData = MemberSchema().array().parse(members);
  // instead of 
  // const validatedData = MemberInputSchema().extend({ id: z.string() }).array().parse(members);

  return validatedData;
};
@anhdd-kuro anhdd-kuro changed the title Add option for generate get data too Add option for generate original type too Jul 3, 2022
@github-actions github-actions bot added the good first issue Good for newcomers label Jul 3, 2022
@Brian-McBride
Copy link

I needed the same thing. I opened a PR #109

@Brian-McBride

This comment was marked as spam.

@Code-Hex
Copy link
Owner

Code-Hex commented Jul 4, 2022

@anhdd-kuro Thanks for your suggestion!

Previously, I closed the suggestion #20

this was because fragment support was likely to take time. I'd be happy to merge this and any other correspondence if someone could help me with this!

@Code-Hex
Copy link
Owner

Code-Hex commented Jul 4, 2022

I'm watching @Brian-McBride PR #109

@Code-Hex
Copy link
Owner

@Brian-McBride @anhdd-kuro Thanks for great work!!

I've merged PR #132. I will adjust it a little bit and publish it as a new version.

@Code-Hex
Copy link
Owner

@anhdd-kuro @Brian-McBride Hi 👋

I just published the latest version v0.6.0 which is included this feature.

Thank you!

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

3 participants