0

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
J Singh
  • 59
  • 11

2 Answers2

1

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

Scott Nedderman
  • 547
  • 5
  • 4
  • Thank you. also `buildFederatedSchema` for newer Apollo versions. – mhashim6 Mar 24 '23 at 15:55
  • 1
    Actually, `buildFederatedSchema` is the old name and has been deprecated. See the comment in Apollo's docs [here](https://www.apollographql.com/docs/apollo-server/using-federation/api/apollo-subgraph/#buildsubgraphschema). – Scott Nedderman Apr 17 '23 at 04:17
  • Oops thank you, I think I hadn't enough sleep for a while : ) – mhashim6 Apr 22 '23 at 00:09
0

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?

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '22 at 15:26
  • You’re correct that following the quick start guide works fine and you can spin up a federation 2 server, however I should have added this to the question that I am trying to use the new subgraph directives which is where this @link comes from https://www.apollographql.com/docs/federation/v2/subgraphs/ though I’m not sure if link was introduced in this update or not. – J Singh Apr 02 '22 at 17:08