MrSquaare
MrSquaare

Reputation: 63

Error from a non-sharable field defined in a subgraph that shoudn't have this field

I have multiple GraphQL microservices (subgraphs, using ApolloFederationDriver) with a GraphQL gateway (using ApolloGatewayDriver)

I have a really strange bug since I've upgraded my GraphQL microservices and gateway to use @apollo/server (@nestjs/graphql@11 + @nestjs/apollo@11) : I have an error when my subgraphs are composed telling me that subgraph A has a non shareable field from an object type subgraph B and so it is not happy with. I would ok with this error, the problem is that subgraph A doesn't have the mentionned entity from the subgraph B and I don't know why I got this error.

For information, I have a monorepo, which contains a package where all my object types are defined and exported I know this is quite strange, let me know if you have any idea where this error could come from.

Thanks :)

EDIT 1: So it seems that if one of my ObjectType has a @Field(() => AnotherObjectType, AnotherObjectType will be included in every subgraph schemas. Is this behavior expected?

EDIT 2: Here is an example based on a NestJS sample: https://github.com/MrSquaare/31-graphql-federation-code-first. I got errors because posts-application uses non-sharable fields from User model (which is normal) But I also got errors because somethings-application uses non-sharable fields from User model, which is not normal since somethings-application doesn't use the User model

Upvotes: 1

Views: 2010

Answers (2)

TDK
TDK

Reputation: 91

Make your User type "shareable":

import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType()
@Directive('@key(fields: "id")')
@Directive('@shareable') //this is missing in your implementation
export class User {
    @Field((type) => ID)
    id: number;

    @Field((type) => String)
    name: string;
}

For more information: https://www.apollographql.com/docs/federation/federated-types/sharing-types/#using-shareable

Upvotes: 0

Abbas Tolgay Yılmaz
Abbas Tolgay Yılmaz

Reputation: 93

I recently came across with this issue and was I was able to solve this by adding

@Directive('@key(fields: "id")')

on top of those mentioned entities.

In my case I was using @nestjs/graphql with code-first approach.

Upvotes: 0

Related Questions