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

Allow lazy root types (query, mutation, subscription) #1418

Merged
merged 18 commits into from Aug 4, 2023

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Aug 2, 2023

This makes it possible to create a schema with the root types lazy. This way, only when the root type is needed, the type will be initialized by calling the callable.

Why would a Schema have an instance of the mutation and subscription root type, when it's only serving a query operation? Visa versa. That does not make sense to me.

This is in line with the rest of the codebase, where all the types can be passed as lazy.

$schema = new Schema([
    'query' => fn () => $query,
    'mutation' => fn () => $mutation,
    'subscription' => fn () => $subscription,
]);

Alternative solution

An alternative solution could be to allow passing a string with the name of the root type. When needed, the Schema will call the typeLoader with the given name. For example:

new Schema([
    'query' => 'Query',
    'typeLoader' => $typeLoader,
]);

This makes it possible to create a schema with the root types lazy. This way, only when the root
type is needed, the type will be initialized by calling the callable.

Why would a Schema have an instance of the mutation and subscription root type, when it's only
serving a query operation? Visa versa. That does not make sense to me.
@shmax
Copy link
Contributor

shmax commented Aug 2, 2023

Nice concept. Don't know why I didn't think of it when I was working on this stuff. 👍

src/Type/Schema.php Outdated Show resolved Hide resolved
src/Type/Schema.php Outdated Show resolved Hide resolved
src/Type/Schema.php Outdated Show resolved Hide resolved
@ruudk
Copy link
Contributor Author

ruudk commented Aug 4, 2023

@spawnia nice, added some tweaks as comments

@spawnia spawnia merged commit 73ef849 into webonyx:master Aug 4, 2023
@spawnia
Copy link
Collaborator

spawnia commented Aug 4, 2023

Thank you, released with https://github.com/webonyx/graphql-php/releases/tag/v15.6.0.

@stayallive
Copy link
Contributor

Very nice 🤘

spawnia added a commit to nuwave/lighthouse that referenced this pull request Aug 7, 2023
@N-Silbernagel
Copy link

Is it possible to leverage this lazy loading using the schema definition language?

@spawnia
Copy link
Collaborator

spawnia commented Nov 28, 2023

Is it possible to leverage this lazy loading using the schema definition language?

Sure, depends on the implementation.

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.

None yet

5 participants