craftcms / cms

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

TypeError when querying GraphQL #4848

Closed vieko closed 5 years ago

vieko commented 5 years ago

Description

I get the following error regardless of what query I run:

{"error": "Something went wrong when processing the GraphQL query."}

Looks like we are running into a type error when generating the schema:

TypeError: Return value of craft\models\MatrixBlockType::getField() must be an instance of craft\fields\Matrix, instance of craft\fields\Entries returned in /var/www/html/vendor/craftcms/cms/src/models/MatrixBlockType.php:135
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/elements/MatrixBlock.php(140): craft\models\MatrixBlockType->getField()
#1 /var/www/html/vendor/craftcms/cms/src/gql/types/generators/MatrixBlockType.php(45): craft\elements\MatrixBlock::gqlTypeNameByContext(Object(craft\models\MatrixBlockType))
#2 /var/www/html/vendor/craftcms/cms/src/gql/interfaces/elements/MatrixBlock.php(52): craft\gql\types\generators\MatrixBlockType::generateTypes()
#3 /var/www/html/vendor/craftcms/cms/src/gql/types/elements/MatrixBlock.php(30): craft\gql\interfaces\elements\MatrixBlock::getType()
#4 /var/www/html/vendor/craftcms/cms/src/gql/types/generators/MatrixBlockType.php(63): craft\gql\types\elements\MatrixBlock->__construct(Array)
#5 /var/www/html/vendor/craftcms/cms/src/fields/Matrix.php(787): craft\gql\types\generators\MatrixBlockType::generateTypes(Object(craft\fields\Matrix))
#6 /var/www/html/vendor/craftcms/cms/src/gql/types/generators/EntryType.php(50): craft\fields\Matrix->getContentGqlType()
#7 /var/www/html/vendor/craftcms/cms/src/gql/interfaces/elements/Entry.php(53): craft\gql\types\generators\EntryType::generateTypes()
#8 /var/www/html/vendor/craftcms/cms/src/gql/queries/Entry.php(36): craft\gql\interfaces\elements\Entry::getType()
#9 /var/www/html/vendor/craftcms/cms/src/services/Gql.php(501): craft\gql\queries\Entry::getQueries()
#10 /var/www/html/vendor/craftcms/cms/src/services/Gql.php(162): craft\services\Gql->_registerGqlQueries()
#11 /var/www/html/vendor/craftcms/cms/src/controllers/GraphqlController.php(137): craft\services\Gql->getSchemaDef(Object(craft\models\GqlSchema), true)
#12 [internal function]: craft\controllers\GraphqlController->actionApi()
#13 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#14 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#15 /var/www/html/vendor/craftcms/cms/src/web/Controller.php(187): yii\base\Controller->runAction('api', Array)
#16 /var/www/html/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('api', Array)
#17 /var/www/html/vendor/craftcms/cms/src/web/Application.php(299): yii\base\Module->runAction('graphql/api', Array)
#18 /var/www/html/vendor/craftcms/cms/src/web/Application.php(566): craft\web\Application->runAction('graphql/api', Array)
#19 /var/www/html/vendor/craftcms/cms/src/web/Application.php(278): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#20 /var/www/html/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#21 /var/www/html/web/index.php(21): yii\base\Application->run()
#22 {main}

Next craft\errors\GqlException: Something went wrong when processing the GraphQL query. in /var/www/html/vendor/craftcms/cms/src/controllers/GraphqlController.php:141
Stack trace:
#0 [internal function]: craft\controllers\GraphqlController->actionApi()
#1 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#2 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#3 /var/www/html/vendor/craftcms/cms/src/web/Controller.php(187): yii\base\Controller->runAction('api', Array)
#4 /var/www/html/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('api', Array)
#5 /var/www/html/vendor/craftcms/cms/src/web/Application.php(299): yii\base\Module->runAction('graphql/api', Array)
#6 /var/www/html/vendor/craftcms/cms/src/web/Application.php(566): craft\web\Application->runAction('graphql/api', Array)
#7 /var/www/html/vendor/craftcms/cms/src/web/Application.php(278): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#8 /var/www/html/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#9 /var/www/html/web/index.php(21): yii\base\Application->run()
#10 {main}

Steps to reproduce

  1. Update to the latest Craft and setup the GraphQL per instructions.
  2. Try to query the GraphQL endpoint for a localhost development environment.

Additional info

andris-sevcenko commented 5 years ago

Is it possible to get a DB dump, composer.lock and composer.json files over email to support@craftcms.com?

andris-sevcenko commented 5 years ago

Also, if it is, can you reference this github issue?

brandonkelly commented 5 years ago

From the error it looks like you had a Matrix field that was converted to an Entries field. Unfortunately Craft doesn’t currently have a way to clean up after itself when that happens, so the Matrix blocks still exist in the database.

I’ve just updated getAllBlockTypes() to ignore any block types that are no longer associated with a Matrix field, which should fix this error for you.

To get the fix early, change your craftcms/cms requirement in composer.json to:

"require": {
  "craftcms/cms": "dev-develop#16c1db5676c7c5eab63c56c927c9f576466073ee as 3.3.0.1",
  "...": "..."
}

Then run composer update.

vieko commented 5 years ago

Thank you Brandon, good to know! will give it a try.

khalwat commented 5 years ago

Did this end up fixing it for you @vieko ?

vieko commented 5 years ago

This did fix it for me.

ricricucit commented 4 years ago

@brandonkelly I'm getting this error ({"error": "Something went wrong when processing the GraphQL query."}), only on one environment out of 3, sending this:

query gtm {
      globalSets (id: 4) {
        ... on services_GlobalSet {
          GTM
        }
    }
  }

This works perfectly fine when launched from http://whateverEnvUrl/admin/graphiql?schemaUid=whatever, but when calling it from /admin/actions/graphql/api this fails on my production server. (not on staging, not locally).

Any hint? (the field has always been there, never changed).

On the logs:

2020-09-07 16:43:32 [-][106432][-][error][craft\errors\GqlException] craft\errors\GqlException: Tried to load an unregistered type „services_GlobalSet”. This can indicate both a typo in the query or an issue with the schema used. in /srv/www/phineo-wok-stage.siruphosting.net/web/htdocs/craft/vendor/craftcms/cms/src/gql/TypeLoader.php:39
Stack trace:
#0 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Type/Schema.php(319): craft\gql\TypeLoader::loadType('services_Global...')
#1 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Type/Schema.php(212): GraphQL\Type\Schema->loadType('services_Global...')
#2 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Utils/AST.php(504): GraphQL\Type\Schema->getType('services_Global...')
#3 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Utils/TypeInfo.php(66): GraphQL\Utils\AST::typeFromAST(Object(GraphQL\Type\Schema), Object(GraphQL\Language\AST\NamedTypeNode))
#4 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Validator/Rules/OverlappingFieldsCanBeMerged.php(825): GraphQL\Utils\TypeInfo::typeFromAST(Object(GraphQL\Type\Schema), Object(GraphQL\Language\AST\NamedTypeNode))
#5 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Validator/Rules/OverlappingFieldsCanBeMerged.php(739): GraphQL\Validator\Rules\OverlappingFieldsCanBeMerged->internalCollectFieldsAndFragmentNames(Object(GraphQL\Validator\ValidationContext), NULL, Object(GraphQL\Language\AST\SelectionSetNode), Array, Array)
#6 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Validator/Rules/OverlappingFieldsCanBeMerged.php(162): GraphQL\Validator\Rules\OverlappingFieldsCanBeMerged->getFieldsAndFragmentNames(Object(GraphQL\Validator\ValidationContext), NULL, Object(GraphQL\Language\AST\SelectionSetNode))
#7 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Validator/Rules/OverlappingFieldsCanBeMerged.php(71): GraphQL\Validator\Rules\OverlappingFieldsCanBeMerged->findConflictsWithinSelectionSet(Object(GraphQL\Validator\ValidationContext), NULL, Object(GraphQL\Language\AST\SelectionSetNode))
#8 [internal function]: GraphQL\Validator\Rules\OverlappingFieldsCanBeMerged->GraphQL\Validator\Rules\{closure}(Object(GraphQL\Language\AST\SelectionSetNode), 'selectionSet', Object(GraphQL\Language\AST\FieldNode), Array, Array)
#9 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Language/Visitor.php(372): call_user_func_array(Object(Closure), Array)
#10 [internal function]: GraphQL\Language\Visitor::GraphQL\Language\{closure}(Object(GraphQL\Language\AST\SelectionSetNode), 'selectionSet', Object(GraphQL\Language\AST\FieldNode), Array, Array)
#11 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Language/Visitor.php(426): call_user_func_array(Object(Closure), Array)
#12 [internal function]: GraphQL\Language\Visitor::GraphQL\Language\{closure}(Object(GraphQL\Language\AST\SelectionSetNode), 'selectionSet', Object(GraphQL\Language\AST\FieldNode), Array, Array)
#13 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Language/Visitor.php(251): call_user_func(Object(Closure), Object(GraphQL\Language\AST\SelectionSetNode), 'selectionSet', Object(GraphQL\Language\AST\FieldNode), Array, Array)
#14 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Validator/DocumentValidator.php(259): GraphQL\Language\Visitor::visit(Object(GraphQL\Language\AST\DocumentNode), Array)
#15 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/Validator/DocumentValidator.php(96): GraphQL\Validator\DocumentValidator::visitUsingRules(Object(GraphQL\Type\Schema), Object(GraphQL\Utils\TypeInfo), Object(GraphQL\Language\AST\DocumentNode), Array)
#16 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/GraphQL.php(140): GraphQL\Validator\DocumentValidator::validate(Object(GraphQL\Type\Schema), Object(GraphQL\Language\AST\DocumentNode), Array)
#17 /srv/www/liveEnv/web/htdocs/craft/vendor/webonyx/graphql-php/src/GraphQL.php(85): GraphQL\GraphQL::promiseToExecute(Object(GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter), Object(GraphQL\Type\Schema), 'query gtm {\n   ...', NULL, NULL, NULL, NULL, NULL, Array)
#18 /srv/www/liveEnv/web/htdocs/craft/vendor/craftcms/cms/src/services/Gql.php(474): GraphQL\GraphQL::executeQuery(Object(GraphQL\Type\Schema), 'query gtm {\n   ...', NULL, NULL, NULL, NULL, NULL, Array)
#19 /srv/www/liveEnv/web/htdocs/craft/vendor/craftcms/cms/src/controllers/GraphqlController.php(149): craft\services\Gql->executeQuery(Object(craft\models\GqlSchema), 'query gtm {\n   ...', NULL, NULL, false)
#20 [internal function]: craft\controllers\GraphqlController->actionApi()
#21 /srv/www/liveEnv/web/htdocs/craft/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#22 /srv/www/liveEnv/web/htdocs/craft/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams(Array)
#23 /srv/www/liveEnv/web/htdocs/craft/vendor/craftcms/cms/src/web/Controller.php(181): yii\base\Controller->runAction('api', Array)
#24 /srv/www/liveEnv/web/htdocs/craft/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('api', Array)
#25 /srv/www/liveEnv/web/htdocs/craft/vendor/craftcms/cms/src/web/Application.php(294): yii\base\Module->runAction('graphql/api', Array)
#26 /srv/www/liveEnv/web/htdocs/craft/vendor/craftcms/cms/src/web/Application.php(597): craft\web\Application->runAction('graphql/api', Array)
#27 /srv/www/liveEnv/web/htdocs/craft/vendor/craftcms/cms/src/web/Application.php(273): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#28 /srv/www/liveEnv/web/htdocs/craft/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#29 /srv/www/liveEnv/web/htdocs/index.php(21): yii\base\Application->run()
#30 {main}
2020-09-07 16:43:32 [-][106432][-][info][application] $_GET = [
    'p' => 'admin/actions/graphql/api'
]

JFYI, the call:

window.fetch('/admin/actions/graphql/api', {
    method: 'POST',
    body: `query gtm {
      globalSets (id: 4) {
        ... on services_GlobalSet {
          GTM
        }
      }
    }`,
    headers: {
      Authorization: bearerToken,
      'Content-Type': 'application/graphql'
    }
  })
andris-sevcenko commented 4 years ago

@ricricucit is the schema associated with the token configured to actually see the relevant global set?

ricricucit commented 4 years ago

yep

andris-sevcenko commented 4 years ago

@ricricucit is it possible to get a DB dump and the composer.json/composer.lock files to reproduce? If so, can you send the over to support@craftcms.com and reference this issue?

ricricucit commented 4 years ago

ok, sending a WeTransfer.

roelvanhintum commented 3 years ago

@ricricucit did you find a solution for this? I'm running into the exact same issue.

ricricucit commented 3 years ago

sorry, but unfortunately I do not remember. Checking in my inbox, the communication with CraftCMS support have been dropped, without a clear solution from anyone, but looking at the client's website...things are actually working, so I solved it, somehow.......that's all I can say...

andris-sevcenko commented 3 years ago

@ricricucit @roelvanhintum for posterity, here is the response I sent you on Sep 09, 2020:

Enrico,

that's really odd behavior.

It seems that your server configuration is stripping with the Authorization header.

There are multiple signs that point to this, but the most certain proof is that if I shoot a {ping} query at the server using a Bearer token with a random value, the server never complains about an invalid token, which means that the Authorization header is missing and the server falls back to the public schema.

I would probably not call it a dropped conversation with no real solution from anyone.

ricricucit commented 3 years ago

ok, I have dropped the convo, and: sorry @andris-sevcenko. Re-reading my message, it sounded like blaming support, which was not at all my intention. I do remember that the hint wasn't useful, tho...so I might have solved it using a new DB dump or something like that. @roelvanhintum: now @andris-sevcenko is here, you can ask him 👋🙂

andris-sevcenko commented 3 years ago

@ricricucit all good, Ricardo.

@roelvanhintum if you look at the token listing page, does the token "last used" time update after each request?

roelvanhintum commented 3 years ago

@andris-sevcenko i'm using the public scheme, so tokens shouldn't mather right? Ping does work, but all the other stuff does not. I'm guessing the public scheme settings aren't enforced from the project config. I'll try enabling admin to verify this.

Also, I am going to try copying my database to the accept server to see if that fixes it. The acceptance db is setup using the project config, instead of a database copy, so maybe that is the problem.

roelvanhintum commented 3 years ago

@andris-sevcenko somehow i ended up with 2 public schemes in my accept environment (allowAdminChanges has never been enabled here). The first one is empty and probably used for public queries. Removing the empty (no scope) one solves the problem.

andris-sevcenko commented 3 years ago

@roelvanhintum huh. That's odd. Let me know if you ever encounter this again!