-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IJ/AS plugin] Add options to ignore fields when reporting unused fie…
…lds (#5197)
- Loading branch information
Showing
8 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
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
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
68 changes: 68 additions & 0 deletions
68
intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/inspection/GraphQL.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,68 @@ | ||
package com.apollographql.ijplugin.inspection | ||
|
||
import com.intellij.lang.jsgraphql.psi.GraphQLFieldDefinition | ||
import com.intellij.lang.jsgraphql.psi.GraphQLIdentifier | ||
import com.intellij.lang.jsgraphql.psi.GraphQLInterfaceTypeDefinition | ||
import com.intellij.lang.jsgraphql.psi.GraphQLNamedTypeDefinition | ||
import com.intellij.lang.jsgraphql.psi.GraphQLObjectTypeDefinition | ||
import com.intellij.psi.util.parentOfType | ||
|
||
/** | ||
* Given a field identifier, returns the set of field coordinates that match it. | ||
* A field coordinate is a string of the form "TypeName.fieldName". | ||
* Interfaces are taken into account which is why multiple coordinates can be returned. | ||
* | ||
* For example, given the following schema: | ||
* ```graphql | ||
* interface Node { | ||
* id: ID! | ||
* } | ||
* | ||
* interface HasId { | ||
* id: ID! | ||
* } | ||
* | ||
* type User implements Node & HasId { | ||
* id: ID! | ||
* name: String! | ||
* } | ||
* ``` | ||
* | ||
* And the following query: | ||
* ```graphql | ||
* query { | ||
* user { | ||
* id | ||
* name | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* The following coordinates will be returned for `id`: | ||
* - `User.id` | ||
* - `Node.id` | ||
* - `HasId.id` | ||
*/ | ||
fun matchingFieldCoordinates(fieldIdentifier: GraphQLIdentifier): Set<String> { | ||
val fieldDefinition = fieldIdentifier.reference?.resolve()?.parent as? GraphQLFieldDefinition ?: return emptySet() | ||
val namedTypeDefinition = fieldDefinition.parentOfType<GraphQLNamedTypeDefinition>() ?: return emptySet() | ||
return matchingFieldCoordinates(fieldDefinition, namedTypeDefinition) | ||
} | ||
|
||
private fun matchingFieldCoordinates( | ||
fieldDefinition: GraphQLFieldDefinition, | ||
namedTypeDefinition: GraphQLNamedTypeDefinition, | ||
): Set<String> { | ||
val typeName = namedTypeDefinition.typeNameDefinition?.name ?: return emptySet() | ||
|
||
val fieldsDefinitions = (namedTypeDefinition as? GraphQLObjectTypeDefinition)?.fieldsDefinition | ||
?: (namedTypeDefinition as? GraphQLInterfaceTypeDefinition)?.fieldsDefinition ?: return emptySet() | ||
if (fieldsDefinitions.fieldDefinitionList.none { it.name == fieldDefinition.name }) return emptySet() | ||
|
||
val fieldCoordinates = mutableSetOf(typeName + "." + fieldDefinition.name) | ||
val implementedInterfaces = (namedTypeDefinition as? GraphQLObjectTypeDefinition)?.implementsInterfaces | ||
?: (namedTypeDefinition as? GraphQLInterfaceTypeDefinition)?.implementsInterfaces ?: return fieldCoordinates | ||
val implementedInterfaceTypeDefinitions = implementedInterfaces.typeNameList.mapNotNull { it.nameIdentifier.reference?.resolve()?.parentOfType<GraphQLInterfaceTypeDefinition>() } | ||
if (implementedInterfaceTypeDefinitions.isEmpty()) return fieldCoordinates | ||
return fieldCoordinates + implementedInterfaceTypeDefinitions.flatMap { matchingFieldCoordinates(fieldDefinition, it) } | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ query animals { | |
# noinspection ApolloUnusedField | ||
barkVolume | ||
fieldOnDogAndCat | ||
name | ||
} | ||
} | ||
} |