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

240 views Asked by At

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?

1

There are 1 answers

0
Borek Bernard On BEST ANSWER

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