Skip to content

Commit 03d84d2

Browse files
author
Vitaliy
authored
Merge pull request #159 from konarshankar07/schema-graphql-resolver-inspection--task-35
#35 :- Inspection. GraphQL resolver should implement Resolver interface
2 parents 8e0c566 + e15af0c commit 03d84d2

File tree

19 files changed

+290
-44
lines changed

19 files changed

+290
-44
lines changed

resources/META-INF/withJsGraphQl.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
18
<idea-plugin>
29
<extensions defaultExtensionNs="com.intellij">
310
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.graphql.GraphQlResolverIndex" />
@@ -9,5 +16,11 @@
916
enabledByDefault="true"
1017
level="ERROR"
1118
implementationClass="com.magento.idea.magento2plugin.inspections.php.GraphQlResolverInspection"/>
19+
<localInspection language="GraphQL" groupPath="GraphQL"
20+
shortName="SchemaResolverInspection" displayName="Schema Resolver must implements ResolverInterface"
21+
groupName="Magento 2"
22+
enabledByDefault="true"
23+
level="ERROR"
24+
implementationClass="com.magento.idea.magento2plugin.inspections.graphqls.SchemaResolverInspection"/>
1225
</extensions>
1326
</idea-plugin>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<p>
10+
This inspection detects the invalid resolver interface in schema.graphqls files
11+
</p>
12+
</body>
13+
</html>

resources/magento2/inspection.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
inspection.class.hierarchyImplementationCheck=Class must implement {0}
2-
inspection.class.hierarchyExtendingCheck=Class must extend {0}
31
inspection.plugin.duplicateInSameFile=The plugin name already used in this file. For more details see Inspection Description.
42
inspection.plugin.duplicateInOtherPlaces=The plugin name "{0}" for targeted "{1}" class is already used in the module "{2}" ({3} scope). For more details see Inspection Description.
53
inspection.graphql.resolver.mustImplement=Class must implements any of the following interfaces: \\Magento\\Framework\\GraphQl\\Query\\ResolverInterface, \\Magento\\Framework\\GraphQl\\Query\\Resolver\\BatchResolverInterface, \\Magento\\Framework\\GraphQl\\Query\\Resolver\\BatchServiceContractResolverInterface
4+
inspection.graphql.resolver.notExist=Resolver class do not exist
65
inspection.graphql.resolver.fix.family=Implement Resolver interface
76
inspection.graphql.resolver.fix.title=Select one of the following interface
87
inspection.plugin.error.nonPublicMethod=You can't declare a plugin for a not public method.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.inspections.graphqls;
7+
8+
import com.intellij.codeInspection.LocalInspectionTool;
9+
import com.intellij.codeInspection.ProblemHighlightType;
10+
import com.intellij.codeInspection.ProblemsHolder;
11+
import com.intellij.lang.jsgraphql.psi.GraphQLValue;
12+
import com.intellij.lang.jsgraphql.psi.GraphQLVisitor;
13+
import com.jetbrains.php.lang.psi.elements.PhpClass;
14+
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
15+
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
16+
import com.magento.idea.magento2plugin.util.magento.graphql.GraphQlUtil;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
public class SchemaResolverInspection extends LocalInspectionTool {
20+
21+
private final InspectionBundle inspectionBundle = new InspectionBundle();
22+
23+
@NotNull
24+
@Override
25+
public GraphQLVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
26+
return new GraphQLVisitor() {
27+
@Override
28+
public void visitValue(@NotNull GraphQLValue element) {
29+
String getVisitedElementValue = element.getText();
30+
if (getVisitedElementValue == null) {
31+
return;
32+
}
33+
34+
String resolverFQN = GraphQlUtil.resolverStringToPhpFQN(getVisitedElementValue);
35+
GetPhpClassByFQN getPhpClassByFQN = GetPhpClassByFQN.getInstance(holder.getProject());
36+
PhpClass resolverClass = getPhpClassByFQN.execute(resolverFQN);
37+
if (resolverClass == null) {
38+
holder.registerProblem(element,
39+
inspectionBundle.message(
40+
"inspection.graphql.resolver.notExist"
41+
),
42+
ProblemHighlightType.ERROR);
43+
return;
44+
}
45+
if (!GraphQlUtil.isResolver(resolverClass)) {
46+
holder.registerProblem(element,
47+
inspectionBundle.message(
48+
"inspection.graphql.resolver.mustImplement"
49+
),
50+
ProblemHighlightType.ERROR);
51+
}
52+
}
53+
};
54+
}
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.magento.files;
6+
7+
public class GraphQLSchema {
8+
public static String FILE_NAME = "schema.graphqls";
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Query {
5+
test(input: test!): test @resolver(class: "\\Magento\\Backend\\Model\\Source\\YesNo") @doc(description: "Test")
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Query {
5+
test(input: test!): test @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\ImplementsBatchResolverInterface") @doc(description: "Test")
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Query {
5+
test(input: test!): test @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\ImplementsBatchServiceContractResolverInterface") @doc(description: "Test")
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Query {
5+
test(input: test!): test @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\ImplementsResolverInterface") @doc(description: "Test")
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Query\Resolver\BatchResolverInterface;
11+
12+
class ImplementsBatchResolverInterface implements BatchResolverInterface
13+
{
14+
}

0 commit comments

Comments
 (0)