Skip to content

Backport apollo_entity_ref_field to pre-1.0#612

Merged
myronmarston merged 9 commits intopre-1.0from
myron/expose-apollo-entity-ref-pre-1.0
Jun 19, 2025
Merged

Backport apollo_entity_ref_field to pre-1.0#612
myronmarston merged 9 commits intopre-1.0from
myron/expose-apollo-entity-ref-pre-1.0

Conversation

…dex`.

This can be used, for example, when migrating a field in an index to a
new GraphQL field name. Previously, we were not able to correctly
determine the mapping of the field in this case (since it's graphql-only).
With this fix, we now use the mapping of the indexing field with the same
name. This ensures we treat `text` and `nested` fields correctly, for
example.
For a custom GraphQL resolver it can be useful to define an object
field that has a `name_in_index` of a scalar field. When a client
requests subfields of the object field, we want to request the scalar
field from the datastore. Previously, we requested non-existent
subfields.

For example, given these two fields:

```
t.field "owner_id", "ID", indexing_only: true
t.field "owner", "ComponentOwner", name_in_index: "owner_id", graphql_only: true
```

...when a client requests `owner { id }`, we want to request `owner_id`
from the datastore rather than `owner_id.id` (which does not exist, and
is what ElasticGraph did before this fix).
- Validate that an existing indexing field exists at the provided `name_in_index`
  since we don't define an indexing field for a `graphql_only` field and the
  referenced field must already exist.
- Fix `backing_indexing_field` to correctly handle nested field paths.
  This allows us to support `graphql_only` fields with a nested `name_in_index`
  field path under `highlights`.
- Fix `total_edge_count` so it returns the count of the entire collection,
  rather than just the number of nodes on the current page.
- Fix handling of `before`/`after` args, converting them to the form required
  by the GraphQL gem. Previously, a `NoMethodError` was thrown because
  the GraphQL gem expects `String` arguments but we were providing
  `DecodedCursor` objects.
Instead of an assignment API that looks like this:

```ruby
schema.object_type "MyType" do |t|
  t.default_graphql_resolver = :my_resolver

  t.field "field1", "String"
  t.field "field2", "String" do |f|
    f.resolver = :my_other_resolver
  end
end
```

It now looks like this:

```ruby
schema.object_type "MyType" do |t|
  t.resolve_fields_with :my_resolver

  t.field "field1", "String"
  t.field "field2", "String" do |f|
    f.resolve_with :my_other_resolver
  end
end
```

This is more in keeping with our other schema definition APIs (e.g.
`f.documentation "docstring" instead of `f.documentation = "docstring"`)
and paves the way for these APIs to accept additional arguments.

While this is a breaking change, now is an ideal time to break things,
in the runup to the 1.0.0 release.
This allows us to pair a `name` of a GraphQL resolver with `config`.
We'll be using this in a later PR to allow GraphQL resolvers to be
parameterized at the field level.
Previously, we supported resolver parameters provided when registering
the resolver, but it's quite useful to be able to provide them when
configuring a type or field to use a resolver. This allows resolver
params to be provided in all 3 spots.
…ref.

When plugging into an Apollo supergraph, it's quite useful to be able to
expoes "entity reference" types (e.g. a type with just an id field, that
refers to an entity owned by another subgraph) rather than just exposing
an id field. This allows Apollo to federate the type.

This adds two new APIs to support this:

* `apollo_entity_ref_field`
* `apollo_entity_ref_paginated_collection_field`

The latter exposes a list of ids as a paginated relay connection.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jun 17, 2025

CLA assistant check
All committers have signed the CLA.

@myronmarston myronmarston marked this pull request as ready for review June 19, 2025 01:54
@myronmarston myronmarston merged commit de06ecc into pre-1.0 Jun 19, 2025
22 of 23 checks passed
@myronmarston myronmarston deleted the myron/expose-apollo-entity-ref-pre-1.0 branch June 19, 2025 01:54
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 this pull request may close these issues.

2 participants