graphqlapollo-servergraphql-tools

Apollo Server: how to post-process all `id: ID` fields without directives?


Our schema looks like this:

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

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

The backend returns auto-increment IDs, we want to prefix them by type and currently, we're using directives for that:

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

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

Is there a way to avoid adding @uniqueID everywhere and just depend on the ID type? In other words, is it possible to write a schema visitor against our original schema?


Solution

  • Yes it's possible, via mapSchema. It has been answered here: https://github.com/ardatan/graphql-tools/discussions/2126.