incetarik / nestjs-graphql-zod

A library providing dynamic GraphQL object classes from their zod validation objects.
Mozilla Public License 2.0
83 stars 10 forks source link

Add examples for Mutations #4

Closed sschneider-ihre-pvs closed 2 years ago

sschneider-ihre-pvs commented 2 years ago

There are only query examples, please add one for Mutations like create, update, delete.

incetarik commented 2 years ago

There are only query examples, please add one for Mutations like create, update, delete.

You can simply write Mutation instead of Query like MutationWithZod. The subscription is also the same. They have very same overloads for corresponding decorator overloads.

sschneider-ihre-pvs commented 2 years ago

But what about if none nullable competes with zod required?

sschneider-ihre-pvs commented 2 years ago

so if you have a field that is required for a database operation maybe even a primary key and it is allowed to be nullable, so in the graphql schema it needs to be nullable also. but in the zod schema either default null or required

incetarik commented 2 years ago

so if you have a field that is required for a database operation maybe even a primary key and it is allowed to be nullable, so in the graphql schema it needs to be nullable also. but in the zod schema either default null or required

Could you please provide an example of what you want to mention? Because I think I need to get this well, it is not clear for me yet.

sschneider-ihre-pvs commented 2 years ago
id primary nullable value
[null] "----"
1 "Hello World"
incetarik commented 2 years ago

The default values are also applied if you provide the default with the zod. For example:

const Scheme = z.object({
  id: z.string().optional().default('----')
}).describe('DbTable: Example')

With this scheme, it is expected to have the following GraphQL scheme:

"""Example"""
model DbTable {
  id: String
}

As you see the id field is now nullable.

So you apply this scheme object to any method like:

class ResolverClass {
  @MutationWithZod(Scheme)
  method() {
    // actual logic here
  }
}

Now as you have used .default(...) call on the zod scheme above, now your field/property will have its default value. Therefore if nothing is given from the client, the default value will be used. I hope this answers your question @sschneider-ihre-pvs.