Skip to content
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

[plugin-federation] schema should contain @key directives on entities #427

Closed
leeroyrose opened this issue Jun 20, 2022 · 7 comments
Closed

Comments

@leeroyrose
Copy link

leeroyrose commented Jun 20, 2022

Using the example graph created for the unit tests

When a graph is introspected that implements builder.asEntity, the @key directive is missing from the output - as seen in the unit test snapshots test snapshots. The expected output would be similar to this snapshot

Though the graph technically resolves it's entities correctly (due to the _service { sdl } query rendering the @key directive), it's making graph composition using the rover cli fail.

Is this something that would be acceptable to add? If so, I can work on a PR. Thank you!

@hayes
Copy link
Owner

hayes commented Jun 20, 2022

This is an issue with graphql. As you can see in the snapshots when using the printSubGraphSchema method, the directive is defined. GraphQLs printSchema method doesn't support printing directives. The solution is to use another printing method. One option is https://github.com/ardatan/graphql-tools/blob/master/packages/utils/src/print-schema-with-directives.ts

There are some long discussions around this in the graphql repo, but it's currently considered working as intended, which is unfortunate, and an issue that should probably be revised now that there is more active development it.

@hayes
Copy link
Owner

hayes commented Jun 20, 2022

If you want to go down the rabbit hole: graphql/graphql-js#1343 is the current state of this issue.

@hayes
Copy link
Owner

hayes commented Jun 20, 2022

We probably should have better docs around how to set up federation and some of these limitations. I personally don't have much experience with, or use for apollo federation, other than writing this plugin. Would be happy to take any feedback or suggestions (or contributions) you might have for the API or docs.

@leeroyrose
Copy link
Author

leeroyrose commented Jun 23, 2022

Hey, thank you for the response. That is quite the rabbit hole! given it's a GQL specific implementation, we were able to work around it for our use case without relying on the output of an introspection on the pothos graph sdl.

Since doing that, and moving forward with our implementation, we have spotted some other issues in terms of how the _entities query resolves. For context, we have a gateway, with multiple graphs behind it - none of which are built using pothos. We have a new graph which is built in pothos which we want to onboard, but so far is proving tricky.

Lets say we have a search graph, which has only an id, we would want to resolve the entity from our new pothos product graph. We defined the asEntity function and implemented the resolveReference handler - but it never seems to be called. When we run the query that the gateway would run to resolve the entity, directly against the subgraph, we get nothing back. I've attached a sample app. If you run yarn && yarn dev and perform the request:

http://localhost:3001/

query {
  _entities(representations: [{ __typename: "Product", id: "1" }]) {
    ... on Product {
      id
      name
      price
      weight
    }
  }
  byId(id: "1") {
    id
    name
    price
    weight
  }
  all {
    id
    name
    price
    weight
  }
}

The _entities query is resolving the fields as null and the resolveReference handler is not called. Results:

{
  "data": {
    "_entities": [
      {
        "id": "1",
        "name": null,
        "price": null,
        "weight": null
      }
    ],
    "byId": {
      "id": "1",
      "name": "Table",
      "price": 899,
      "weight": 100
    },
    "all": [
      {
        "id": "1",
        "name": "Table",
        "price": 899,
        "weight": 100
      },
      {
        "id": "2",
        "name": "Couch",
        "price": 1299,
        "weight": 1000
      },
      {
        "id": "3",
        "name": "Chair",
        "price": 54,
        "weight": 50
      }
    ]
  }
}

I can't tell if I have missed something, an implementation detail, or if there is a bug going in the plugin. I would most certainly expect the _entities query to resolve correctly and the resolveReference handler to be called for it to be a fully functioning subgraph. Any help on this would be greatly appreciated!

federation-experiment-pothos.zip

@hayes
Copy link
Owner

hayes commented Jun 23, 2022

Interesting. This is the second report I've gotten today about entities not resolving correctly. I'll take a look tonight and see if I can figure out what's going on.

@hayes
Copy link
Owner

hayes commented Jun 24, 2022

@leeroyrose Just released an update that I think might fix your issue. There were some big breaking changes in a patch release of @apollo/subgraph (they moved where the resolveReference was loaded from) that broke things if you were on the newer version.

@leeroyrose
Copy link
Author

That is awesome. I have tested and can confirm the entities query is now resolving and the resolveReference handler is being called. Thank you for your efforts on this! 👏🌟

@hayes hayes closed this as completed Jun 24, 2022
saevarb added a commit to saevarb/pothos that referenced this issue Jul 18, 2022
I've been stumbling around with apollo yelling at me trying to get federation to work, and it eventually lead me to issue hayes#427. Switching to `printSubgraphSchema` fixed the problem, but I did not try your other suggested alternative.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants