Code-Hex/graphql-codegen-typescript-validation-schema

Support for non-input types?

markandrus opened this issue · 3 comments

I tried this out and, if I'm not mistaken, it only generates schemas (in my case, zod schemas) for input types. I would like to use this with non-input types as well (for example, to parse some JSON and ensure it adheres to the correct GraphQL type).

I would also greatly appreciate this. We have some generic functions that cache data returned from GQL queries, and want to make this as safe as possible, which entails parsing the cached data with zod before returning it. For this, we need auto-generated zod schemas for all generated query types.

MH4GF commented

@markandrus @efstajas Wouldn't withObjectType solve this?

https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/3a49a554b32d4c3121125c88a8e9f96c2e4b646f/README.md#withobjecttype

it('generate object type contains object type', async () => {
const schema = buildSchema(/* GraphQL */ `
type Book {
author: Author
title: String
}
type Author {
books: [Book]
name: String
}
`);
const result = await plugin(
schema,
[],
{
schema: 'zod',
withObjectType: true,
},
{},
);
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export function BookSchema(): z.ZodObject<Properties<Book>> {
return z.object({
__typename: z.literal('Book').optional(),
author: AuthorSchema().nullish(),
title: z.string().nullish()
})
}
export function AuthorSchema(): z.ZodObject<Properties<Author>> {
return z.object({
__typename: z.literal('Author').optional(),
books: z.array(BookSchema().nullable()).nullish(),
name: z.string().nullish()
})
}
"
`)
for (const wantNotContain of ['Query', 'Mutation', 'Subscription'])
expect(result.content).not.toContain(wantNotContain);
});

I close this issue because inactive. Please open again if you need anything else.