craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.29k stars 638 forks source link

[4.x]: GraphQL schema is no longer lazy loaded #15429

Closed markhuot closed 4 months ago

markhuot commented 4 months ago

What happened?

Description

The entire GraphQL schema is read in on every request, regardless of the query complexity or query needs. Because of this our web requests are rather slow (1.5s instead of 500ms). This adds up when you have a headless front-end making 3-4 requests per page.

I believe I've identified the reason for this, here: https://github.com/craftcms/cms/commit/b9d8ec55bc7cf20321a2fbfd20ce41785fd5f6bb

Steps to reproduce

  1. Submit a GraphQL query for {ping}.
  2. Notice that every matrix block is queried, every entry type is queried, etc… via the DB query log

Expected behavior

The schema should be lazy loaded, where possible.

Actual behavior

The entire schema is read every time.

Notes

I'm not sure how realistic is to reverse this decision or if there could be a config for it? Basically wondering what sort of issues I'd run in to if I tried to force a lazy schema.

Craft CMS version

4.9.5

PHP version

8.3.4

Operating system and version

Ubuntu

Database type and version

MySQL 8

Image driver and version

n/a

Installed plugins and versions

No response

brandonkelly commented 4 months ago

Thanks for pointing that out. On second look, the problematic query in #13622 could have been solved by putting the fragment after the main query, so I probably should have just recommended doing that.

I can’t revert the change without potentially breaking things at this point, but I did just add a new lazyGqlTypes config setting for 4.11 + 5.3, which you can enable to revert to the previous behavior. (4d805f478d3b4bc338ac61db3c24550799fd34ee)

markhuot commented 4 months ago

Thanks @brandonkelly! This is great and exactly what we needed.

markhuot commented 4 months ago

Note: I tried updating my composer.json to 4.x as 4.11.0 and running composer update but I'm getting Fatal error: Uncaught Error: Class "Yii" not found in…. Probably my issue though? Is there any reason the 4.x branch shouldn't work?

brandonkelly commented 4 months ago

@markhuot Sorry, that’s fixed now (a2bdfbfa3224d2b8d8c782e9b6202aa404874302). Bug introduced by #15346 that only affects installs that are still returning an array from their config/general.php or config/db.php files. I forgot the feature is coming to Craft 4.11, so I had fixed it for the 5.3 branch but not 4.11.