3. Adding a mutation to our schema
2m

Overview

We know how should look in a schema. Next we'll add incrementTrackViews and a response type to Catstronauts.

In this lesson, we will:

  • Add a Mutation type to our schema
  • Define incrementTrackViews with a required id
  • Create an IncrementTrackViewsResponse type with status and a nullable track

✍️ Updating our schema

To add a , let's go to our schema in our server/src folder, in the schema.graphql file.

We'll start with the type keyword, then Mutation, followed by curly braces.

type Mutation {
}

We want to increment the number of views for a track, so we'll call this incrementTrackViews. This needs to know which track to update, so we'll open up parentheses, and inside, we add an called id. This 's type is ID, and it's required, so we'll add an exclamation point (!) at the end.

incrementTrackViews(id: ID!)

We need a return type for this . We could return a single Track because that's what this updates, but as we saw in the previous lesson, there's a better approach.

Let's create a new type for our response. Following convention, we'll combine the name of our mutation (IncrementTrackViews) with Response.

type IncrementTrackViewsResponse {
}

This type will have the three we mentioned earlier:

  • code (a non-nullable Int)
  • success (a non-nullable Boolean)
  • and message (a non-nullable String)

Finally, we'll add the objects that were modified. In our case, we only had one: track of type Track. Note that track can be null, because our might fail.

Let's also add comments for each of these so that it makes our API documentation more useful.

"Similar to an HTTP status code, represents the status of the mutation"
code: Int!
"Whether the mutation completed successfully"
success: Boolean!
"A human-readable message for display in the UI"
message: String!
"The updated track after a successful mutation; null if the mutation failed"
track: Track

Lastly, we can set the return type of our to this new IncrementTrackViewsResponse type, and make it non-nullable. Here's what the incrementTrackViews should look like now:

server/src/schema.graphql
type Mutation {
"Increment the number of views for a track when its card is clicked"
incrementTrackViews(id: ID!): IncrementTrackViewsResponse!
}

Code review

Practice

In the mutation response type (IncrementTrackViewsResponse), why is the modified object's return type nullable (track: Track)?
Code Challenge!

Update Mutation.createPlanet to accept an input of type non-null CreatePlanetInput and return a non-null CreatePlanetResponse type. Next, complete CreatePlanetInput with name and mass fields. (See the Planet type for reference!)

Loading...
Loading progress

Up next

Now that our schema is good to go, let's figure out what endpoints we'll need to use to update our data.

Previous

Share your questions and comments about this lesson

Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.