From 26b4206a299b8a9d3e5f8fd074d04aba17b8e723 Mon Sep 17 00:00:00 2001 From: SilinMykola Date: Thu, 30 Jun 2022 14:49:20 +0300 Subject: [PATCH 1/3] 1105 add graphQL schema file creation action --- resources/META-INF/plugin.xml | 2 + .../Magento GraphQL Schema.graphqls.ft | 0 .../internal/Magento GraphQL Schema.html | 18 +++++ .../context/xml/NewGraphQLSchemaAction.java | 57 ++++++++++++++++ .../generator/GraphQLSchemaGenerator.java | 49 ++++++++++++++ .../util/FindOrCreateGraphQLSchema.java | 65 +++++++++++++++++++ .../magento/files/GraphQLSchema.java | 31 ++++++++- 7 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 resources/fileTemplates/internal/Magento GraphQL Schema.graphqls.ft create mode 100644 resources/fileTemplates/internal/Magento GraphQL Schema.html create mode 100644 src/com/magento/idea/magento2plugin/actions/context/xml/NewGraphQLSchemaAction.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/GraphQLSchemaGenerator.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 5e59edae9..a75fbd09a 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -60,6 +60,7 @@ + @@ -626,6 +627,7 @@ + diff --git a/resources/fileTemplates/internal/Magento GraphQL Schema.graphqls.ft b/resources/fileTemplates/internal/Magento GraphQL Schema.graphqls.ft new file mode 100644 index 000000000..e69de29bb diff --git a/resources/fileTemplates/internal/Magento GraphQL Schema.html b/resources/fileTemplates/internal/Magento GraphQL Schema.html new file mode 100644 index 000000000..cc330759a --- /dev/null +++ b/resources/fileTemplates/internal/Magento GraphQL Schema.html @@ -0,0 +1,18 @@ + + + + +

+ The schema.graphqls file defines the basic structure of GraphQL queries and mutations. +

+

+ Read more about Define the GraphQL schema for a module. +

+
+ + diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewGraphQLSchemaAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewGraphQLSchemaAction.java new file mode 100644 index 000000000..69ef4ffcd --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewGraphQLSchemaAction.java @@ -0,0 +1,57 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.context.xml; + +import com.intellij.ide.fileTemplates.actions.AttributesDefaults; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.context.AbstractContextAction; +import com.magento.idea.magento2plugin.magento.files.GraphQLSchema; +import com.magento.idea.magento2plugin.magento.packages.ComponentType; +import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; +import org.jetbrains.annotations.NotNull; + + +public class NewGraphQLSchemaAction extends AbstractContextAction { + + public static final String ACTION_NAME = "Magento 2 GraphQL Schema File"; + public static final String ACTION_DESCRIPTION = "Create a new Magento 2 schema.graphqls file"; + + /** + * New schema.graphqls file action constructor. + */ + public NewGraphQLSchemaAction() { + super(ACTION_NAME, ACTION_DESCRIPTION, GraphQLSchema.getInstance()); + } + + @Override + protected boolean isVisible( + final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, + final PsiDirectory targetDirectory, + final PsiFile targetFile + ) { + final PsiDirectory configDir = moduleData.getConfigDir(); + + if (configDir == null) { + return false; + } + + return targetDirectory.getName().equals(Package.moduleBaseAreaDir) + && targetDirectory.equals(configDir) + && moduleData.getType().equals(ComponentType.module); + } + + @Override + protected AttributesDefaults getProperties( + final @NotNull AttributesDefaults defaults, + final @NotNull GetMagentoModuleUtil.MagentoModuleData moduleData, + final PsiDirectory targetDirectory, + final PsiFile targetFile + ) { + return defaults; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/GraphQLSchemaGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/GraphQLSchemaGenerator.java new file mode 100644 index 000000000..08f881f21 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/GraphQLSchemaGenerator.java @@ -0,0 +1,49 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateGraphQLSchema; +import java.util.Properties; + +public class GraphQLSchemaGenerator extends FileGenerator { + + private final String moduleName; + + private final FindOrCreateGraphQLSchema findOrCreateGraphQlSchema; + + + /** + * Constructor for graphQL schema file generator. + * + * @param moduleName String + * @param project String + */ + public GraphQLSchemaGenerator( + final String moduleName, + final Project project) { + super(project); + this.moduleName = moduleName; + findOrCreateGraphQlSchema = new FindOrCreateGraphQLSchema(project); + } + + @Override + public PsiFile generate(final String actionName) { + final PsiFile graphQLSchema = findOrCreateGraphQlSchema.execute( + actionName, + moduleName + ); + + if (graphQLSchema == null) { + return null; + } + return graphQLSchema; + } + + @Override + protected void fillAttributes(final Properties attributes) {} //NOPMD +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java new file mode 100644 index 000000000..8e4270291 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java @@ -0,0 +1,65 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator.util; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.indexes.ModuleIndex; +import com.magento.idea.magento2plugin.magento.files.GraphQLSchema; +import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface; +import com.magento.idea.magento2plugin.magento.packages.Areas; +import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil; +import java.util.Properties; + +public final class FindOrCreateGraphQLSchema { + + private final Project project; + + public FindOrCreateGraphQLSchema(final Project project) { + this.project = project; + } + + /** + * Finds or creates graphQL schema file. + * + * @param actionName String + * @param moduleName String + * + * @return PsiFile + */ + public PsiFile execute(final String actionName, final String moduleName) { + PsiDirectory parentDirectory = new ModuleIndex(project) + .getModuleDirectoryByModuleName(moduleName); + + if (parentDirectory == null) { + return null; + } + final DirectoryGenerator directoryGenerator = DirectoryGenerator.getInstance(); + final FileFromTemplateGenerator fileFromTemplateGenerator = + new FileFromTemplateGenerator(project); + parentDirectory = directoryGenerator + .findOrCreateSubdirectory(parentDirectory, Package.moduleBaseAreaDir); + final GraphQLSchema graphQlSchemaFile = GraphQLSchema.getInstance(); + PsiFile graphQlSchema = FileBasedIndexUtil.findModuleConfigFile( + graphQlSchemaFile.getFileName(), + Areas.getAreaByString(Areas.base.name()), + moduleName, + project + ); + + if (graphQlSchema == null) { + graphQlSchema = fileFromTemplateGenerator.generate( + (ModuleFileInterface) graphQlSchema, + new Properties(), + parentDirectory, + actionName + ); + } + return graphQlSchema; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/GraphQLSchema.java b/src/com/magento/idea/magento2plugin/magento/files/GraphQLSchema.java index 8a15c7ec2..6be9ce0dd 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/GraphQLSchema.java +++ b/src/com/magento/idea/magento2plugin/magento/files/GraphQLSchema.java @@ -2,8 +2,35 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + package com.magento.idea.magento2plugin.magento.files; -public class GraphQLSchema { - public static String FILE_NAME = "schema.graphqls"; +import com.intellij.lang.Language; +import com.jetbrains.php.lang.PhpLanguage; + +public class GraphQLSchema implements ModuleFileInterface { + + public static final String FILE_NAME = "schema.graphqls"; + + public static final String TEMPLATE = "Magento GraphQL Schema"; + private static final GraphQLSchema INSTANCE = new GraphQLSchema(); + + public static GraphQLSchema getInstance() { + return INSTANCE; + } + + @Override + public String getFileName() { + return FILE_NAME; + } + + @Override + public String getTemplate() { + return TEMPLATE; + } + + @Override + public Language getLanguage() { + return PhpLanguage.INSTANCE; + } } From 0021f42c17808d51fc5bc32af74958711fc8d27f Mon Sep 17 00:00:00 2001 From: SilinMykola Date: Fri, 1 Jul 2022 14:56:24 +0300 Subject: [PATCH 2/3] 1105 fixes after a CR --- .../context/xml/NewGraphQLSchemaAction.java | 4 +- .../generator/GraphQLSchemaGenerator.java | 49 ------------------- .../util/FindOrCreateGraphQLSchema.java | 4 +- ...hQLSchema.java => SchemaGraphQLsFile.java} | 10 ++-- .../SchemaResolverInspectionTest.java | 23 +++++++-- 5 files changed, 27 insertions(+), 63 deletions(-) delete mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/GraphQLSchemaGenerator.java rename src/com/magento/idea/magento2plugin/magento/files/{GraphQLSchema.java => SchemaGraphQLsFile.java} (66%) diff --git a/src/com/magento/idea/magento2plugin/actions/context/xml/NewGraphQLSchemaAction.java b/src/com/magento/idea/magento2plugin/actions/context/xml/NewGraphQLSchemaAction.java index 69ef4ffcd..65b37bba8 100644 --- a/src/com/magento/idea/magento2plugin/actions/context/xml/NewGraphQLSchemaAction.java +++ b/src/com/magento/idea/magento2plugin/actions/context/xml/NewGraphQLSchemaAction.java @@ -9,7 +9,7 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.actions.context.AbstractContextAction; -import com.magento.idea.magento2plugin.magento.files.GraphQLSchema; +import com.magento.idea.magento2plugin.magento.files.SchemaGraphQLsFile; import com.magento.idea.magento2plugin.magento.packages.ComponentType; import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; @@ -25,7 +25,7 @@ public class NewGraphQLSchemaAction extends AbstractContextAction { * New schema.graphqls file action constructor. */ public NewGraphQLSchemaAction() { - super(ACTION_NAME, ACTION_DESCRIPTION, GraphQLSchema.getInstance()); + super(ACTION_NAME, ACTION_DESCRIPTION, SchemaGraphQLsFile.getInstance()); } @Override diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/GraphQLSchemaGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/GraphQLSchemaGenerator.java deleted file mode 100644 index 08f881f21..000000000 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/GraphQLSchemaGenerator.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright © Magento, Inc. All rights reserved. - * See COPYING.txt for license details. - */ - -package com.magento.idea.magento2plugin.actions.generation.generator; - -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiFile; -import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateGraphQLSchema; -import java.util.Properties; - -public class GraphQLSchemaGenerator extends FileGenerator { - - private final String moduleName; - - private final FindOrCreateGraphQLSchema findOrCreateGraphQlSchema; - - - /** - * Constructor for graphQL schema file generator. - * - * @param moduleName String - * @param project String - */ - public GraphQLSchemaGenerator( - final String moduleName, - final Project project) { - super(project); - this.moduleName = moduleName; - findOrCreateGraphQlSchema = new FindOrCreateGraphQLSchema(project); - } - - @Override - public PsiFile generate(final String actionName) { - final PsiFile graphQLSchema = findOrCreateGraphQlSchema.execute( - actionName, - moduleName - ); - - if (graphQLSchema == null) { - return null; - } - return graphQLSchema; - } - - @Override - protected void fillAttributes(final Properties attributes) {} //NOPMD -} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java index 8e4270291..0ca5026bb 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java @@ -9,8 +9,8 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.magento.idea.magento2plugin.indexes.ModuleIndex; -import com.magento.idea.magento2plugin.magento.files.GraphQLSchema; import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface; +import com.magento.idea.magento2plugin.magento.files.SchemaGraphQLsFile; import com.magento.idea.magento2plugin.magento.packages.Areas; import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil; @@ -44,7 +44,7 @@ public PsiFile execute(final String actionName, final String moduleName) { new FileFromTemplateGenerator(project); parentDirectory = directoryGenerator .findOrCreateSubdirectory(parentDirectory, Package.moduleBaseAreaDir); - final GraphQLSchema graphQlSchemaFile = GraphQLSchema.getInstance(); + final SchemaGraphQLsFile graphQlSchemaFile = SchemaGraphQLsFile.getInstance(); PsiFile graphQlSchema = FileBasedIndexUtil.findModuleConfigFile( graphQlSchemaFile.getFileName(), Areas.getAreaByString(Areas.base.name()), diff --git a/src/com/magento/idea/magento2plugin/magento/files/GraphQLSchema.java b/src/com/magento/idea/magento2plugin/magento/files/SchemaGraphQLsFile.java similarity index 66% rename from src/com/magento/idea/magento2plugin/magento/files/GraphQLSchema.java rename to src/com/magento/idea/magento2plugin/magento/files/SchemaGraphQLsFile.java index 6be9ce0dd..7f929566b 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/GraphQLSchema.java +++ b/src/com/magento/idea/magento2plugin/magento/files/SchemaGraphQLsFile.java @@ -6,16 +6,16 @@ package com.magento.idea.magento2plugin.magento.files; import com.intellij.lang.Language; -import com.jetbrains.php.lang.PhpLanguage; +import com.intellij.lang.jsgraphql.GraphQLLanguage; -public class GraphQLSchema implements ModuleFileInterface { +public class SchemaGraphQLsFile implements ModuleFileInterface { public static final String FILE_NAME = "schema.graphqls"; public static final String TEMPLATE = "Magento GraphQL Schema"; - private static final GraphQLSchema INSTANCE = new GraphQLSchema(); + private static final SchemaGraphQLsFile INSTANCE = new SchemaGraphQLsFile(); - public static GraphQLSchema getInstance() { + public static SchemaGraphQLsFile getInstance() { return INSTANCE; } @@ -31,6 +31,6 @@ public String getTemplate() { @Override public Language getLanguage() { - return PhpLanguage.INSTANCE; + return GraphQLLanguage.INSTANCE; } } diff --git a/tests/com/magento/idea/magento2plugin/inspections/graphqls/SchemaResolverInspectionTest.java b/tests/com/magento/idea/magento2plugin/inspections/graphqls/SchemaResolverInspectionTest.java index 19610c8b0..a73a008ed 100644 --- a/tests/com/magento/idea/magento2plugin/inspections/graphqls/SchemaResolverInspectionTest.java +++ b/tests/com/magento/idea/magento2plugin/inspections/graphqls/SchemaResolverInspectionTest.java @@ -2,9 +2,10 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + package com.magento.idea.magento2plugin.inspections.graphqls; -import com.magento.idea.magento2plugin.magento.files.GraphQLSchema; +import com.magento.idea.magento2plugin.magento.files.SchemaGraphQLsFile; public class SchemaResolverInspectionTest extends InspectionGraphqlsFixtureTestCase { @@ -22,24 +23,36 @@ protected boolean isWriteActionRequired() { return false; } + /** + * Inspection with valid schema resolver. + */ public void testWithValidSchemaResolverInterface() throws Exception { - myFixture.configureByFile(getFixturePath(GraphQLSchema.FILE_NAME)); + myFixture.configureByFile(getFixturePath(SchemaGraphQLsFile.FILE_NAME)); assertHasNoHighlighting(errorMessage); } + /** + * Inspection with invalid schema resolver. + */ public void testWithInvalidSchemaResolverInterface() throws Exception { - myFixture.configureByFile(getFixturePath(GraphQLSchema.FILE_NAME)); + myFixture.configureByFile(getFixturePath(SchemaGraphQLsFile.FILE_NAME)); assertHasHighlighting(errorMessage); } + /** + * Inspection with valid batch resolver. + */ public void testWithValidBatchResolverInterface() throws Exception { - myFixture.configureByFile(getFixturePath(GraphQLSchema.FILE_NAME)); + myFixture.configureByFile(getFixturePath(SchemaGraphQLsFile.FILE_NAME)); assertHasNoHighlighting(errorMessage); } + /** + * Inspection with valid batch service contract resolver. + */ public void testWithValidBatchServiceContractResolverInterface() throws Exception { - myFixture.configureByFile(getFixturePath(GraphQLSchema.FILE_NAME)); + myFixture.configureByFile(getFixturePath(SchemaGraphQLsFile.FILE_NAME)); assertHasNoHighlighting(errorMessage); } From 4b1717a6eba77c8bf3411384a43a512b5001cecf Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 1 Jul 2022 16:45:14 +0300 Subject: [PATCH 3/3] 1105: Removed unnecessary service --- .../util/FindOrCreateGraphQLSchema.java | 65 ------------------- 1 file changed, 65 deletions(-) delete mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java deleted file mode 100644 index 0ca5026bb..000000000 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateGraphQLSchema.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright © Magento, Inc. All rights reserved. - * See COPYING.txt for license details. - */ - -package com.magento.idea.magento2plugin.actions.generation.generator.util; - -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiDirectory; -import com.intellij.psi.PsiFile; -import com.magento.idea.magento2plugin.indexes.ModuleIndex; -import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface; -import com.magento.idea.magento2plugin.magento.files.SchemaGraphQLsFile; -import com.magento.idea.magento2plugin.magento.packages.Areas; -import com.magento.idea.magento2plugin.magento.packages.Package; -import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil; -import java.util.Properties; - -public final class FindOrCreateGraphQLSchema { - - private final Project project; - - public FindOrCreateGraphQLSchema(final Project project) { - this.project = project; - } - - /** - * Finds or creates graphQL schema file. - * - * @param actionName String - * @param moduleName String - * - * @return PsiFile - */ - public PsiFile execute(final String actionName, final String moduleName) { - PsiDirectory parentDirectory = new ModuleIndex(project) - .getModuleDirectoryByModuleName(moduleName); - - if (parentDirectory == null) { - return null; - } - final DirectoryGenerator directoryGenerator = DirectoryGenerator.getInstance(); - final FileFromTemplateGenerator fileFromTemplateGenerator = - new FileFromTemplateGenerator(project); - parentDirectory = directoryGenerator - .findOrCreateSubdirectory(parentDirectory, Package.moduleBaseAreaDir); - final SchemaGraphQLsFile graphQlSchemaFile = SchemaGraphQLsFile.getInstance(); - PsiFile graphQlSchema = FileBasedIndexUtil.findModuleConfigFile( - graphQlSchemaFile.getFileName(), - Areas.getAreaByString(Areas.base.name()), - moduleName, - project - ); - - if (graphQlSchema == null) { - graphQlSchema = fileFromTemplateGenerator.generate( - (ModuleFileInterface) graphQlSchema, - new Properties(), - parentDirectory, - actionName - ); - } - return graphQlSchema; - } -}