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

Never resolve additional types in Schema constructor #1137

Closed
wants to merge 1 commit into from

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented May 4, 2022

When you construct a Schema and pass an array of type callables like this:

Schema(['types' => [
  'TypeName' => function() { },
]);

the callables are immediately resolved in the constructor.

If you want the types to not be resolved, you must use a callable that returns the types array with callables like this:

Schema(['types' => fn() => [
  'TypeName' => function() { },
]);

I was surprised by that because I thought my array of callables was already enough for lazy loading.

When looking at the source code I find the distinction a bit weird and I think it can be handled in the same way.

When Schema::getTypeMap is called, it will call Schema::collectAllTypes which will call Schema::resolveAdditionalTypes.

@ruudk ruudk marked this pull request as ready for review May 4, 2022 11:14
When you construct a Schema and pass an array of type callables like this:
```php
Schema(['types' => [
  'TypeName' => function() { },
]);
```
the callables are immediately resolved in the constructor.

If you want the types to not be resolved, you must use a callable that returns the types array with callables like this:
```php
Schema(['types' => fn() => [
  'TypeName' => function() { },
]);
```

I was surprised by that because I thought my array of callables was already enough for lazy loading.

When looking at the source code I find the distinction a bit weird and I think it can be handled in the same way.

When `Schema::getTypeMap` is called, it will call `Schema::collectAllTypes` which will call `Schema::resolveAdditionalTypes`.
@spawnia
Copy link
Collaborator

spawnia commented May 4, 2022

I am trying to minimize type loading in #1104, perhaps you can base your pull request off of that.

@ruudk
Copy link
Contributor Author

ruudk commented May 4, 2022

@spawnia Hmm, not really sure how. Maybe you can include this change in that PR when you finish it.

@spawnia
Copy link
Collaborator

spawnia commented May 4, 2022

Lazy loading of types is done differently, consider https://webonyx.github.io/graphql-php/schema-definition/#lazy-loading-of-types

@ruudk
Copy link
Contributor Author

ruudk commented May 4, 2022

I need to register some types that are not referenced by other types, how is that possible without using the types array?

@spawnia
Copy link
Collaborator

spawnia commented May 4, 2022

Can you illustrate why you would need many such types? If there is a small number of them, eager loading them should be no problem.

Anyways, because types currently does not require to be a map with type names as keys, it is not really possible to load the types within lazily. As soon as the first unknown type needs to be found, we need to traverse and resolve the types within. We could change this, but that would be a big breaking change. Not sure if it is worth it.

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