-
-
Notifications
You must be signed in to change notification settings - Fork 678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abstract types with mercurius federation #1359
Comments
I suppose you are using Apollo Federation V2. |
I haven't tried actually. This version looks much efficient than the previous one and doesn't use too much of apollo. I'll have a try next week, thank you |
I had some time to try it this morning and the new code is not working, giving me this error
Maybe this code works only with graphql v16? |
@ramiel V2.0 is on the way. If you really want to use |
Yep. I need to wait for type-graphql that supports v16. I'm waiting for it in any case for other thousand reasons. Ciao |
ok, now that version 2 is (almost) out I tried the new implementation to build a federated schema. This is not working either, because if I have two services and each build its own federated schema, the error is |
Can you share the code? |
I can share part of the code. // buildFederatedSchema.ts
import { GraphQLSchema, specifiedDirectives } from 'graphql';
import gql from 'graphql-tag';
import { buildSubgraphSchema } from '@apollo/subgraph';
import { addResolversToSchema } from '@graphql-tools/schema';
import { IResolvers, printSchemaWithDirectives } from '@graphql-tools/utils';
import {
buildSchema,
BuildSchemaOptions,
createResolversMap,
} from 'type-graphql';
/**
* Given a schema, patches the query, mutation and subscription to work in federation
*/
export const patchSchemaExtensions = (schema: GraphQLSchema): string =>
printSchemaWithDirectives(schema);
// printSchemaWithDirectives(schema)
// .replace('type Query {', 'type Query @extends {')
// .replace('type Mutation {', 'type Mutation @extends {')
// .replace('type Subscription {', 'type Subscription @extends {');
export async function buildFederatedSchema(
options: Omit<BuildSchemaOptions, 'skipCheck'>,
referenceResolvers?: IResolvers,
) {
const schema = await buildSchema({
...options,
directives: [...specifiedDirectives, ...(options.directives || [])],
skipCheck: true,
});
const federatedSchema = buildSubgraphSchema({
typeDefs: gql(printSchemaWithDirectives(schema)),
resolvers: createResolversMap(schema) as any,
});
if (referenceResolvers) {
addResolversToSchema({
schema: federatedSchema,
resolvers: referenceResolvers,
});
}
return { schema: federatedSchema };
} // schema
export const createSchema = async (opt?: { emitSchemaFile?: boolean }) => {
const { schema } = await buildFederatedSchema({
resolvers: [
...
],
container: getContainer,
authChecker: customAuthChecker,
orphanedTypes,
});
if (opt?.emitSchemaFile) {
emitSchemaToFile(schema).catch((e) => {
console.error(e);
});
}
return { schema };
}; // server.ts
app.register(mercurius, {
schema: schema,
federationMetadata: true,
allowBatchedQueries: true,
graphiql: false,
context: contextProviderFn,
persistedQueryProvider: mercurius.persistedQueryDefaults.automatic(),
errorFormatter: errorFormatter(logger),
}); When I load this from the gateway I get the error The generated schema is the following (partial) directive @key(
fields: _FieldSet!
resolvable: Boolean = true
) on OBJECT | INTERFACE
directive @requires(fields: _FieldSet!) on FIELD_DEFINITION
directive @provides(fields: _FieldSet!) on FIELD_DEFINITION
directive @extends on OBJECT | INTERFACE
directive @external(reason: String) on OBJECT | FIELD_DEFINITION
directive @tag(
name: String!
) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
# Rest of the schema here It looks these directives create the problem. I have another service that do not print the directives in the schema. What can be the problem? |
Why do you need |
I use it in another part of the app that prints the schema to a file, but as you can see it does nothing. Also, I'm not using that file at the moment, I just load the SDL directly from the running service. So you can ignore it |
@ramiel |
So, the code that you see is copy/pasted from that branch. type-graphql is at version 2.0.0-beta1 and graphql at version 16.6 |
@ramiel |
From |
I see. I'm using mercurius gateway as gateway. Mercurius is complaining about the presence of |
I also feel it's not the directive itself, but the schema is somehow wrong (from the perspective of mercurius) |
What's weird is that this schema is produced by mercurius itself, as you can see by my code! |
I've never used Mercurius. |
I just asked @mcollina about this: mercurius-js/mercurius#897 |
Mystery solved, mercurius doesn't support federation v2. Thanks for your help |
Describe the Bug
I'm trying to use type-graphql within the context of a federation done with graphql. I need to create a schema that is compatible with the federation spec and I'm using a custom function to do so. I cannot use any apollo helper, so here my solution (not entirely the real code, just an extract)
So, I use it like this
This works and the gateway is able to recognise all my graphql universe. What is not working are my abstract types. The problem is that graphql looks for
resolveType
function but inside resolver that function is named__resolveType
.My current solution is to generate a schema good for the federation gateway, but use instead the normal schema into local mercurius.
I'm not sure what I can provide more to make this issue clear, so I'm happy to collaborate to sort this out.
Also, I'm probably doing some mistake in creating the schema for the federation, in that case I apologize 😄
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: