Skip to content

Commit 6026cd2

Browse files
martinbonninBoD
andauthored
Add a special comment to disable the GraphQL intelliJ plugin inspection (#5154)
* Add a special comment to disable the GraphQL intelliJ plugin inspection * Update libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo3/ast/gqldocument.kt Co-authored-by: Benoit Lubek <[email protected]> --------- Co-authored-by: Benoit Lubek <[email protected]>
1 parent c24c1c3 commit 6026cd2

File tree

10 files changed

+659
-63
lines changed

10 files changed

+659
-63
lines changed

libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo3/ast/gql.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ class GQLDirectiveDefinition(
727727
"include",
728728
"skip",
729729
"deprecated",
730+
"specifiedBy"
730731
)
731732
}
732733
}

libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo3/ast/gqldocument.kt

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.apollographql.apollo3.ast.internal.apollo_v0_1_definitionsStr
66
import com.apollographql.apollo3.ast.internal.apollo_v0_2_definitionsStr
77
import com.apollographql.apollo3.ast.internal.builtinsDefinitionsStr
88
import com.apollographql.apollo3.ast.internal.linkDefinitionsStr
9+
import okio.Buffer
910

1011
/**
1112
* Add builtin definitions from the latest spec version to the [GQLDocument]
@@ -47,7 +48,7 @@ fun apolloDefinitions() = definitionsFromString(apollo_v0_1_definitionsStr)
4748
* Extra apollo specific definitions from https://specs.apollo.dev/kotlin_labs/<[version]>
4849
*/
4950
fun apolloDefinitions(version: String): List<GQLDefinition> {
50-
return definitionsFromString(when(version) {
51+
return definitionsFromString(when (version) {
5152
"v0.1" -> apollo_v0_1_definitionsStr
5253
"v0.2" -> apollo_v0_2_definitionsStr
5354
else -> error("Apollo definitions $version are not supported")
@@ -91,7 +92,11 @@ internal enum class ConflictResolution {
9192
TakeLeft
9293
}
9394

94-
internal fun combineDefinitions(left: List<GQLDefinition>, right: List<GQLDefinition>, conflictResolution: ConflictResolution): List<GQLDefinition> {
95+
internal fun combineDefinitions(
96+
left: List<GQLDefinition>,
97+
right: List<GQLDefinition>,
98+
conflictResolution: ConflictResolution,
99+
): List<GQLDefinition> {
95100
val mergedDefinitions = left.toMutableList()
96101

97102
right.forEach { builtInTypeDefinition ->
@@ -111,6 +116,7 @@ internal fun combineDefinitions(left: List<GQLDefinition>, right: List<GQLDefini
111116

112117
return mergedDefinitions
113118
}
119+
114120
private fun GQLDocument.withDefinitions(definitions: List<GQLDefinition>): GQLDocument {
115121
return copy(
116122
definitions = combineDefinitions(this.definitions, definitions, ConflictResolution.TakeLeft)
@@ -126,9 +132,33 @@ private fun GQLDocument.withDefinitions(definitions: List<GQLDefinition>): GQLDo
126132
*/
127133
@ApolloExperimental
128134
fun GQLDocument.toSDL(indent: String = " "): String {
129-
return this.copy(
130-
definitions = definitions.filter {
131-
it !is GQLScalarTypeDefinition || it.name !in GQLTypeDefinition.builtInTypes
135+
val buffer = Buffer()
136+
val writer = SDLWriter(buffer, indent)
137+
138+
definitions.forEachIndexed { index, definition ->
139+
when {
140+
definition is GQLScalarTypeDefinition && definition.name in GQLTypeDefinition.builtInTypes -> {
141+
// Always skip scalar definitions, it's a must in the spec
142+
return@forEachIndexed
132143
}
133-
).toUtf8(indent)
144+
145+
definition is GQLTypeDefinition && definition.name in GQLTypeDefinition.builtInTypes ||
146+
definition is GQLDirectiveDefinition && definition.name in GQLDirectiveDefinition.builtInDirectives -> {
147+
// Tools like the GraphQL intelliJ plugin expect a "server" schema, without builtin types
148+
// Suppress the errors for now
149+
buffer.writeUtf8("# See https://github.com/JetBrains/js-graphql-intellij-plugin/issues/665\n")
150+
buffer.writeUtf8("# noinspection GraphQLTypeRedefinition\n")
151+
writer.write(definition)
152+
153+
}
154+
155+
else -> {
156+
writer.write(definition)
157+
}
158+
}
159+
if (index < definitions.size - 1) {
160+
writer.write("\n")
161+
}
162+
}
163+
return buffer.readUtf8()
134164
}

libraries/apollo-ast/src/jvmTest/kotlin/com/apollographql/apollo3/graphql/ast/test/ParserTest.kt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ParserTest {
1414
@Test
1515
fun parserProducesExpectedValues(@TestParameter(valuesProvider = ParametersProvider::class) graphqlFile: File) {
1616
checkExpected(graphqlFile) {
17-
graphqlFile.parseAsGQLDocument()
17+
it.parseAsGQLDocument()
1818
.issues
1919
.serialize()
2020
}
@@ -32,28 +32,6 @@ class ParserTest {
3232
}
3333
}
3434

35-
/**
36-
* run the block and checks the result against the .expected file. Will overwrite the result if required
37-
*
38-
* @param block the callback to produce the result.
39-
*/
40-
fun checkExpected(graphQLFile: File, block: () -> String) {
41-
val actual = block()
42-
43-
val expectedFile = File(graphQLFile.parent, graphQLFile.nameWithoutExtension + ".expected")
44-
val expected = try {
45-
expectedFile.readText()
46-
} catch (e: Exception) {
47-
null
48-
}
49-
50-
if (shouldUpdateTestFixtures()) {
51-
expectedFile.writeText(actual)
52-
} else {
53-
assertEquals(expected, actual)
54-
}
55-
}
56-
5735
companion object {
5836
private const val separator = "\n------------\n"
5937

@@ -77,5 +55,27 @@ class ParserTest {
7755

7856
return Regex(testFilter).containsMatchIn(value)
7957
}
58+
59+
/**
60+
* run the block and checks the result against the .expected file. Will overwrite the result if required
61+
*
62+
* @param block the callback to produce the result.
63+
*/
64+
fun checkExpected(graphQLFile: File, block: (File) -> String) {
65+
val actual = block(graphQLFile)
66+
67+
val expectedFile = File(graphQLFile.parent, graphQLFile.nameWithoutExtension + ".expected")
68+
val expected = try {
69+
expectedFile.readText()
70+
} catch (e: Exception) {
71+
null
72+
}
73+
74+
if (shouldUpdateTestFixtures()) {
75+
expectedFile.writeText(actual)
76+
} else {
77+
assertEquals(expected, actual)
78+
}
79+
}
8080
}
8181
}
Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,39 @@
11
package com.apollographql.apollo3.graphql.ast.test
22

3-
import com.apollographql.apollo3.ast.GQLDocument
3+
import com.apollographql.apollo3.ast.introspection.toSchema
44
import com.apollographql.apollo3.ast.parseAsGQLDocument
55
import com.apollographql.apollo3.ast.toSDL
6+
import com.apollographql.apollo3.ast.withBuiltinDefinitions
7+
import com.apollographql.apollo3.graphql.ast.test.ParserTest.Companion.checkExpected
8+
import java.io.File
69
import kotlin.test.Test
7-
import kotlin.test.assertEquals
810

911
class SDLWriterTest {
1012
@Test
1113
fun simpleTest() {
12-
val schemaString = """
13-
schema {
14-
query: Query
15-
}
16-
17-
type Query {
18-
foo: Int
19-
}
20-
21-
interface A {
22-
a: String
23-
}
24-
25-
interface B {
26-
b: String
27-
}
28-
29-
type C implements A & B {
30-
a: String
31-
32-
b: String
33-
}
34-
35-
interface D implements A & B {
36-
a: String
37-
38-
b: String
39-
}
14+
val sdlSchema = File("${CWD}/test-fixtures/sdl/simple.graphqls")
4015

41-
""".trimIndent()
16+
checkExpected(sdlSchema) {
17+
it.parseAsGQLDocument().getOrThrow().toSDL(" ")
18+
}
19+
}
20+
21+
@Test
22+
fun typeRedefinitionInspectionIsIgnored() {
23+
val sdlSchema = File("${CWD}/test-fixtures/sdl/type_redefinitions.graphqls")
24+
25+
checkExpected(sdlSchema) {
26+
it.parseAsGQLDocument().getOrThrow().withBuiltinDefinitions().toSDL(" ")
27+
}
28+
}
29+
30+
@Test
31+
fun introspectionSchema() {
32+
val sdlSchema = File("${CWD}/test-fixtures/sdl/introspection.json")
4233

43-
val schema: GQLDocument = schemaString.parseAsGQLDocument().getOrThrow()
44-
val sdl = schema.toSDL(" ")
45-
assertEquals(schemaString, sdl)
34+
checkExpected(sdlSchema) {
35+
it.toSchema().toGQLDocument().toSDL(" ")
36+
}
4637
}
4738

4839
@Test

0 commit comments

Comments
 (0)