Returning null from a GraphQL mutation in type-graphql

August 06, 2019

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
        }
    }

Darragh ORiordan

Hi! I'm Darragh ORiordan. I live and work in Auckland, New Zealand 🥝 enjoying the ocean 🏄 and building things on the web 💻 Contact me on Twitter!