Apollo Server:如何在不使用指令的情况下对所有“ id:ID”字段进行后处理?

问题描述

我们的模式如下:

type Product {
  id: ID!
  name: String!
}

type User {
  id: ID!
  firstName: String!
}

后端返回自动递增的ID,我们想按类型给它们添加前缀,当前,我们正在使用伪指令:

type Product {
  id: ID! @uniqueID
  name: String!
}

type User {
  id: ID! @uniqueID
  firstName: String!
}
class UniqueIdDirective extends SchemaDirectiveVisitor {
  visitFieldDeFinition(...) {
    ...
  }
}

有没有一种方法可以避免仅在@uniqueID类型上添加ID?换句话说,是否可以针对我们的原始架构编写架构访问者?

解决方法

是的,可以通过mapSchema。在这里已经得到答案:https://github.com/ardatan/graphql-tools/discussions/2126