Subgraph doesn't work
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
I experienced the same error going from 2.2.3 -> 2.3.0 using pothos as my schema builder.
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.
with 2.2.3 works perfectly?
Same problem here with 2.3.0 and 2.3.1. No issue with 2.2.3
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).
With 2.2.3 works perfectly.
Still in issue in 2.9.3. After downgrading to 2.2.3 it worked.
Any update on when we can expect a fix?
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),
UPDATE:
If I print the schema myself (i.e. in a schema transformer under
transformSchema), it looks as expected.printSubgraphSchema(lexicographicSortSchema(schema)),Shouldn't
autoSchemaFiledo 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.