Brian Han

Default Values with GraphQL + FaunaDB

May 25, 2020

I started dipping my toes into learning GraphQL today using FaunaDB with this guide here.

After logging in, and creating a database, I wrote a simple schema for a tiny workout app I’m building.

type Workout {
  pushups: Int
  squats: Int
  created: Date!
}

type Query {
  allWorkouts: [Workout!]
}

Basically, I want to build an app where I can input the number of pushups and squats I do in a day.

What’s cool about FaunaDB is that you can start with a basic schema and upload it to FaunaDB to generate a GraphQL API with all the basic CRUD actions.

Here’s a subset of the schema it generated for me based on my simple schema focusing on the WorkoutInput.

type Mutation {
  createWorkout(data: WorkoutInput!): Workout!  updateWorkout(
    id: ID!
    data: WorkoutInput!
  ): Workout
  deleteWorkout(id: ID!): Workout
}

input WorkoutInput {  pushups: Int  squats: Int  created: Date!}

Without the default values, running a createWorkout mutation would result in the values for pushups and squats to be null. Instead, I want them to default to 0 whenever a new workout is created.

This WorkoutInput is where can declare some default values!

Here’s how we can do that:

Copy and paste WorkoutInput into your own schema and add default values in the input.

type Workout {
  pushups: Int
  squats: Int
  created: Date!
}

type Query {
  allWorkouts: [Workout!]
}

input WorkoutInput {
  pushups: Int = 0  squats: Int = 0  created: Date!
}

Finally, update your schema in FaunaDB and write a createWorkout mutation. Now pushups and squats should default to 0.

mutation {
  createWorkout(data:{created: "2020-05-15"}) {
    pushups
    squats
  }
}
{
  "data": {
    "createWorkout": {
      "pushups": 0,
      "squats": 0
    }
  }
}

Brian Han is a UX developer living and working in Austin, Texas. He's quite bad at Twitter., but you should follow him anyways.

© 2020 Brian Han