03 / 05

What is operation type and operation name why do we use them?

Difficulty: 5/10
operation type, operation name, schema conventions

The operation type is either query, mutation, or subscription and describes what type of operation you intend to do. This keyword is required unless you’re using the shorthand syntax for queries (it is always required for mutations and subscriptions).

Operation Name:
  1. 1

    If you wish to provide a name for your operation, then you must specify the operation type as well.

  2. 2

    The operation name is an explicit name that you assign to your operation; you should pick a meaningful name.

  3. 3

    It is required when sending multiple operations in one document, but even if you’re only sending one operation it’s encouraged because operation names are helpful for debugging and server-side logging.

  4. 4

    Think of this just like a function name in your favorite programming language. in JavaScript, we can easily work only with anonymous functions, but when we give a function a name, it’s easier to track it down, debug our code, and log when it’s called.

Operation Type and Name Examples
Example query from client:
Why Use Operation Names
  1. 1

    Debugging and Logging: Named operations appear in server logs and Apollo Studio, making it easy to identify which query caused an error. Anonymous operations appear as "anonymous" or have generated IDs, making debugging harder.

  2. 2

    Persisted Queries: Operation names are used as keys in persisted query stores. A document with a name like GetUserProfile can be stored and referenced by its hash, reducing request size and improving security.

  3. 3

    Client-Side Caching: Apollo Client and similar libraries use operation names to identify and normalize cached data. Named operations help with type-safe cache operations.

  4. 4

    Performance Monitoring: Tools like Apollo Studio track performance metrics per operation name, enabling identification of slow queries.

  5. 5

    Document Organization: Multiple operations can be defined in the same document. Operation names distinguish between them and prevent conflicts.

  6. 6

    Code Generation: Tools like GraphQL Code Generator use operation names to generate type-safe hooks and components (e.g., useGetUserQuery from operation name GetUser).

Multiple Operations in One Document

Operation names should be meaningful and follow consistent naming patterns. For queries, use nouns or compound nouns describing the data being fetched (e.g., GetUser, ListProducts, SearchPosts). For mutations, use verbs describing the action (e.g., CreateUser, UpdateProfile, DeleteComment). For subscriptions, use past-tense event names (e.g., UserCreated, MessageAdded, StatusChanged). This naming pattern improves readability and aligns with GraphQL best practices.

Operation Type Shortcut Syntax
  1. 1

    Anonymous Query: When the operation type is query and there's no operation name, the query keyword can be omitted entirely. This is the only case where the operation type can be omitted.

  2. 2

    Anonymous Query Example: { user(id: "123") { name } } (valid and common for simple queries)

  3. 3

    No Shortcut for Mutations/Subscriptions: Mutations and subscriptions always require the operation type keyword, even without an operation name.

Scenario Questions

0-2 years experience

  1. 1You need to add a new query to fetch a list of books. How would you choose the operation type and name for this GraphQL request?
  2. 2If a client sends a mutation called `updateUser` but you defined it as a query in the schema, what will happen and how would you fix it?

2-5 years experience

  1. 1During a sprint we noticed that a frontend team is calling a GraphQL operation named `createOrder` but the backend schema defines it as `addOrder`. How would you troubleshoot and resolve the mismatch?
  2. 2We want to expose both a `search` query and a `search` mutation for different use cases. What considerations guide your decision on operation types and naming to avoid confusion?

5-8 years experience

  1. 1Our platform supports dozens of microservices each exposing their own GraphQL schemas that are stitched together. How do you enforce consistent operation naming and type conventions across services to keep the gateway stable?
  2. 2A performance regression appeared after we introduced a new `deleteUser` mutation that shares the same name as an existing `deleteUser` query in a federated schema. How would you diagnose and prevent such collisions at scale?

8+ years experience

  1. 1We are planning to migrate a legacy REST API to GraphQL across multiple product lines. How would you design a naming strategy for operation types and names that supports backward compatibility, versioning, and cross‑team ownership?
  2. 2Our organization wants to generate client SDKs automatically from the GraphQL schema. What impact do operation naming conventions have on SDK ergonomics and long‑term maintenance, and how would you govern them?

Follow-up Questions

  • How does the operation name affect client caching?
  • What are the risks of reusing the same name for different operation types?
  • Can you change an operation’s type without breaking existing clients?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.