Magento 2 custom module graphql error
-
15-04-2021 - |
Question
I have a custom module with schema.graphqls
module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="NeoSolax_CompareList" setup_version="0.0.3" >
<sequence>
<module name="Magento_GraphQl"/>
</sequence>
</module>
</config>
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'NeoSolax_CompareList',
__DIR__
);
schema.graphqls
type Mutation {
addProductToCompareList(input:compareListInput): CompareListOutput @resolver(class: "NeoSolax\\CompareList\\Model\\Resolver\\addToCompareList")@doc(description:"Add products to compare")
}
input compareListInput{
productId:String
customerId: String
}
type CompareListOutput{
count:Int
listUrl:String
items:[CompareItem]
}
type CompareItem{
id:String
product_url:String
name:String
remove_url:String
productScope:String
}
When I try to connect my backend with Altair extension I get the following error
"1 exception(s):\nException #0 (GraphQL\\Error\\Error): Type \"CompareListOutput\" not found in document.\n\nException #0 (GraphQL\\Error\\Error): Type \"CompareListOutput\" not found in document.\n<pre>#1 GraphQL\\Utils\\ASTDefinitionBuilder->internalBuildType() called at [vendor/webonyx/graphql-php/src/Utils/ASTDefinitionBuilder.php:189]\n#2 GraphQL\\Utils\\ASTDefinitionBuilder->buildType() called at [vendor/webonyx/graphql-php/src/Utils/ASTDefinitionBuilder.php:171]\n#3 GraphQL\\Utils\\ASTDefinitionBuilder->internalBuildWrappedType() called at [vendor/webonyx/graphql-php/src/Utils/ASTDefinitionBuilder.php:309]\n#4 GraphQL\\Utils\\ASTDefinitionBuilder->buildField() called at [vendor/webonyx/graphql-php/src/Utils/ASTDefinitionBuilder.php:297]\n#5 GraphQL\\Utils\\ASTDefinitionBuilder->GraphQL\\Utils\\{closure}() called at [vendor/webonyx/graphql-php/src/Utils/Utils.php:284]\n#6 GraphQL\\Utils\\Utils::keyValMap() called at [vendor/webonyx/graphql-php/src/Utils/ASTDefinitionBuilder.php:298]\n#7 GraphQL\\Utils\\ASTDefinitionBuilder->makeFieldDefMap() called at [vendor/webonyx/graphql-php/src/Utils/ASTDefinitionBuilder.php:279]\n#8 GraphQL\\Utils\\ASTDefinitionBuilder->GraphQL\\Utils\\{closure}() called at
addToCompareList.php
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
$productId = $args['productId'];
if ($productId && ($this->_customerVisitor->getId() || $this->_customerSession->isLoggedIn())) {
$storeId = $this->_storeManager->getStore()->getId();
try {
/** @var Product $product */
$product = $this->productRepository->getById($productId, false, $storeId);
} catch (NoSuchEntityException $e) {
$product = null;
}
if ($product && $this->compareAvailability->isAvailableForCompare($product)) {
$this->_catalogProductCompareList->addProduct($product);
}
}
$compareList = $this->compareProducts->getSectionData();
return [
'items' => $compareList['items'],
'count'=>$compareList['count'],
'listUrl'=>$compareList['listUrl']
];
}
What have I done wrong please help
Solution
Keep Space between CompareListOutput and the opening bracket. now your output type graphql look like a single word, if there is no space between them cannot understand the graphql compiler
wrong => CompareListOutput{
correct => CompareListOutput {
Rewrite like below.
type CompareListOutput {
count:Int
listUrl:String
items:[CompareItem]
}
Hope this works .
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange