-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IJ Plugin] Add inspection to warn about the presence of a GraphQL co…
…nfig file (#5908) * Add inspection to warn about the presence of a GraphQL config file * Add v3 -> v4 migration to delete GraphQL config files * Add tests * Tweak wording * Change severity to warning and tweak messages * Fix test * Update intellij-plugin/src/main/resources/inspectionDescriptions/ApolloGraphQLConfigFilePresent.html Co-authored-by: Martin Bonnin <martin@mbonnin.net> --------- Co-authored-by: Martin Bonnin <martin@mbonnin.net>
- Loading branch information
1 parent
d160e98
commit 3bd84c7
Showing
11 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
.../kotlin/com/apollographql/ijplugin/inspection/ApolloGraphQLConfigFilePresentInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.apollographql.ijplugin.inspection | ||
|
||
import com.apollographql.ijplugin.ApolloBundle | ||
import com.apollographql.ijplugin.project.apolloProjectService | ||
import com.apollographql.ijplugin.settings.projectSettingsState | ||
import com.apollographql.ijplugin.telemetry.TelemetryEvent | ||
import com.apollographql.ijplugin.telemetry.telemetryService | ||
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo | ||
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils | ||
import com.intellij.codeInspection.LocalInspectionTool | ||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.lang.annotation.AnnotationHolder | ||
import com.intellij.lang.annotation.Annotator | ||
import com.intellij.lang.annotation.HighlightSeverity | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.Condition | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiElementVisitor | ||
|
||
private val graphQLConfigFileNames = setOf( | ||
"graphql.config.json", | ||
"graphql.config.js", | ||
"graphql.config.cjs", | ||
"graphql.config.ts", | ||
"graphql.config.yaml", | ||
"graphql.config.yml", | ||
".graphqlrc", | ||
".graphqlrc.json", | ||
".graphqlrc.yaml", | ||
".graphqlrc.yml", | ||
".graphqlrc.js", | ||
".graphqlrc.ts" | ||
) | ||
|
||
class ApolloGraphQLConfigFilePresentInspection : LocalInspectionTool() { | ||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object : PsiElementVisitor() { | ||
override fun visitElement(element: PsiElement) { | ||
if (!element.project.apolloProjectService.apolloVersion.isAtLeastV4 || !element.project.projectSettingsState.contributeConfigurationToGraphqlPlugin) return | ||
if (element.containingFile.name in graphQLConfigFileNames && element.containingFile == element) { | ||
holder.registerProblem(element, ApolloBundle.message("inspection.graphQLConfigFilePresent.reportText"), ApolloGraphQLConfigFilePresentQuickFix(element.containingFile.name)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private class ApolloGraphQLConfigFilePresentQuickFix(private val fileName: String) : LocalQuickFix { | ||
override fun getName(): String { | ||
return ApolloBundle.message("inspection.graphQLConfigFilePresent.quickFix", fileName) | ||
} | ||
|
||
override fun getFamilyName() = name | ||
|
||
override fun availableInBatchMode() = false | ||
|
||
override fun generatePreview(project: Project, previewDescriptor: ProblemDescriptor): IntentionPreviewInfo = IntentionPreviewInfo.EMPTY | ||
|
||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { | ||
if (!IntentionPreviewUtils.isIntentionPreviewActive()) project.telemetryService.logEvent(TelemetryEvent.ApolloIjGraphQLConfigFilePresentQuickFix()) | ||
val psiFile = descriptor.psiElement.containingFile | ||
psiFile.virtualFile.delete(this) | ||
} | ||
} | ||
|
||
class ApolloGraphQLConfigFilePresentAnnotator : Annotator { | ||
override fun annotate(element: PsiElement, holder: AnnotationHolder) { | ||
if (!element.project.apolloProjectService.apolloVersion.isAtLeastV4 || !element.project.projectSettingsState.contributeConfigurationToGraphqlPlugin) return | ||
if (element.containingFile.name in graphQLConfigFileNames && element.containingFile == element) { | ||
holder.newAnnotation(HighlightSeverity.WARNING, ApolloBundle.message("inspection.graphQLConfigFilePresent.reportText")) | ||
.range(element) | ||
.create() | ||
} | ||
} | ||
} | ||
|
||
class GraphQLConfigFileFilter : Condition<VirtualFile> { | ||
override fun value(virtualFile: VirtualFile): Boolean { | ||
return virtualFile.name in graphQLConfigFileNames | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
.../com/apollographql/ijplugin/refactoring/migration/v3tov4/item/RemoveGraphqlConfigFiles.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.apollographql.ijplugin.refactoring.migration.v3tov4.item | ||
|
||
import com.apollographql.ijplugin.refactoring.migration.item.DeletesElements | ||
import com.apollographql.ijplugin.refactoring.migration.item.MigrationItem | ||
import com.apollographql.ijplugin.refactoring.migration.item.MigrationItemUsageInfo | ||
import com.apollographql.ijplugin.refactoring.migration.item.toMigrationItemUsageInfo | ||
import com.apollographql.ijplugin.util.findPsiFilesByName | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiMigration | ||
import com.intellij.psi.search.GlobalSearchScope | ||
|
||
private val graphQLConfigFileNames = setOf( | ||
"graphql.config.json", | ||
"graphql.config.js", | ||
"graphql.config.cjs", | ||
"graphql.config.ts", | ||
"graphql.config.yaml", | ||
"graphql.config.yml", | ||
".graphqlrc", | ||
".graphqlrc.json", | ||
".graphqlrc.yaml", | ||
".graphqlrc.yml", | ||
".graphqlrc.js", | ||
".graphqlrc.ts" | ||
) | ||
|
||
object RemoveGraphqlConfigFiles : MigrationItem(), DeletesElements { | ||
override fun findUsages(project: Project, migration: PsiMigration, searchScope: GlobalSearchScope): List<MigrationItemUsageInfo> { | ||
return graphQLConfigFileNames.flatMap { | ||
project.findPsiFilesByName(it, searchScope) | ||
}.map { it.toMigrationItemUsageInfo() } | ||
} | ||
|
||
override fun performRefactoring(project: Project, migration: PsiMigration, usage: MigrationItemUsageInfo) { | ||
usage.element.delete() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...llij-plugin/src/main/resources/inspectionDescriptions/ApolloGraphQLConfigFilePresent.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<html> | ||
<body> | ||
Suggests to delete GraphQL config files on projects using Apollo Kotlin 4. | ||
<p> | ||
It is no longer useful to have a GraphQL config file, as the Apollo plugin automatically provides the location of your | ||
project's operations and schema files to the GraphQL plugin, <br> | ||
</p> | ||
<p> | ||
Note: the Apollo plugin uses the Gradle tooling API to retrieve the project's GraphQL configuration. | ||
</p> | ||
<p> | ||
<a href="https://www.apollographql.com/docs/kotlin/v4/testing/android-studio-plugin#integration-with-the-graphql-intellij-plugin">More information</a> | ||
</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...lin/com/apollographql/ijplugin/inspection/ApolloGraphQLConfigFilePresentInspectionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.apollographql.ijplugin.inspection | ||
|
||
import com.apollographql.ijplugin.ApolloTestCase | ||
import com.intellij.testFramework.TestDataPath | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
|
||
@TestDataPath("\$CONTENT_ROOT/testData/inspection") | ||
@RunWith(JUnit4::class) | ||
class ApolloGraphQLConfigFilePresentInspectionTest : ApolloTestCase() { | ||
|
||
override fun getTestDataPath() = "src/test/testData/inspection" | ||
|
||
@Throws(Exception::class) | ||
override fun setUp() { | ||
super.setUp() | ||
myFixture.enableInspections(ApolloGraphQLConfigFilePresentInspection()) | ||
} | ||
|
||
@Test | ||
fun testGraphqlConfigPresenceError() { | ||
myFixture.configureByFile("graphql.config.yml") | ||
checkHighlighting() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
intellij-plugin/src/test/testData/inspection/graphql.config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<warning descr="The Apollo plugin retrieves the GraphQL configuration from Gradle and doesn't use the GraphQL config file" textAttributesKey="WARNING_ATTRIBUTES"><warning descr="The Apollo plugin retrieves the GraphQL configuration from Gradle and doesn't use the GraphQL config file" textAttributesKey="WARNING_ATTRIBUTES"> | ||
projects: | ||
main: | ||
schema: src/main/graphql/schema.graphqls | ||
documents: '**/*.graphql' | ||
extensions: | ||
endpoints: | ||
main: "https://apollo-fullstack-tutorial.herokuapp.com/graphql" | ||
apollo-key: ${APOLLO_KEY} | ||
|
||
</warning></warning> |
8 changes: 8 additions & 0 deletions
8
intellij-plugin/src/test/testData/migration/v3-to-v4/graphql.config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
projects: | ||
main: | ||
schema: src/main/graphql/schema.graphqls | ||
documents: '**/*.graphql' | ||
extensions: | ||
endpoints: | ||
main: "https://apollo-fullstack-tutorial.herokuapp.com/graphql" | ||
apollo-key: ${APOLLO_KEY} |