Just a random question about graphql. Does anyone ...
# random
d
Just a random question about graphql. Does anyone have a best practice or a method/library they prefer to use for validations beyond the types setup in the schema? A quick google search and I found https://github.com/profusion/apollo-validation-directives which looks interesting
t
curious about this as well
n
I didn’t set it up but we’re using
type-graphql
and
zod
to make our own Decorators:
Copy code
import { ArgumentValidationError, createMethodDecorator } from "type-graphql";
import { z } from "zod";

export function ValidateArgs(schema: z.ZodSchema<any>, argName?: string): MethodDecorator {
    return createMethodDecorator((data, next) => {
        try {
            if (argName) {
                schema.parse(data.args[argName]);
            } else {
                schema.parse(data.args);
            }
        } catch (err: any) {
            throw new ArgumentValidationError(err);
        }

        return next();
    });
}
Copy code
@ValidateArgs(organizationFindOneSchema)
    @Query(() => Organization, { nullable: true })
    async organizationFindOne(@Arg("id") id: string): Promise<Organization | undefined> {
        return this.organizationService.findOne(id);
    }
t
has anyone looked at this
d
I looked it over when I was trying to handle BigInt values from a postgres DB (I ended up just creating my own custom scalar with new GraphQLScalarType though). I do like what they have noted about validations though. Have you tried it at all?
t
no about to give it a shot
d
I am late to the party when it comes to GraphQL but I have to say I am thoroughly impressed with it so far. Interested to hear how it goes for you
I’m thinking I might just write my own directives using graphql-tools. Doesn’t seem that crazy and gives more flexibility. https://www.graphql-tools.com/docs/schema-directives