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

[4.x]: Test curl API get error:$_schema must not be accessed before initialization #11453

Closed
Allen-lzr opened this issue Jun 17, 2022 · 2 comments

Comments

@Allen-lzr
Copy link

Allen-lzr commented Jun 17, 2022

What happened?

Description

Hi! I installed CraftCMS locally with GraphQL enabled. When I tested the graphQL with curl command, it got some errors.

Steps to reproduce

  1. Enable trial version: CraftCMS 4.0.4 Pro
  2. Modify config/routes.php, add 'api' => 'graphql/api',
  3. Modify config/general.php, add 'enableGql' => true,
  4. use curl:
    curl \
    -H "Content-Type: application/graphql" \
    -H "Authorization: Bearer K_W9XKV_m4hYBTg0BM9VXeVD9oHB9mPk" \
    -d '{ping}' \
    http://localhost/api
    

Expected behavior

Return pong

Actual behavior

Get error:

{"name":"Exception","message":"Typed property craft\\models\\GqlToken::$_schema must not be accessed before initialization","code":0,"type":"Error","file":"/var/www/html/craftcms/vendor/craftcms/cms/src/models/GqlToken.php","line":175,"stack-trace":["#0 /var/www/html/craftcms/vendor/craftcms/cms/src/models/GqlToken.php(140): craft\\models\\GqlToken->getSchema()","#1 /var/www/html/craftcms/vendor/craftcms/cms/src/controllers/GraphqlController.php(236): craft\\models\\GqlToken->getIsValid()","#2 /var/www/html/craftcms/vendor/craftcms/cms/src/controllers/GraphqlController.php(111): craft\\controllers\\GraphqlController->_schema()","#3 [internal function]: craft\\controllers\\GraphqlController->actionApi()","#4 /var/www/html/craftcms/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array()","#5 /var/www/html/craftcms/vendor/yiisoft/yii2/base/Controller.php(178): yii\\base\\InlineAction->runWithParams()","#6 /var/www/html/craftcms/vendor/yiisoft/yii2/base/Module.php(552): yii\\base\\Controller->runAction()","#7 /var/www/html/craftcms/vendor/craftcms/cms/src/web/Application.php(301): yii\\base\\Module->runAction()","#8 /var/www/html/craftcms/vendor/yiisoft/yii2/web/Application.php(103): craft\\web\\Application->runAction()","#9 /var/www/html/craftcms/vendor/craftcms/cms/src/web/Application.php(286): yii\\web\\Application->handleRequest()","#10 /var/www/html/craftcms/vendor/yiisoft/yii2/base/Application.php(384): craft\\web\\Application->handleRequest()","#11 /var/www/html/craftcms/web/index.php(12): yii\\base\\Application->run()","#12 {main}"]}

Craft CMS version

4.0.4

PHP version

8.1

Operating system and version

Ubuntu 22.04

Database type and version

MariaDB 10.6.7

Image driver and version

No response

Installed plugins and versions

@brandonkelly
Copy link
Member

The only way I was able to reproduce this was if I used a token that wasn’t set to a schema yet – either because it was created before there were any schemas, or if it was set to a schema that had been deleted.

I’ve “fixed” this for the next release, but only to make the behavior consistent with Craft 3: you will now get a 400 response with the message Invalid Authorization header, as Craft doesn’t actually consider a token “valid” unless it’s assigned to a schema.

You can work around the error by creating a schema, then re-editing your token and selecting the new schema.

@brandonkelly
Copy link
Member

Craft 4.0.5 is out now with that fix.

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

No branches or pull requests

2 participants