Skip to content

Commit f24d7e9

Browse files
author
SilinMykola
committed
1105 add graphQL schema file creation action
1 parent f26b82e commit f24d7e9

File tree

7 files changed

+220
-2
lines changed

7 files changed

+220
-2
lines changed

resources/META-INF/plugin.xml

+2
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.
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>
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.GraphQLSchema;
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, GraphQLSchema.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.generator;
7+
8+
import com.intellij.openapi.project.Project;
9+
import com.intellij.psi.PsiFile;
10+
import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateGraphQLSchema;
11+
import java.util.Properties;
12+
13+
public class GraphQLSchemaGenerator extends FileGenerator {
14+
15+
private final String moduleName;
16+
17+
private final FindOrCreateGraphQLSchema findOrCreateGraphQlSchema;
18+
19+
20+
/**
21+
* Constructor for graphQL schema file generator.
22+
*
23+
* @param moduleName String
24+
* @param project String
25+
*/
26+
public GraphQLSchemaGenerator(
27+
final String moduleName,
28+
final Project project) {
29+
super(project);
30+
this.moduleName = moduleName;
31+
findOrCreateGraphQlSchema = new FindOrCreateGraphQLSchema(project);
32+
}
33+
34+
@Override
35+
public PsiFile generate(final String actionName) {
36+
final PsiFile graphQLSchema = findOrCreateGraphQlSchema.execute(
37+
actionName,
38+
moduleName
39+
);
40+
41+
if (graphQLSchema == null) {
42+
return null;
43+
}
44+
return graphQLSchema;
45+
}
46+
47+
@Override
48+
protected void fillAttributes(final Properties attributes) {} //NOPMD
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.generator.util;
7+
8+
import com.intellij.openapi.project.Project;
9+
import com.intellij.psi.PsiDirectory;
10+
import com.intellij.psi.PsiFile;
11+
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
12+
import com.magento.idea.magento2plugin.magento.files.GraphQLSchema;
13+
import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface;
14+
import com.magento.idea.magento2plugin.magento.packages.Areas;
15+
import com.magento.idea.magento2plugin.magento.packages.Package;
16+
import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil;
17+
import java.util.Properties;
18+
19+
public final class FindOrCreateGraphQLSchema {
20+
21+
private final Project project;
22+
23+
public FindOrCreateGraphQLSchema(final Project project) {
24+
this.project = project;
25+
}
26+
27+
/**
28+
* Finds or creates graphQL schema file.
29+
*
30+
* @param actionName String
31+
* @param moduleName String
32+
*
33+
* @return PsiFile
34+
*/
35+
public PsiFile execute(final String actionName, final String moduleName) {
36+
PsiDirectory parentDirectory = new ModuleIndex(project)
37+
.getModuleDirectoryByModuleName(moduleName);
38+
39+
if (parentDirectory == null) {
40+
return null;
41+
}
42+
final DirectoryGenerator directoryGenerator = DirectoryGenerator.getInstance();
43+
final FileFromTemplateGenerator fileFromTemplateGenerator =
44+
new FileFromTemplateGenerator(project);
45+
parentDirectory = directoryGenerator
46+
.findOrCreateSubdirectory(parentDirectory, Package.moduleBaseAreaDir);
47+
final GraphQLSchema graphQlSchemaFile = GraphQLSchema.getInstance();
48+
PsiFile graphQlSchema = FileBasedIndexUtil.findModuleConfigFile(
49+
graphQlSchemaFile.getFileName(),
50+
Areas.getAreaByString(Areas.base.name()),
51+
moduleName,
52+
project
53+
);
54+
55+
if (graphQlSchema == null) {
56+
graphQlSchema = fileFromTemplateGenerator.generate(
57+
(ModuleFileInterface) graphQlSchema,
58+
new Properties(),
59+
parentDirectory,
60+
actionName
61+
);
62+
}
63+
return graphQlSchema;
64+
}
65+
}

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

+29-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,35 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.magento.files;
67

7-
public class GraphQLSchema {
8-
public static String FILE_NAME = "schema.graphqls";
8+
import com.intellij.lang.Language;
9+
import com.jetbrains.php.lang.PhpLanguage;
10+
11+
public class GraphQLSchema 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 GraphQLSchema INSTANCE = new GraphQLSchema();
17+
18+
public static GraphQLSchema 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 PhpLanguage.INSTANCE;
35+
}
936
}

0 commit comments

Comments
 (0)