8

I use gql from graphql-tag. Let's say I have a gql object defined like this:

const QUERY_ACCOUNT_INFO = gql`
  query AccountInfo {
    viewer {
      lastname
      firstname
      email
      phone
      id
    }
  }
`

There must be a way to get AccountInfo from it. How can I do it?

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100

2 Answers2

13

If you are using Apollo there is also the explicit getOperationName which seems to be rather undocumented but has worked for all my usecases.

import { getOperationName } from "@apollo/client/utilities";

export const AdminListItemsDocument = gql`
  query AdminListItems(
    $first: Int
    $after: String
    $before: String
    $last: Int
  ) {
    items(
      first: $first
      after: $after
      before: $before
      last: $last
    ) {
      nodes {
        id
        name
      }
      totalCount
      pageInfo {
        hasPreviousPage
        hasNextPage
        startCursor
        endCursor
      }
    }
  }
`;

getOperationName(AdminListBlockLanguagesDocument); // => "AdminListItems"
Marcus Riemer
  • 7,244
  • 8
  • 51
  • 76
11

What's returned by gql is a DocumentNode object. A GraphQL document could include multiple definitions, but assuming it only has the one and it's an operation, you can just do:

const operation = doc.definitions[0]
const operationName = operation && operation.name

If we allow there may be fragments, we probably want to do:

const operation = doc.definitions.find((def) => def.kind === 'OperationDefinition')
const operationName = operation && operation.name

Keep in mind it's technically possible for multiple operations to exist in the same document, but if you're running this client-side against your own code that fact may be irrelevant.

The core library also provides a utility function:

const { getOperationAST } = require('graphql')
const operation = getOperationAST(doc)
const operationName = operation && operation.name
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • If I use getOperationAST I get the typescript error: Expected 2 arguments but got 1. – amaster Dec 12 '20 at 00:14
  • 3
    If I use the operation = doc.definition[0] ... way then I get typescript error Property 'name' does not exist on type 'DefinitionNode' – amaster Dec 12 '20 at 00:15
  • Side question: how to do the same with a fragment? The question is different but googling led me here – Eric Burel Dec 09 '21 at 13:17
  • If I have `x = gql' query MyFakeName ($var: var) { realName (var: $var) { someField } } '`, then I get `x.definitions[0].name.value = MyFakeName`. How do I get `realName` ? – Juan Perez Feb 10 '23 at 11:36
  • @Juan Perez `x.definitions[0].selectionSet.selections[0].name.value` -> `realName` – Hải Bùi Aug 21 '23 at 03:02