printSchema with directives for uploading to Apollo Studio managed federation

I’m working on a federated subgraph and trying to convert a GraphQLSchema into a schema.graphql for uploading to Apollo Studio with rover (using apollo managed federation). I’ve tried using printSchema or printSubgraphSchema, but both strip any directives like @key() and also any extends. This causes issues when trying to extend an entity from another subgraph.

I need to print from a GraphQLSchema because I’m using a code-first schema that is split across many files.

Is there a way to print the schema and include the directives or is there another approach to uploading my schema to Apollo Studio with the directives included?

Thanks fo any help in this!

I’m doing something like this:

const unfederatedSchema = makeSchema(...);
const schema = transformSchemaFederation(unfederatedSchema, ...)

const sdl = printSubgraphSchema(schema);
const path = resolve(__dirname, './generated/schema.graphql');
writeFileSync(path, sdl);

I’m expecting this:

extend type Space @key(field: 'id') {
  id: ID! @external

What I actually get:

type Space {
  id: ID!

Hey @Curbol, printSubgraphSchema will print all of those correctly, provided the metadata is captured on the schema object correctly.

I’m not sure what transformSchemaFederation does here, but I can see that buildSubgraphSchema is missing from this equation (which captures the @key and other federation directives correctly on the schema object for printing). It’s likely that your code-first approach isn’t going to play nice with this though - buildSubgraphSchema currently only supports modules (SDL + resolvers). If you can programatically get the correct subgraph SDL somehow you should be able to make this work.

The buildSubgraphSchema fn is exported by the @apollo/subgraph package.

1 Like