rmosolgo / rmosolgo/graphql-ruby

Passing information between IDs and interface resolution

Open
#1,802 10 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
5.4k
Forks
1.4k
Avg merge
23h 19m
Merged PRs (30d)
28

Description

Imagine you have a single rails model

class Foo < ActiveRecord::Base
end

and two GraphQL types both implementing Node:

type Type1 implements Node { id ... }
type Type2 implements Node { id ... }

For weird implementation reasons, Foo is the backing model for both GraphQL types. Not in that Foo has some sort of attribute that tells you whether it's Type1 or Type2, but in that every Foo backs a Type1 and a Type2 in the API.

In order to implement Node, we give the two types ID schemes, e.g. gid://shopify/Type1/1 and gid://shopify/Type2/1. Note that from the API perspective these IDs refer to different objects, but at the implementation layer they end up loading the same ActiveRecord object.

As it turns out, it's really difficult to make the following GraphQL query work:

{
  node(id: "gid://shopify/Type1/1") {
    id
  }
}

This is because the concrete type gets resolved twice. First, to load the record, we get the GID, parse out the type, convert that to the appropriate underlying model, and load from the database. The resolver for the node field thus returns an instance of Foo which is correct.

However, we then have to resolve the type again. node is a field of type Node, which means we go through the interface type resolution to determine which concrete GraphQL type (Type1 or Type2) is backing this instance of Node. In this second iteration, all we have are the abstract type (Node) and the implementation object (Foo). This is insufficient information to determine the correct concrete type, since we no longer have access to the GID.

Ideally this second resolution wouldn't even need to happen. We've already resolved the type once to load the object, there's no point in trying to resolve it again. At the very least, this second step needs access to the GID so it can determine the correct concrete type.

(We've worked around this issue by always wrapping Foo in a proxy object at load time so that it can itself hold the type it was loaded to serve, but the code for that is super-gross.)

@rmosolgo @theorygeek

cc @felix-d in case I've missed anything from your case

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the node field's GID-based record loading and the later Node interface type resolution. Investigate how the resolved type or GID could be retained between those steps; done means Type1 and Type2 can share Foo while node(id: ...) resolves the requested concrete type without requiring a proxy workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, rails, ruby
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.