如何从`gql`对象中获取查询名称? [英] How to get the name of a query from a `gql` object?

查看:0
本文介绍了如何从`gql`对象中获取查询名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用graphql-tag中的gql。 假设我有一个gql对象定义如下:

const QUERY_ACCOUNT_INFO = gql`
  query AccountInfo {
    viewer {
      lastname
      firstname
      email
      phone
      id
    }
  }
`
必须有一种方法可以从中获取AccountInfo。我该怎么做?

推荐答案

如果您使用的是Apollo,那么还有一个显式的getOperationName,它似乎相当没有文档,但在我的所有用例中都有效。

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"

这篇关于如何从`gql`对象中获取查询名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆