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

Automatically reset static caches in Type, Introspection and Directive when overriding standard types #1632

Merged
merged 25 commits into from
Nov 13, 2024

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Nov 11, 2024

See #1424

This library uses a static cache for standard types and the internal directives.

While not ideal, this works fine. As long as you don't have a scenario where you want to change them.

In my situation, we have 2 schema's with a custom ID type. They are not the same instance. This still works fine, but as soon as we run our whole test suite at once, the static cache is initialized in some test, and another test later expects something else.

I think the better solution would be to remove these static caches completely, and store them on an instance instead of statically. But that will be a much bigger task given the current architecture.

When you use overrideStandardTypes, the static caches will automatically be reset.

It could also be used for people that run their GraphQL server in tools like RoadRunner and FrankenPHP.

@ruudk
Copy link
Contributor Author

ruudk commented Nov 11, 2024

CI is broken because of Renovate. See #1630 (comment)

@ruudk ruudk changed the title Add Type::reset and Directive::reset methods Add reset method to Type, Directive and Introspection Nov 11, 2024
@ruudk ruudk changed the title Add reset method to Type, Directive and Introspection Add reset method to Type, Directive and Introspection + Store built-in types in static property + Store introspection field definitions as property on instance Nov 11, 2024
src/Type/Definition/Directive.php Outdated Show resolved Hide resolved
src/Type/Definition/Type.php Outdated Show resolved Hide resolved
@ruudk
Copy link
Contributor Author

ruudk commented Nov 12, 2024

@spawnia Added a test that covers all 3 reset methods, since they are related. If you clear the Type's cache, the other's also have to be reset to prevent duplicate type instances.

@ruudk ruudk requested a review from spawnia November 12, 2024 07:40
@ruudk
Copy link
Contributor Author

ruudk commented Nov 12, 2024

Another idea could be to introduce a single TypeCache object, where all the types are cached.

Then there is only 1 place to reset the cache.

See webonyx#1424

This library uses a static cache for standard types and the internal directives.

While not ideal, this works fine. As long as you don't have a scenario where you want to change
them.

In my situation, we have 2 schema's with a custom ID type. They are not the same instance.
This still works fine, but as soon as we run our whole test suite at once, the static cache
is initialized in some test, and another test later expects something else.

While I think the better solution would be to remove these static caches completely, and store
them on an instance instead of statically. But that will be a much bigger task given the current
architecture.

With these 2 reset methods, we can manually reset the caches in our test suite.

It could also be used for people that run their GraphQL server in tools like RoadRunner
and FrankenPHP.
This way, it behaves the same as the standard types.
Since the ReferenceExecutor is an instance, we can keep this cache on the instance. Great!
This is done in a single test, because if you reset the Type without resetting the rest
it will result in different standard types.
src/Type/Definition/Type.php Outdated Show resolved Hide resolved
src/Type/Definition/Type.php Outdated Show resolved Hide resolved
src/Type/Definition/Type.php Outdated Show resolved Hide resolved
src/Type/Definition/Type.php Outdated Show resolved Hide resolved
ruudk and others added 4 commits November 12, 2024 18:16
Co-authored-by: Benedikt Franke <benedikt@franke.tech>
Co-authored-by: Benedikt Franke <benedikt@franke.tech>
Co-authored-by: Benedikt Franke <benedikt@franke.tech>
Co-authored-by: Benedikt Franke <benedikt@franke.tech>
@spawnia
Copy link
Collaborator

spawnia commented Nov 12, 2024

Another idea could be to introduce a single TypeCache object, where all the types are cached.

Then there is only 1 place to reset the cache.

I like the idea of centralizing this into a single method that library users are expected to call. From the perspective of a user of this library, isn't the need to reset anything tied to using Type::overrideStandardTypes()? So how about we just reset all dependent caches when it is called. Or are there other situations where it would be necessary?

@ruudk
Copy link
Contributor Author

ruudk commented Nov 12, 2024

Smart! I'll test if that is sufficient.

@ruudk
Copy link
Contributor Author

ruudk commented Nov 12, 2024

Alright. When I do it like this, it works perfectly for my use case. But there is 1 test that fails: testPreservesOriginalStandardTypes.

I think this test does not make much sense to be honest. When you are overriding the standard types, why would you expect the other standard types to be the same instance? Is it expected that the standard types are already initialized (used) before calling overrideStandardTypes? I don't think so.

What do you think @spawnia ?

https://github.com/search?q=%22Type%3A%3AoverrideStandardTypes%22+OR+%22GraphQL%3A%3AoverrideStandardTypes%22+lang%3Aphp&type=code&p=1

@ruudk ruudk requested a review from spawnia November 12, 2024 18:28
src/Type/Definition/Type.php Outdated Show resolved Hide resolved
src/GraphQL.php Outdated Show resolved Hide resolved
src/Type/Definition/Type.php Outdated Show resolved Hide resolved
I think end users don't have to care about this, they can use the overrideStandardTypes
method for this.
@ruudk ruudk changed the title Add reset method to Type, Directive and Introspection + Store built-in types in static property + Store introspection field definitions as property on instance Automatically reset static caches in Type, Introspection and Directive when overriding standard types Nov 13, 2024
@spawnia
Copy link
Collaborator

spawnia commented Nov 13, 2024

I modified the test to be more explicit about what it is trying to achieve and also make it more understandable why we would want that. @ruudk Are you ok with the current state?

@ruudk
Copy link
Contributor Author

ruudk commented Nov 13, 2024

@spawnia Yeah this makes sense. I tested it on our CI and it works 🎉 Thanks for the help!

@spawnia spawnia merged commit a167afa into webonyx:master Nov 13, 2024
16 checks passed
@spawnia
Copy link
Collaborator

spawnia commented Nov 13, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants