federation icon indicating copy to clipboard operation
federation copied to clipboard

Subgraph doesn't work

Open Brilhante29 opened this issue 2 years ago • 9 comments

When I run my project with subgraphs in the latest version of node and nestjs, I got this error

super( ^ GraphQLError: The schema is not a valid GraphQL schema.. Caused by: Invalid definition for directive "@tag": "@tag" should have locations FIELD_DEFINITION, OBJECT, INTERFACE, UNION, ARGUMENT_DEFINITION, SCALAR, ENUM, ENUM_VALUE, INPUT_OBJECT, INPUT_FIELD_DEFINITION, but found (non-subset) FIELD_DEFINITION, OBJECT, INTERFACE, UNION, ARGUMENT_DEFINITION, SCALAR, ENUM, ENUM_VALUE, INPUT_OBJECT, INPUT_FIELD_DEFINITION, SCHEMA

Brilhante29 avatar Feb 06 '23 04:02 Brilhante29

I experienced the same error going from 2.2.3 -> 2.3.0 using pothos as my schema builder.

TLadd avatar Feb 06 '23 20:02 TLadd

I also just ran into this same issue when going from 2.0.5 to 2.3.0. In particular, this happens when calling builder.toSubgraphSchema. The primary motivation for this upgrade was getting support for node 18, but it seems e.g. 2.2.3 is fine for my use case for now.

saevarb avatar Feb 06 '23 20:02 saevarb

with 2.2.3 works perfectly?

Brilhante29 avatar Feb 06 '23 21:02 Brilhante29

Same problem here with 2.3.0 and 2.3.1. No issue with 2.2.3

alexbchr avatar Feb 08 '23 16:02 alexbchr

From what we can tell, this error occurs for subgraphs when they're using the @tag directive definition from federation spec version 2.3, i.e.

directive @tag(name: String!) repeatable on
  | FIELD_DEFINITION
  | INTERFACE
  | OBJECT
  | UNION
  | ARGUMENT_DEFINITION
  | SCALAR
  | ENUM
  | ENUM_VALUE
  | INPUT_OBJECT
  | INPUT_FIELD_DEFINITION 
  | SCHEMA

but if their subgraph schema is declaring a lower federation spec, e.g.

extend schema @link(url: "https://specs.apollo.dev/federation/v2.2", ...)

You'll need to either upgrade your subgraph to use federation spec v2.3 e.g. with

extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", ...)

or you'll need to use the @tag directive definition of your declared federation spec version (or alternatively omit the @tag directive definition if possible, as buildSubgraphSchema() should automatically add the appropriate definition).

sachindshinde avatar Feb 08 '23 19:02 sachindshinde

With 2.2.3 works perfectly.

zhumeisongsong avatar Mar 23 '23 09:03 zhumeisongsong

Still in issue in 2.9.3. After downgrading to 2.2.3 it worked.

Any update on when we can expect a fix?

marc-wilson avatar Oct 11 '24 21:10 marc-wilson

Still having this issue, even when using @apollo/subgraph at version 2.2.3, when using with nestjs.

relevant package versions:

"@apollo/server": "4.11.3",
"@apollo/subgraph": "2.2.3",
"@nestjs/apollo": "12.1.0",
"@nestjs/graphql": "12.1.1"

--> also tried with "@apollo/subgraph": "2.9.3", which should be the latest version at the moment, no success

graphql config in nestjs module:

driver: ApolloFederationDriver,
resolvers: gqlResolvers,
typeDefs: gqlTypeDefs,
sortSchema: true,
autoSchemaFile: {
  path: join(process.cwd(), 'src/schema.gql'),
  federation: {
    version: 2,
    importUrl: 'https://specs.apollo.dev/federation/v2.3',
  },
},
plugins: apolloPlugins,

I've also tried with the more concise variant of autoSchemaFile, just to see if that might be an issue, but it didn't help:

autoSchemaFile: {
  path: join(process.cwd(), 'src/schema.gql'),
  federation: 2
}

UPDATE:

If I print the schema myself (i.e. in a schema transformer under transformSchema), it looks as expected.

    printSubgraphSchema(lexicographicSortSchema(schema)),

Shouldn't autoSchemaFile do exactly this as well?

As a workaround I solved it with this schema transformer approach:

// the transformer
const GQL_SUBGRAPH_SCHEMA_PATH = join(process.cwd(), 'src/subgraph-schema.gql');

const gqlSubgraphSchemaPrinter = (schema: GraphQLSchema): GraphQLSchema => {
  const subgraphSchema = printSubgraphSchema(lexicographicSortSchema(schema));
  // write the schema to the file path in GQL_SCHEMA_PATH
  writeFileSync(GQL_SUBGRAPH_SCHEMA_PATH, subgraphSchema);
  return schema;
};

// in the GraphQL Module config:
transformSchema: (schema) => gqlSubgraphSchemaPrinter(schema),

platzhersh avatar Jan 20 '25 14:01 platzhersh

UPDATE:

If I print the schema myself (i.e. in a schema transformer under transformSchema), it looks as expected.

printSubgraphSchema(lexicographicSortSchema(schema)),

Shouldn't autoSchemaFile do exactly this as well?

As a workaround I solved it with this schema transformer approach:

// the transformer const GQL_SUBGRAPH_SCHEMA_PATH = join(process.cwd(), 'src/subgraph-schema.gql');

const gqlSubgraphSchemaPrinter = (schema: GraphQLSchema): GraphQLSchema => { const subgraphSchema = printSubgraphSchema(lexicographicSortSchema(schema)); // write the schema to the file path in GQL_SCHEMA_PATH writeFileSync(GQL_SUBGRAPH_SCHEMA_PATH, subgraphSchema); return schema; };

// in the GraphQL Module config: transformSchema: (schema) => gqlSubgraphSchemaPrinter(schema),

Seems to still be present in: "@nestjs/apollo": "13.2.1" "@apollo/server": "5.0.0", "@apollo/subgraph": "2.11.3",

The workaround above fixed it for me, meaning it is now writing @inacessible to queries having this directive. Meaning

    @Directive('@inaccessible')

is correctly written to the GQL schema file, which previously was not the case.

I did not get the exception though (as OP), but just a printed gql file, which does not contain the expected directives.

PlayAnyData avatar Oct 24 '25 11:10 PlayAnyData