微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在一个查询中使用多个片段来自不同类型?

如何解决如何在一个查询中使用多个片段来自不同类型?

我有很多查询在不同类型上使用相同的字段,因此我开始使用片段。而且我似乎无法思考如何在使用多种类型的查询中使用这些片段。

例如,我有一个具有 Flux 类型和 FluxGroup 类型的查询

export const GET_EXPLORER_CATEGORIES_QUERY = gql`
  ${CORE_FLUX_FIELDS}
  query Explorer_categories {
    explorer_categories {
      id
      name
      related {
        __typename
        ... on Flux {
          ...CoreFluxFields
          favorite @client
          isNotifActivated @client
          sources {
            id
            public_name
            image {
              id
              uri
            }
          }
        }
        ... on FluxGroup {
          id
          name
          description
          subscribed
          notification_subscribed
          favorite @client
          isNotifActivated @client
          image {
            id
            uri
          }
          fluxes {
            id
            name
            description
            is_activated
            image {
              id
              uri
            }
          }
        }
      }
    }
  }
`;

在我的 fragments.js 中,我有

import {gql} from '@apollo/client';

export const CORE_FLUX_FIELDS = gql`
  fragment CoreFluxFields on Flux {
    id
    name
    subscribed
    description
    notification_subscribed
    is_activated
    image {
      id
      uri
    }
  }
`;

export const CORE_FLUXGROUP_FIELDS = gql`
  fragment CoreFluxGroupFields on FluxGroup {
    id
    name
    subscribed
    description
    notification_subscribed
    is_activated
    image {
      id
      uri
    }
  }
`;

如您所见,CoreFluxFieldsCoreFluxGroupFields 使用相同的字段,但用于不同类型的 FluxFluxGroup。如何在我的 GET_EXPLORER_CATEGORIES_QUERY 上同时使用它们?如果我在 ${CORE_FLUXGROUP_FIELDS}添加 ${CORE_FLUX_FIELDS} 并在 ...CoreFluxGroupFields 中使用 ...on FluxGroup,它会破坏一切。

有没有办法做到这一点?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。