apollographql / apollo-server

🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
https://www.apollographql.com/docs/apollo-server/
MIT License
13.76k stars 2.03k forks source link

Multiple keys in federated service not resolved correctly #2814

Closed ivome closed 5 years ago

ivome commented 5 years ago

I can't get the resolving of types in federated services to work that have multiple keys.

For example, when I have two services:

Service 1:

  extend type Query {
    me: User
  }

  type User @key(fields: "id") @key(fields: "username") {
    id: ID!
    name: String
    username: String
  }

In service 2 I am trying to resolve the user by username:

  type Review @key(fields: "id") {
    id: ID!
    author: User
    text: String!
  }

  extend type User @key(fields: "username") {
    username: String @external
    reviews: [Review]
  }

The gateway tries to query the field id of the User type instead of the username in service 2 which doesn't even exist in the service schema.

However, when I change the order of the key directives on the user type, it works. So it seems something in the gateway doesn't work quite right.

Here is repository with the adjusted demo repo that reproduces the behavior: https://github.com/ivome/federation-demo

jbaxleyiii commented 5 years ago

@ivome thank you for this report! We will take a look!

jbaxleyiii commented 5 years ago

@JacksonKearl did you fix this?

JacksonKearl commented 5 years ago

Not yet, fix seems to be to change

https://github.com/apollographql/apollo-server/blob/9cda6610210b5936bbd927aed63b1ef255fc3143/packages/apollo-gateway/src/buildQueryPlan.ts#L289

to

const keyFields = context.getKeyFields(parentType, parentGroup.serviceName);

But that breaks compound keys in an unexpected way. I think the @.federation.keys dict isn't getting properly initialized for compound keys, or I'm misusing it.