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

Support for "global object identification" and the Node interface #2507

Open
lervag opened this issue Jan 9, 2025 · 5 comments · May be fixed by #2510
Open

Support for "global object identification" and the Node interface #2507

lervag opened this issue Jan 9, 2025 · 5 comments · May be fixed by #2510

Comments

@lervag
Copy link
Contributor

lervag commented Jan 9, 2025

I want to implement support for global object identification in my service. In particular, I want to implement the Node interface and the Query.node field. The latter is non-trivial; we currently have an implementation that requires us to manually add each type that implements Node to a custom data structure that we use to resolve the ID. So, each type will extend Node[TYPE] and we add the TYPE to a slightly complex structure manually to handle the resolver.

I am curious if anyone has implemented this in a more general manner that is more straight forward to use?

The dream scenario would be to have something where we simply do case class Foo(...) extends Node. Or perhaps case class Foo(...) extends Node[Foo] where everything else required for Query.node is handled automatically.

@paulpdaniels
Copy link
Collaborator

I imagine it could be done similarly to how we do federation. Though I’m not clear on how you determine the subtype, is it encoded in the ID? The spec seems somewhat vague on the point so I’m guessing we can’t assume much about the ID structure, which makes the design somewhat tricky.

@lervag
Copy link
Contributor Author

lervag commented Jan 9, 2025

Though I’m not clear on how you determine the subtype, is it encoded in the ID?

Ah, yes. That's an important detail I forgot to mention! In our code, we have typed all of our IDs. In the schema, they are simply ID, but in our backend code they become e.g. ID[Foo] where we have a structure/mechanism for expressing this typed ID.

@paulpdaniels
Copy link
Collaborator

How do you determine the type of the id though? Is the type prefixed to the id Foo_<id>? or is it like a packed byte representing the type? In federation we always get something in an object format {__typename: "Foo", id: ""} so we can do a lookup for the resolver of that type.

@paulpdaniels paulpdaniels linked a pull request Jan 12, 2025 that will close this issue
@paulpdaniels
Copy link
Collaborator

Took a stab at an implementation here if you could take a look. I've never used the spec myself so I'm not sure what the best DX would be here, so I just followed what we did for federation.

@lervag
Copy link
Contributor Author

lervag commented Jan 14, 2025

Sorry for the late reply!

How do you determine the type of the id though?

Sorry, I should have written about that as well. We've made a structure for this that constructs ID's like this: subgraphname:entitynr:primarykeys where our service defines the subgraphname and we manually assign entity numbers for each entity and the primarykeys are a json representation of the primary key in our database.

Is the type prefixed to the id Foo_<id>? or is it like a packed byte representing the type?

It's prefixed.

Took a stab at an implementation here if you could take a look. I've never used the spec myself so I'm not sure what the best DX would be here, so I just followed what we did for federation.

Thanks! I'll take a look and give you some feedback as soon as possible!

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

Successfully merging a pull request may close this issue.

2 participants