Skip to content

feat: conditional relationships evaluate readable/writable guards per-request - #450

Merged
jkeen merged 7 commits into
graphiti-api:mainfrom
timkomip:fix-conditional-realtionships
Jul 29, 2026
Merged

jkeen merged 7 commits into
graphiti-api:mainfrom
timkomip:fix-conditional-realtionships

Conversation

@timkomip

@timkomip timkomip commented Apr 7, 2023 •

Copy link
Copy Markdown
Contributor

History

Setting the readable option on a relationship to a callables (procs, lambdas, etc) would only evaulate on load (once). Conditionally hiding relationships per request was not supported. Feature was disabled

Current State (before PR)

readable option on relationships (has_many, belongs_to) doesn't support callables (procs, lambdas, etc).

This PR

Adds support for setting readable with callables.

Changes

  • Sideload evaluates readable flag every time so that the read conditions can be check against current request (or context)
  • Move readable check to serializer. Sets :if option on JSONAPI::Serializable::Relationship object with lambda that calls sideload.readable?
  • Apply sideload/relationship without checking readable? on sideload since the check will be happening in the serializer.
  • Filters out the include from the include_hash if the current included relationships is not readable.

Update (July 2026)

The branch has grown since the original description:

  • Both flags, all guard types. readable:/writable: accept symbols, strings, and procs, evaluated per-request. Unreadable relationships are omitted from responses and scrubbed from ?include=; unwritable ones reject sideposting with unwritable_relationship.
  • Guard resolution. The declaring resource wins; if it doesn't define the method but the related resource does, that one is used. Private methods are eligible.
  • Guards take no arguments by design — they're evaluated when scrubbing ?include=, before any records exist, so they can only depend on request context.

⚠️ Behavior change: these options were previously accepted but silently treated as true. Upgrading enforces them, so formerly-succeeding reads/writes may be denied. Upgraders are covered three ways: the schema check reports relationship :x became guarded, a gem post-install message explains the change, and bin/rails runner 'puts Graphiti.guarded_relationships' lists every affected relationship.

Comment thread spec/serialization_spec.rb Outdated
end

context "when a sideload macro not readable" do
xcontext "when a sideload macro not readable" do

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timkomip why are these skipped now?

@jkeen jkeen added the question label Feb 27, 2024
@jkeen jkeen removed the question label Jul 28, 2026
@jkeen jkeen changed the title Add support for conditional relationships feat: add support for conditional relationships Jul 28, 2026
jkeen added a commit that referenced this pull request Jul 29, 2026
…ditional relationships) (#450)

Symbols, strings, and procs passed to a relationship's readable/writable options are now evaluated per-request instead of being silently truthy. Unreadable relationships are omitted from serialization and scrubbed from includes; unwritable ones reject sideposting with unwritable_relationship. The declaring resource wins when the guard exists on both sides.

Because these guards were previously ignored, upgrading apps get three signals: guarded relationships are marked in schema.json and SchemaDiff reports them as 'became guarded', a post_install_message warns on bundle update, and Graphiti.guarded_relationships lists every affected relationship for auditing.

Also fixes serializer relationship_condition_blocks leaking across the serializer hierarchy via shared class_attribute state, and a spec-suite Graphiti.logger double leak.
timkomip and others added 7 commits July 28, 2026 21:11
Moves readable/writable check to serializer so that the check happens
before each render vs once on load.
Relationship readable/writable guards set with a symbol or proc just read as true before this change. Enforcing them changes behavior for any app that declared one, so two upgrade signals:

- schema.json now marks guarded relationships, and SchemaDiff reports "became guarded" as backwards-incompatible, naming each one in CI.
- A gemspec post_install_message points everyone else at the new Graphiti.guarded_relationships audit helper at bundle update.
@jkeen
jkeen force-pushed the fix-conditional-realtionships branch from 306259d to d409463 Compare July 29, 2026 02:41
@jkeen jkeen changed the title feat: add support for conditional relationships feat: conditional relationships - evaluate readable/writable guards per-request Jul 29, 2026
@jkeen jkeen changed the title feat: conditional relationships - evaluate readable/writable guards per-request feat: conditional relationships evaluate readable/writable guards per-request Jul 29, 2026
@jkeen
jkeen merged commit 3ed3187 into graphiti-api:main Jul 29, 2026
51 checks passed
github-actions Bot pushed a commit that referenced this pull request Jul 29, 2026
# [1.12.0](v1.11.1...v1.12.0) (2026-07-29)

### Bug Fixes

* GQL name chaining ([#415](#415)) [skip ci] ([930928a](930928a))

### Features

* conditional relationships evaluate readable/writable guards per-request ([#450](#450)) ([3ed3187](3ed3187))
* pass the model and attribute name to writable guards ([#511](#511)) [skip ci] ([24a8a94](24a8a94))
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

jkeen added a commit to jkeen/graphiti that referenced this pull request Jul 30, 2026
…-request (graphiti-api#450)

Symbols, strings, and procs passed to a relationship's readable/writable options are now evaluated per-request instead of being silently treated as true. Unreadable relationships are omitted from serialization and scrubbed from includes; unwritable ones reject sideposting with an unwritable_relationship error. The resource declaring the relationship wins when the guard is defined on both sides; guards take no arguments because include scrubbing runs before any records exist.

apps already passing non-boolean values to these options gain enforcement on upgrade. Guarded relationships are marked in schema.json (SchemaDiff reports "became guarded"), a post_install_message explains the change on bundle update, and Graphiti.guarded_relationships lists affected relationships.

Also fixes relationship condition blocks mutating a hash shared across the serializer class hierarchy, which let one resource's guard affect same-named relationships on sibling serializers.

Co-authored-by: Jeff Keen <[email protected]>
jkeen pushed a commit to jkeen/graphiti that referenced this pull request Jul 30, 2026
# [1.12.0](graphiti-api/graphiti@v1.11.1...v1.12.0) (2026-07-29)

### Bug Fixes

* GQL name chaining ([graphiti-api#415](graphiti-api#415)) [skip ci] ([930928a](graphiti-api@930928a))

### Features

* conditional relationships evaluate readable/writable guards per-request ([graphiti-api#450](graphiti-api#450)) ([3ed3187](graphiti-api@3ed3187))
* pass the model and attribute name to writable guards ([graphiti-api#511](graphiti-api#511)) [skip ci] ([24a8a94](graphiti-api@24a8a94))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants