Overview
We know how mutations should look in a schema. Next we'll add incrementTrackViews and a dedicated response type to Catstronauts.
In this lesson, we will:
- Add a
Mutationtype to our schema - Define
incrementTrackViewswith a requiredidargument - Create an
IncrementTrackViewsResponsetype with status fields and a nullabletrack
✍️ Updating our schema
To add a mutation, 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 mutation incrementTrackViews. This mutation needs to know which track to update, so we'll open up parentheses, and inside, we add an argument called id. This argument'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 mutation. We could return a single Track because that's what this mutation updates, but as we saw in the previous lesson, there's a better approach.
Let's create a new type for our mutation response. Following convention, we'll combine the name of our mutation (IncrementTrackViews) with Response.
type IncrementTrackViewsResponse {}
This type will have the three fields we mentioned earlier:
code(a non-nullableInt)success(a non-nullableBoolean)- and
message(a non-nullableString)
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 mutation might fail.
Let's also add comments for each of these fields so that it makes our GraphQL 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 mutation to this new IncrementTrackViewsResponse type, and make it non-nullable. Here's what the incrementTrackViews mutation should look like now:
type Mutation {"Increment the number of views for a track when its card is clicked"incrementTrackViews(id: ID!): IncrementTrackViewsResponse!}
Code review
Practice
IncrementTrackViewsResponse), why is the modified object's return type nullable (track: Track)?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!)
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.
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.