Skip to content

Commit 5ed7e07

Browse files
Merge pull request #1123 from SilinMykola/1105-add-graphql-schema-file-create-action
1105 add graphQL schema file creation action
2 parents 6cb0b5f + 4b1717a commit 5ed7e07

File tree

7 files changed

+131
-14
lines changed

7 files changed

+131
-14
lines changed

resources/META-INF/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<separator/>
6161
<!-- Context dependent actions -->
6262
<action id="MagentoCreateAclFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewAclXmlAction"/>
63+
<action id="MagentoCreateGraphQlSchemaFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewGraphQLSchemaAction"/>
6364
<action id="MagentoCreateConfigFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewConfigXmlAction"/>
6465
<action id="MagentoCreateCrontabFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewCrontabXmlAction"/>
6566
<action id="MagentoCreateDiFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewDiXmlAction"/>
@@ -626,6 +627,7 @@
626627
<internalFileTemplate name="Magento Routes XML"/>
627628
<internalFileTemplate name="Magento Layout XML"/>
628629
<internalFileTemplate name="Magento ACL XML"/>
630+
<internalFileTemplate name="Magento GraphQL Schema"/>
629631
<internalFileTemplate name="Magento Collection Class"/>
630632
<internalFileTemplate name="Magento Model Class"/>
631633
<internalFileTemplate name="Magento Resource Model Class"/>

resources/fileTemplates/internal/Magento GraphQL Schema.graphqls.ft

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html lang="en">
8+
<body>
9+
<font face="verdana" size="-1">
10+
<p>
11+
The schema.graphqls file defines the basic structure of GraphQL queries and mutations.
12+
</p>
13+
<p>
14+
Read more about <a href="https://devdocs.magento.com/guides/v2.4/graphql/develop/create-graphqls-file.html">Define the GraphQL schema for a module</a>.
15+
</p>
16+
</font>
17+
</body>
18+
</html>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.context.xml;
7+
8+
import com.intellij.ide.fileTemplates.actions.AttributesDefaults;
9+
import com.intellij.psi.PsiDirectory;
10+
import com.intellij.psi.PsiFile;
11+
import com.magento.idea.magento2plugin.actions.context.AbstractContextAction;
12+
import com.magento.idea.magento2plugin.magento.files.SchemaGraphQLsFile;
13+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
14+
import com.magento.idea.magento2plugin.magento.packages.Package;
15+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
16+
import org.jetbrains.annotations.NotNull;
17+
18+
19+
public class NewGraphQLSchemaAction extends AbstractContextAction {
20+
21+
public static final String ACTION_NAME = "Magento 2 GraphQL Schema File";
22+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 schema.graphqls file";
23+
24+
/**
25+
* New schema.graphqls file action constructor.
26+
*/
27+
public NewGraphQLSchemaAction() {
28+
super(ACTION_NAME, ACTION_DESCRIPTION, SchemaGraphQLsFile.getInstance());
29+
}
30+
31+
@Override
32+
protected boolean isVisible(
33+
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
34+
final PsiDirectory targetDirectory,
35+
final PsiFile targetFile
36+
) {
37+
final PsiDirectory configDir = moduleData.getConfigDir();
38+
39+
if (configDir == null) {
40+
return false;
41+
}
42+
43+
return targetDirectory.getName().equals(Package.moduleBaseAreaDir)
44+
&& targetDirectory.equals(configDir)
45+
&& moduleData.getType().equals(ComponentType.module);
46+
}
47+
48+
@Override
49+
protected AttributesDefaults getProperties(
50+
final @NotNull AttributesDefaults defaults,
51+
final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData,
52+
final PsiDirectory targetDirectory,
53+
final PsiFile targetFile
54+
) {
55+
return defaults;
56+
}
57+
}

src/com/magento/idea/magento2plugin/magento/files/GraphQLSchema.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.magento.files;
7+
8+
import com.intellij.lang.Language;
9+
import com.intellij.lang.jsgraphql.GraphQLLanguage;
10+
11+
public class SchemaGraphQLsFile implements ModuleFileInterface {
12+
13+
public static final String FILE_NAME = "schema.graphqls";
14+
15+
public static final String TEMPLATE = "Magento GraphQL Schema";
16+
private static final SchemaGraphQLsFile INSTANCE = new SchemaGraphQLsFile();
17+
18+
public static SchemaGraphQLsFile getInstance() {
19+
return INSTANCE;
20+
}
21+
22+
@Override
23+
public String getFileName() {
24+
return FILE_NAME;
25+
}
26+
27+
@Override
28+
public String getTemplate() {
29+
return TEMPLATE;
30+
}
31+
32+
@Override
33+
public Language getLanguage() {
34+
return GraphQLLanguage.INSTANCE;
35+
}
36+
}

tests/com/magento/idea/magento2plugin/inspections/graphqls/SchemaResolverInspectionTest.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.inspections.graphqls;
67

7-
import com.magento.idea.magento2plugin.magento.files.GraphQLSchema;
8+
import com.magento.idea.magento2plugin.magento.files.SchemaGraphQLsFile;
89

910
public class SchemaResolverInspectionTest extends InspectionGraphqlsFixtureTestCase {
1011

@@ -22,24 +23,36 @@ protected boolean isWriteActionRequired() {
2223
return false;
2324
}
2425

26+
/**
27+
* Inspection with valid schema resolver.
28+
*/
2529
public void testWithValidSchemaResolverInterface() throws Exception {
26-
myFixture.configureByFile(getFixturePath(GraphQLSchema.FILE_NAME));
30+
myFixture.configureByFile(getFixturePath(SchemaGraphQLsFile.FILE_NAME));
2731
assertHasNoHighlighting(errorMessage);
2832
}
2933

34+
/**
35+
* Inspection with invalid schema resolver.
36+
*/
3037
public void testWithInvalidSchemaResolverInterface() throws Exception {
31-
myFixture.configureByFile(getFixturePath(GraphQLSchema.FILE_NAME));
38+
myFixture.configureByFile(getFixturePath(SchemaGraphQLsFile.FILE_NAME));
3239
assertHasHighlighting(errorMessage);
3340
}
3441

42+
/**
43+
* Inspection with valid batch resolver.
44+
*/
3545
public void testWithValidBatchResolverInterface() throws Exception {
36-
myFixture.configureByFile(getFixturePath(GraphQLSchema.FILE_NAME));
46+
myFixture.configureByFile(getFixturePath(SchemaGraphQLsFile.FILE_NAME));
3747

3848
assertHasNoHighlighting(errorMessage);
3949
}
4050

51+
/**
52+
* Inspection with valid batch service contract resolver.
53+
*/
4154
public void testWithValidBatchServiceContractResolverInterface() throws Exception {
42-
myFixture.configureByFile(getFixturePath(GraphQLSchema.FILE_NAME));
55+
myFixture.configureByFile(getFixturePath(SchemaGraphQLsFile.FILE_NAME));
4356

4457
assertHasNoHighlighting(errorMessage);
4558
}

0 commit comments

Comments
 (0)