GraphQLError: Unknown directive "@link"

3.2k Views Asked by At

Trying to test the new Apollo Federation 2 and followed the documentation as specified.

To create a 'true' federation 2 subgraph, the docs ask to put the following in the typedef:

extend schema
  @link(
    url: "https://specs.apollo.dev/federation/v2.0"
    import: ["@key", "@shareable"]
  )

Unfortunately, this addition didn't work for me as I got the following error:

GraphQLSchemaValidationError: Unknown directive "@link".
GraphQLError: Unknown directive "@link".
Library versions:
@apollo/subgraph   0.4.0
apollo-server      3.6.7
graphql            16.3.0
2

There are 2 best solutions below

2
On

Could you link the Apollo Federation documentation which made indicated that you should be using the @link directive?

I also set up an Apollo Federation 2 server - but followed along this tutorial: https://www.apollographql.com/docs/federation/v2/quickstart/setup . I did not come across a @link directive, but I might have just followed a different tutorial. Just adding a reference to it here incase it helps you.

I also think that @link might not be an already implemented directive due to this: https://github.com/graphql/graphiql/issues/892 . Did you want to add in a custom directive?

4
On

If you're getting this error, it's probably because you extended your schema (as required for a subgraph), but are not building it as a subgraph schema at startup. To fix it, initialize your ApolloServer object like this:

const server = new ApolloServer({
  schema: buildSubgraphSchema({ typeDefs, resolvers }),
})

Ref: Generate the subgraph schema