Error: Unknown directive "@link" error: Voyage 1 - Building out the SubGraphs

Hello!

I’m following the lesson here to learn more about Federation and Entities, and I’m sort of stuck on the step where the instructions ask you to add this to the schema files

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

When I add this line to the reviews.graphql and locations.graphql files both sub graph servers crash with the same error

Error: Unknown directive "@link".
    at assertValidSDL (/Users/jvajda/Documents/Github/apollo-learning/odyssey-voyage-I/subgraph-reviews/node_modules/graphql/validation/validate.js:135:11)
    at buildASTSchema (/Users/jvajda/Documents/Github/apollo-learning/odyssey-voyage-I/subgraph-reviews/node_modules/graphql/utilities/buildASTSchema.js:44:34)
    at makeExecutableSchema (/Users/jvajda/Documents/Github/apollo-learning/odyssey-voyage-I/subgraph-reviews/node_modules/@graphql-tools/schema/cjs/makeExecutableSchema.js:73:47)
    at Function.constructSchema (/Users/jvajda/Documents/Github/apollo-learning/odyssey-voyage-I/subgraph-reviews/node_modules/@apollo/server/dist/cjs/ApolloServer.js:315:50)
    at new ApolloServer (/Users/jvajda/Documents/Github/apollo-learning/odyssey-voyage-I/subgraph-reviews/node_modules/@apollo/server/dist/cjs/ApolloServer.js:89:49)
    at startApolloServer (/Users/jvajda/Documents/Github/apollo-learning/odyssey-voyage-I/subgraph-reviews/index.js:12:18)
    at Object.<anonymous> (/Users/jvajda/Documents/Github/apollo-learning/odyssey-voyage-I/subgraph-reviews/index.js:35:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)

I actually figured this out, but will leave this message for others, until you do this step correctly:

“where we initialized the ApolloServer, we’re now going to pass the existing typeDefs and resolvers properties as an object into the buildSubgraphSchema function. Then we’ll use the result to set a new ApolloServer configuration property called schema

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

This error will occur. :grinning:

Thanks for bringing this up @jpvajda ! We briefly point out not to worry about the errors that pop up, it’s in the video at 3:45 and in the written content, but it’s definitely easy to miss!

Hello @MichelleMabuyo @jpvajda , please this error is still persisting in my code.

throw new error_1.GraphQLSchemaValidationError(errors);
              ^

GraphQLSchemaValidationError: Unknown directive "@link".

Unknown directive "@link".
    at buildSchemaFromSDL (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\@apollo\federation\node_modules\@apollo\subgraph\dist\schema-helper\buildSchemaFromSDL.js:145:15)
    at buildSubgraphSchema (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\@apollo\federation\node_modules\@apollo\subgraph\dist\buildSubgraphSchema.js:26:57)
    at file:///C:/Users/EmmanuelEshun/Documents/Arms%20Enterprise/resource-management-service/dist/server.js:20:13
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  errors: [
    GraphQLError: Unknown directive "@link".
        at Object.Directive (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\graphql\validation\rules\KnownDirectivesRule.js:56:11)
        at Object.enter (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\graphql\language\visitor.js:301:32)
        at visit (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\graphql\language\visitor.js:197:21)
        at validateSDL (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\graphql\validation\validate.js:121:22)
        at buildSchemaFromSDL (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\@apollo\federation\node_modules\@apollo\subgraph\dist\schema-helper\buildSchemaFromSDL.js:143:47)
        at buildSubgraphSchema (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\@apollo\federation\node_modules\@apollo\subgraph\dist\buildSubgraphSchema.js:26:57)
        at file:///C:/Users/EmmanuelEshun/Documents/Arms%20Enterprise/resource-management-service/dist/server.js:20:13
        at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
      path: undefined,
      locations: undefined,
      extensions: [Object: null prototype] {}
    },
    GraphQLError: Unknown directive "@link".
        at Object.Directive (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\graphql\validation\rules\KnownDirectivesRule.js:56:11)
        at Object.enter (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\graphql\language\visitor.js:301:32)
        at visit (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\graphql\language\visitor.js:197:21)
        at validateSDL (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\graphql\validation\validate.js:121:22)
        at buildSchemaFromSDL (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\@apollo\federation\node_modules\@apollo\subgraph\dist\schema-helper\buildSchemaFromSDL.js:143:47)
        at buildSubgraphSchema (C:\Users\EmmanuelEshun\Documents\Arms Enterprise\resource-management-service\node_modules\@apollo\federation\node_modules\@apollo\subgraph\dist\buildSubgraphSchema.js:26:57)
        at file:///C:/Users/EmmanuelEshun/Documents/Arms%20Enterprise/resource-management-service/dist/server.js:20:13
        at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
      path: undefined,
      locations: undefined,
      extensions: [Object: null prototype] {}
    }
  ]
}

This is my server function

const server = new ApolloServer<Context>({
  schema: buildSubgraphSchema({
    typeDefs,
    resolvers,
  }),
  plugins: [ApolloServerPluginUsageReportingDisabled()],
});
await server.start();

Hi @eshunemma !

It looks like your schema needs this snippet pasted at the top:

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

This is in the “Federation 2 definition” section of the lesson. Hope that helps !

@MichelleMabuyo This should probably be added to the Moving to Apollo Federation 2 docs The only place I saw mention of the change to how ApolloServer is called under Apollo Federation 2 was in this thread.

Hey @hansoksendahl - are you referring to the step where we should add the extend schema snippet?

If so, it is covered in the docs article you linked, under the “Opt in to Federation 2” section: Moving to Apollo Federation 2 - Apollo GraphQL Docs

Let me know if you meant something different though!