-
Notifications
You must be signed in to change notification settings - Fork 103
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||||||||
|
||||||||
private final String moduleName; | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, reformat constructor:
Suggested change
|
||||||||
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 |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All file models should have suffix File There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you use PhpLanguage.INSTANCE? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!