🎁

12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
15 : 06 : 06 : 56
Join egghead, unlock knowledge.

Want more egghead? It's 58% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 40% Off
1×
Become a member
to unlock all features
Autoplay

    Create Operation Names for GraphQL Queries

    Eve PorcelloEve Porcello
    graphqlGraphQL

    If there are multiple GraphQL queries in the same query document, you need to give the query an operation name. In this lesson, we’ll show how to get around anonymous operation errors with operation names.

    To follow along with these queries, go to the Pet Library GraphQL Playground.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Right now, on the left-hand side of our screen, we have a huge query. It's collecting a bunch of data about customers and then about pets. GraphQL will let us do this. We can grab information about multiple types in one query, but I want to break this down into two separate queries, one of which will be for pets. The other will be for customers.

    As soon as I break this down into two separate queries, we're going to run into an issue. When I click play, there are two unnamed queries. If I click the second one of these, it says, "This anonymous operation must be the only defined operation." If there's more than one query in your query document, you're going to need to give both of these a name.

    Right now, these are anonymous queries. Think of those like anonymous functions. We need to give them a name. I'll call the first one, "Pet Page." I'll call the second one, "Customer Page." Now if I select the play button, we'll see the drop-down. We'll also see the operation name, so I can execute these queries one at a time.

    Just to recap, whenever you have more than one query inside of a query document, you need to give it an operation name. The operation name can be whatever you want to call it but conventionally is capitalized.