Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1105 add graphQL schema file creation action #1123

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<separator/>
<!-- Context dependent actions -->
<action id="MagentoCreateAclFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewAclXmlAction"/>
<action id="MagentoCreateGraphQlSchemaFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewGraphQLSchemaAction"/>
<action id="MagentoCreateConfigFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewConfigXmlAction"/>
<action id="MagentoCreateCrontabFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewCrontabXmlAction"/>
<action id="MagentoCreateDiFile" class="com.magento.idea.magento2plugin.actions.context.xml.NewDiXmlAction"/>
Expand Down Expand Up @@ -626,6 +627,7 @@
<internalFileTemplate name="Magento Routes XML"/>
<internalFileTemplate name="Magento Layout XML"/>
<internalFileTemplate name="Magento ACL XML"/>
<internalFileTemplate name="Magento GraphQL Schema"/>
<internalFileTemplate name="Magento Collection Class"/>
<internalFileTemplate name="Magento Model Class"/>
<internalFileTemplate name="Magento Resource Model Class"/>
Expand Down
Empty file.
18 changes: 18 additions & 0 deletions resources/fileTemplates/internal/Magento GraphQL Schema.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html lang="en">
<body>
<font face="verdana" size="-1">
<p>
The schema.graphqls file defines the basic structure of GraphQL queries and mutations.
</p>
<p>
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>.
</p>
</font>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you use this generator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bohdan-harniuk Hi! Thanks for your review. I fixed. Please, check. Thanks!


private final String moduleName;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove unnecessary empty line.

private final FindOrCreateGraphQLSchema findOrCreateGraphQlSchema;


/**
* Constructor for graphQL schema file generator.
*
* @param moduleName String
* @param project String
*/
public GraphQLSchemaGenerator(
final String moduleName,
final Project project) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, reformat constructor:

Suggested change
final Project project) {
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
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All file models should have suffix File

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use PhpLanguage.INSTANCE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
}