Skip to content

withObjectType contains non-requested properties (intentional?) #455

Open
@RobbyUitbeijerse

Description

@RobbyUitbeijerse

Hey @Code-Hex ! Thanks for this plug-in. It's helping us out quite a bit in preventing human errors in our application.

I was looking into the withObjectType but was facing some problems. The option generates models beautifully, but it generates the schemas to contain the entire model based on the introspected graphql model, instead of the one we've been requesting through our query (which might just be intentional).

For example, our query looks like this:

query getSomething($id: String!) {
  something(id: $id) {
    _id
    name
    details: {
      length
    }
  }
}

The entire something.details, contains more properties. Mostly information that we don't want to query, and don't need in our object schemas. Let's say the entire something model looks like this:

 something(id: $id) {
    _id
    name
    details: {
      length
      width
      height
    }
}

The output model schema now gets returned to us as:

export function SomethingSchema(): z.ZodObject<Properties<Something>> {
  return z.object({
    _id: z.string(),
    name: z.string().nullish(),
    details: z.object({
      length: z.string().nullish(),
      width: z.string().nullish(),
      height: z.string().nullish()
    })
  })
}

While this is technically correct in a sense that the properties in theory do exist, our query never contained the 'height' and 'width' properties, leaving us with a schema that doesn't actually match the actual data flowing through our application.

The data is set to nullish(), which is great in a way that you can still use the schema to parse data, however - we also use the schemas with our DTO solution, which takes Zod schemas and uses them to parse incoming/outgoing data and generates OpenAPI specs for our endpoints: https://github.com/risen228/nestjs-zod - the problem we face with this, is that the generated schema will suggest there will be more properties than we would actually have.

I think my question comes down to: Is it intentional that withObjectTypes contains all of the available properties on a graphql model, and if so - shouldn't there be an option to only generate schemas with the requested properties based on the query ?

Happy to think with you on this one, sorry if it has been asked before :)

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