Skip to content

resettable schemas, getCurrentSchemaName for TypeResolver, and IsPublic support for InputObject fields - #1241

Open
vlydev wants to merge 8 commits into
overblog:masterfrom
vlydev:master
Open

vlydev wants to merge 8 commits into
overblog:masterfrom
vlydev:master

Conversation

@vlydev

@vlydev vlydev commented Mar 17, 2026 •

Copy link
Copy Markdown
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Documented? yes
License MIT

Summary

This PR adds three independent improvements to the bundle, primarily motivated by long-running process compatibility (e.g. RoadRunner, Swoole, FrankenPHP) and better ACL
control over input types.


  1. Resettable schemas (SchemaBuilder, Executor, AbstractResolver)

What changed:

  • SchemaBuilder and Executor now implement ResetInterface
  • A new resettable: true option can be set per schema in config
  • ExtensibleSchema gained isResettable()/setIsResettable() methods
  • AbstractResolver now properly separates factories ($solutionsFactory) from built instances ($solutions) and implements reset() — clearing only the built instances, not the
    factories

In long-running process environments (RoadRunner, Swoole, ReactPHP), the Symfony kernel's reset() cycle is called between requests. Previously, built schemas were cached via a
static variable inside the getBuilder() closure, which meant they survived resets entirely — causing stale type registries and memory growth over time.

This change moves schema caching into SchemaBuilder::$builders (an instance property), so reset() can selectively evict schemas marked as resettable: true. Non-resettable
schemas (the default) are kept across resets for performance. The same principle applies to AbstractResolver: factories are registered once at boot and survive resets, while
resolved instances are cleared so they can be rebuilt fresh on the next request.

  # config/packages/graphql.yaml
  overblog_graphql:                                                                                                                                                               
      definitions:                                                                                                                                                                
          schema:                                                                                                                                                                 
              default:                                                                                                                                                            
                  query: RootQuery                                                                                                                                                
                  resettable: true   # rebuilt on kernel reset         

  1. TypeResolver::getCurrentSchemaName()

What changed:

  • Added a public getter getCurrentSchemaName(): ?string alongside the existing setCurrentSchemaName()

The current schema name is set internally during schema build but was previously inaccessible from outside the resolver. Services and extensions that need to apply
schema-specific logic (e.g. different ACL rules per schema, custom type loaders, multi-schema middleware) had no way to read which schema is currently being resolved without
injecting additional state. This getter exposes what was already tracked internally, with zero behavioral change.


  1. #[IsPublic] support on InputObject fields

What changed:

  • InputObjectTypeDefinition now accepts a public key on field-level config
  • MetadataParser reads #[IsPublic] attributes (and @ispublic annotations) on InputObject class properties and maps them to public in the field configuration
  • Using #[IsPublic] on a property without #[Field] throws InvalidArgumentException (consistent with the existing behavior for #[Access] on object types)

#[IsPublic] was already supported on ObjectType fields and at the provider level, but InputObject fields had no equivalent. This gap meant that input fields carrying sensitive
data (e.g. an adminNotes field on a search input) could not be conditionally hidden via expression language — the only option was to create separate input types. This change
brings InputObject field visibility in line with ObjectType field visibility, using the same expression-language mechanism already present in the bundle.

@Nyholm

Nyholm commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator

Oh, This looks awesome.

Thank you. I've asked some friends to give in a thorough review.

@martiis

martiis commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor

Hi, thank you for MR, looking good! The only thing still missing is documentation for the new resettable config and update about public attribute

@Nyholm

Nyholm commented Apr 13, 2026

Copy link
Copy Markdown
Collaborator

@vlydev Can you please add some documentation and I'll be happy to merge

@vlydev

vlydev commented Apr 14, 2026

Copy link
Copy Markdown
Author

@Nyholm done, please check

@vlydev

vlydev commented May 6, 2026

Copy link
Copy Markdown
Author

@Nyholm check please and merge

@MGDSoft

MGDSoft commented May 30, 2026 •

Copy link
Copy Markdown

Some AI agent is available to check this solution? 😭🤣🤣

@vlydev

vlydev commented Jun 25, 2026

Copy link
Copy Markdown
Author

Any updates?

@vlydev

vlydev commented Aug 31, 2026

Copy link
Copy Markdown
Author

@Nyholm @martiis ping

@vlydev

vlydev commented Sep 22, 2026

Copy link
Copy Markdown
Author

Some AI agent is available to check this solution?

@MGDSoft

The PR has three independent changes. None of them breaks backward compatibility, and every new behavior is opt-in or additive.

  1. Resettable schemas (for long-running workers: RoadRunner, FrankenPHP, Swoole)
  • Problem: the built schema was cached in a static $schema variable inside a closure. Nothing could clear it, so a stale schema stayed in the worker across all requests.
  • Change:
    • SchemaBuilder now caches built schemas in an instance property $builders and implements ResetInterface. reset() drops only schemas marked resettable.
    • New per-schema config option resettable: true|false, default false.
    • ExtensibleSchema gets isResettable() / setIsResettable().
    • Executor keeps builder closures ($schemaBuilders) separate from built schemas ($schemas). On reset it keeps only non-resettable ExtensibleSchema instances.
    • AbstractResolver keeps factories ($solutionsFactory) separate from built instances ($solutions). reset() clears the instances, and the factories stay registered.
      TypeResolver is tagged kernel.reset.
  • Default behavior (resettable: false) is unchanged. The schema is built once per worker.
  1. TypeResolver::getCurrentSchemaName()
  • Adds a public getter next to the existing setter. It returns the name of the schema currently being built or resolved.
  • Use case: per-schema logic (ACL, type loaders) without extra state. Behavior is unchanged.
  1. #[IsPublic] / public: on input-object fields
  • InputObjectTypeDefinition accepts public on fields, the same way object-type fields already do.
  • MetadataParser maps #[GQL\IsPublic] / @gql\IsPublic on an #[GQL\Input] property to public in the field config.
  • #[IsPublic] without #[Field] throws InvalidArgumentException. This matches the existing behavior for object types.
  • At runtime the existing PublicFieldsFilterConfigProcessor removes the field when the expression is false. The field disappears from introspection and is rejected if a client
    sends it.

How to verify

  1. Unit tests:
    git fetch origin pull/1241/head:pr-1241 && git checkout pr-1241
    composer install
    vendor/bin/phpunit --filter 'SchemaBuilderTest|ExtensibleSchemaTest|ExecutorTest|TypeResolverTest|TestMetadataParser'
    vendor/bin/phpunit # full suite, nothing else should change
  2. Resettable schema, manual check in a sample app:
    • Set resettable: true on a schema. Call $executor->getSchema('default') twice with $container->get('services_resetter')->reset() in between. You should get different
      instances.
    • Repeat with resettable: false (or no option). You should get the same instance.
  3. getCurrentSchemaName(): inside any service that has TypeResolver injected, call it during a request to the admin schema. It should return 'admin'.
  4. Input field visibility:
    • Add internalNote: { type: String, public: "@=isGranted('ROLE_ADMIN')" } to an input type.
    • As an anonymous user: introspection does not list internalNote, and a query that sends it fails with "Field is not defined by type".
    • As an admin: the field is visible and accepted.
  5. Backward compatibility: existing configs without resettable and without input public should produce the same generated types (graphql:compile output diff is empty).

@whitemarket-official

Copy link
Copy Markdown

@Nyholm any updates?

@badsgahhl

Copy link
Copy Markdown

Hi @vlydev,

we just opened #1254 with a proposal for performance work on the bundle. Part of it touches the same
files as this PR (AbstractResolver, Request\Executor, TypeResolver), so I would rather build on top
of this PR than compete with it. I applied this PR to master and our branch on top of it; the test
suite passes.

While doing that, I ran an audit of the bundle in a long-running process against this PR: one kernel,
kernel.reset before every further request, and every response compared with the response of a fresh
kernel. With this PR, requests are answered again: the functional test suite of the bundle passes as a
long-running process, where master fails at the second request (#1242). Some responses still differ
from a fresh kernel, and one of the differences is security-relevant:

A field with public: "@=isGranted('ROLE_ADMIN')" is decided by the first request of a worker.
After a request of an admin, a regular user gets the value of the field. After a request of a regular
user, the admin gets Cannot query field "privateData". This happens with resettable: false and with
resettable: true.

The cause, as far as I can tell: a type is created once per container (generated types are shared
services) and filters its fields by public when it is built. The reset clears the instances in the type
resolver and, with resettable: true, the schema, but the next request gets the same type objects from
the container again.

All differences the audit finds with this PR

Audit: reset-audit/ in https://github.com/badsgahhl/graphql-benchmarks, run as
reset-audit/run.sh <checkout with this PR> <checkout with dev dependencies>. 17 tests, 7 fail with
either setting:

resettable: false resettable: true
Field visibility (public), admin then user and user then admin wrong value / wrong error same
A type addressed by its name (fragment on User) in a later request than the one that loaded it UnresolvableException: Could not find type with alias "User" same
A type shared by two schemas (both orders) differs same
Two schemas in turn; two schemas with types of the same name ok errors (e.g. Internal server Error)
Schema names after a reset public listed twice ok
A type is loaded for the schema of the request loaded for the other schema ok

The direction I would suggest (not tried yet): let a reset drop the loaded types and the built schemas
and keep only the builders, so that every request builds its types as on PHP-FPM. I'm happy to help with
that, here or in a separate PR, whatever you prefer.

The audit and this comment were prepared with Claude.

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.

6 participants