Skip to content

Generate schemas for Query operations #472

Closed

Description

@macmillen

I need the schemas not just for graphql inputs but also for graphql queries to be able to validate preloaded data from the database for a page. I can't just use withObjectType because it potentially contains more fields than the graphql query.

Example

GraphQL type:

type Author {
  email: String!
  name: String!
  password: String!
}

My GraphQL query operation:

query Author {
  author {
    email
  }
}

The zod schema I get:

export function AuthorSchema(): z.ZodObject<Properties<Author>> {
  return z.object({
    email: z.string(),
    name: z.string(), // this field I don't want
    password: z.string() // this field I don't want
  })
}

Instead I need a schema like this:

export function AuthorQuerySchema(): z.ZodObject<Properties<AuthorQuery>> {
  return z.object({
    email: z.string()
  })
}

Activity

  1. charlescrain commented on Feb 27, 2025

    @charlescrain

    Curious if there's any plan to add this feature? Would be super helpful to have schema parsers for queries when the data is cached somewhere and needs to be parsed back.

  2. Code-Hex commented on Feb 28, 2025

    @Code-Hex
    Owner

    @charlescrain No I don't have plan for query. I accept PR because there are too many patterns, but most recently I am not going to work on them

  3. Code-Hex commented on Apr 25, 2026

    @Code-Hex
    Owner

    Fixed in #1423. Zod and Zod v4 now have withOperationType, which generates schemas from operation selection sets instead of the full object type.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions