|
| 1 | +package com.apollographql.ijplugin.graphql |
| 2 | + |
| 3 | +import com.intellij.lang.jsgraphql.ide.validation.GraphQLErrorFilter |
| 4 | +import com.intellij.lang.jsgraphql.types.GraphQLError |
| 5 | +import com.intellij.lang.jsgraphql.types.language.DirectiveDefinition |
| 6 | +import com.intellij.lang.jsgraphql.types.language.EnumTypeDefinition |
| 7 | +import com.intellij.lang.jsgraphql.types.language.ObjectTypeDefinition |
| 8 | +import com.intellij.lang.jsgraphql.types.language.ScalarTypeDefinition |
| 9 | +import com.intellij.lang.jsgraphql.types.schema.idl.errors.DirectiveRedefinitionError |
| 10 | +import com.intellij.lang.jsgraphql.types.schema.idl.errors.TypeRedefinitionError |
| 11 | +import com.intellij.openapi.project.Project |
| 12 | +import com.intellij.psi.PsiElement |
| 13 | + |
| 14 | +/** |
| 15 | + * Suppress redefinition errors for built-in GraphQL types/directives. |
| 16 | + * |
| 17 | + * TODO: remove this once https://github.com/JetBrains/js-graphql-intellij-plugin/issues/665 is fixed |
| 18 | + */ |
| 19 | +class BuiltInRedefinitionErrorFilter : GraphQLErrorFilter { |
| 20 | + private val builtInScalarTypes = listOf("Int", "Float", "String", "Boolean", "ID") |
| 21 | + private val builtInDirectives = listOf("skip", "include", "deprecated", "defer", "specifiedBy") |
| 22 | + |
| 23 | + override fun isGraphQLErrorSuppressed(project: Project, error: GraphQLError, element: PsiElement?): Boolean { |
| 24 | + return when (error) { |
| 25 | + is TypeRedefinitionError -> { |
| 26 | + when (val node = error.node) { |
| 27 | + is ScalarTypeDefinition -> { |
| 28 | + node.name in builtInScalarTypes |
| 29 | + } |
| 30 | + |
| 31 | + is ObjectTypeDefinition -> { |
| 32 | + node.name.startsWith("__") |
| 33 | + } |
| 34 | + |
| 35 | + is EnumTypeDefinition -> { |
| 36 | + node.name.startsWith("__") |
| 37 | + } |
| 38 | + |
| 39 | + else -> false |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + is DirectiveRedefinitionError -> { |
| 44 | + (error.node as DirectiveDefinition).name in builtInDirectives |
| 45 | + } |
| 46 | + |
| 47 | + else -> false |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments