带有Apollo的Graphql-重构查询,返回的对象正在重复(差异查询中的相同字段)

时间:2020-07-10 05:29:21

标签: graphql apollo

我有几个查询的返回类型相同:

// Query 1
gql`
        mutation insert_shops_users($shopId: uuid) {
          insert_shops_users(objects: [{ shopId: $shopId }]) {
            affected_rows
            returning {
              Shop {
                id
                users {
                  userId
                }
              }
            }
          }
        }
      `,
// Query 2
gql`
        mutation delete_shops_users_by_pk($shopId: uuid!, $userId: String!) {
          delete_shops_users_by_pk(shopId: $shopId, userId: $userId) {
            Shop {
              id
              users {
                userId
              }
            }
          }
        }
      `,

现在,我想提取该部分,例如以名称ShopUserResult并将其用于两个查询中:

在ShopUserResult下提取

Shop {
  id
  users {
    userId
  }
}

结果查询

// Query 1 - after refactor
gql`
        mutation insert_shops_users($shopId: uuid) {
          insert_shops_users(objects: [{ shopId: $shopId }]) {
            affected_rows
            returning {
             ShopUserResult
            }
          }
        }
      `,
// Query 2 - after refactor
gql`
        mutation delete_shops_users_by_pk($shopId: uuid!, $userId: String!) {
          delete_shops_users_by_pk(shopId: $shopId, userId: $userId) {
            ShopUserResult
          }
        }
      `,

我是graphql的新手,非常感谢您提供有关重构的任何建议。

1 个答案:

答案 0 :(得分:0)

可以使用片段(source 1source 2)重构重复的字段集:

查询-之前

C:\Users\...\SendMails.vbs(3, 1) Microsoft Excel: Microsoft Excel cannot access the file 
'C:\Users\...\Macro.xlsm'. There are several possible reasons:

 The file name or path does not exist.
 The file is being used by another program.
 The workbook you are trying to save has the same name as a currently open workbook.

Finished: SUCCESS


查询-使用片段后

分段
gql`
  mutation insert_shops_users($shopId: uuid) {
    insert_shops_users(objects: [{ shopId: $shopId }]) {
      affected_rows
      returning {
        Shop {
          id
          users {
            userId
          }
        }
      }
    }
  }
      `,
询问
const ShopWithUsers = gql`
  fragment ShopWithUsers on shops {
    id
    users {
      userId
    }
  }
`