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

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

Open
RobbyUitbeijerse opened this issue Aug 14, 2023 · 1 comment
Open

Comments

@RobbyUitbeijerse
Copy link

RobbyUitbeijerse commented Aug 14, 2023

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 :)

@Code-Hex
Copy link
Owner

Code-Hex commented Dec 9, 2023

@RobbyUitbeijerse I'm sorry I don't have the motivation to update for withObjectType option.
Could you send me PR for this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants