Returning null from a GraphQL mutation in type-graphql

Photo by Taylor Vick on Unsplash

Null is not a GraphQL type so to return null from a GraphQL mutation you have to return a GraphQL type that is nullable. All GraphQL types are nullable by default so you can just return a bool in the schema but return void from the implementation.

It shouldn't be too common to have a null response though, even for a mutation it could be better to return a state of the created item.

    // With type-graph ql you have to set the schema type nullable (Boolean!) AND set the nullable option to true
    @Mutation(returns => Boolean!, {nullable: true})
    @Authorized()
    async deployBranch(
        @Arg('deploySpecificBranchRequest', { nullable: false }) deploySpecificBranchRequest: DeploySpecificBranchRequest,
        @Ctx() ctx: Context
    ): Promise<void> {
        if (ctx && ctx.userId) {
          // some implementation in here
        }
    }

Hey! Are you a developer?

🚀 Set Up Your Dev Environment in Minutes, Not Hours!

Tired of spending hours setting up a new development machine? I used to be, too—until I automated the entire process!

Now, I just run a single script, grab a coffee, and let my setup take care of itself.

Save 30+ hours configuring a new Mac or Windows (WSL) development environment.
Ensure consistency across all your machines.
Eliminate tedious setup and get coding faster!
Get Instant Access →