Skip to content

Commit e0ad0a4

Browse files
authored
[IJ Plugin] Fix a crash when invoking the 'missing @link' quickfix (#6402)
* Fix a crash when invoking the quickfix of missing @link on a new extra.graphqls * Take containing file only if it's extra.graphqls
1 parent c786b5a commit e0ad0a4

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/inspection/ApolloMissingGraphQLDefinitionImportInspection.kt

+11-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import com.intellij.codeInspection.ProblemsHolder
3535
import com.intellij.lang.jsgraphql.psi.GraphQLArrayValue
3636
import com.intellij.lang.jsgraphql.psi.GraphQLDirective
3737
import com.intellij.lang.jsgraphql.psi.GraphQLElementFactory
38+
import com.intellij.lang.jsgraphql.psi.GraphQLFile
3839
import com.intellij.lang.jsgraphql.psi.GraphQLVisitor
3940
import com.intellij.openapi.project.Project
4041
import com.intellij.psi.PsiElementVisitor
@@ -60,7 +61,8 @@ class ApolloMissingGraphQLDefinitionImportInspection : LocalInspectionTool() {
6061
highlightType: ProblemHighlightType,
6162
) {
6263
if (directiveElement.name !in definitions.directives().map { it.name }) return
63-
val message = if (highlightType == ProblemHighlightType.WEAK_WARNING) "inspection.missingGraphQLDefinitionImport.reportText.warning" else "inspection.missingGraphQLDefinitionImport.reportText.error"
64+
val message =
65+
if (highlightType == ProblemHighlightType.WEAK_WARNING) "inspection.missingGraphQLDefinitionImport.reportText.warning" else "inspection.missingGraphQLDefinitionImport.reportText.error"
6466
if (!directiveElement.isImported(definitionsUrl)) {
6567
val typeKind = ApolloBundle.message("inspection.missingGraphQLDefinitionImport.reportText.directive")
6668
holder.registerProblem(
@@ -123,13 +125,16 @@ private class ImportDefinitionQuickFix(
123125
val linkDirective = schemaFiles.flatMap { it.linkDirectives(definitionsUrl) }.firstOrNull()
124126

125127
if (linkDirective == null) {
126-
val linkDirectiveSchemaExtension = createLinkDirectiveSchemaExtension(project, setOf(element.nameForImport), definitions, definitionsUrl)
127-
val extraSchemaFile = schemaFiles.firstOrNull { it.name == "extra.graphqls" }
128+
val linkDirectiveSchemaExtension =
129+
createLinkDirectiveSchemaExtension(project, setOf(element.nameForImport), definitions, definitionsUrl)
130+
val extraSchemaFile = (element.containingFile as? GraphQLFile)?.takeIf { it.name == "extra.graphqls" }
131+
?: schemaFiles.firstOrNull { it.name == "extra.graphqls" }
128132
if (extraSchemaFile == null) {
129133
GraphQLElementFactory.createFile(project, linkDirectiveSchemaExtension.text).also {
130134
// Save the file to the project
131135
it.name = "extra.graphqls"
132-
schemaFiles.first().containingDirectory!!.add(it)
136+
val directory = schemaFiles.firstOrNull()?.containingDirectory ?: element.containingFile.containingDirectory ?: return
137+
directory.add(it)
133138

134139
// There's a new schema file, reload the configuration
135140
project.gradleToolingModelService.triggerFetchToolingModels()
@@ -140,7 +145,8 @@ private class ImportDefinitionQuickFix(
140145
}
141146
} else {
142147
val importedNames = buildSet {
143-
addAll(linkDirective.arguments!!.argumentList.firstOrNull { it.name == "import" }?.value?.cast<GraphQLArrayValue>()?.valueList.orEmpty().map { it.text.unquoted() })
148+
addAll(linkDirective.arguments!!.argumentList.firstOrNull { it.name == "import" }?.value?.cast<GraphQLArrayValue>()?.valueList.orEmpty()
149+
.map { it.text.unquoted() })
144150
add(element.nameForImport)
145151
}
146152
linkDirective.replace(createLinkDirective(project, importedNames, definitions, definitionsUrl))

0 commit comments

Comments
 (0)