Skip to content

Commit 3eb9832

Browse files
authored
Add integration tests and fix a few codegen errors (#34)
1 parent a6e7cea commit 3eb9832

File tree

10 files changed

+372
-6
lines changed

10 files changed

+372
-6
lines changed

.github/workflows/build-pull-request.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ jobs:
1111
- uses: gradle/actions/setup-gradle@dbbdc275be76ac10734476cc723d82dfe7ec6eda #v3.4.2
1212
- run: |
1313
./gradlew build
14+
./gradlew -p execution-tests build
1415
./gradlew -p sample-ktor build
1516
./gradlew -p sample-http4k build

apollo-execution-processor/src/main/kotlin/com/apollographql/execution/processor/codegen/SchemaDocumentBuilder.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.apollographql.execution.processor.codegen
22

33
import com.apollographql.apollo.ast.*
4+
import com.apollographql.execution.processor.codegen.KotlinSymbols.AstArgument
45
import com.apollographql.execution.processor.codegen.KotlinSymbols.AstBooleanValue
56
import com.apollographql.execution.processor.codegen.KotlinSymbols.AstDirective
67
import com.apollographql.execution.processor.codegen.KotlinSymbols.AstDirectiveDefinition
@@ -45,6 +46,7 @@ import com.squareup.kotlinpoet.KModifier
4546
import com.squareup.kotlinpoet.MemberName
4647
import com.squareup.kotlinpoet.PropertySpec
4748
import com.squareup.kotlinpoet.joinToCode
49+
import com.squareup.kotlinpoet.withIndent
4850

4951
internal class SchemaDocumentBuilder(
5052
val context: KotlinExecutableSchemaContext,
@@ -185,7 +187,7 @@ private fun SirDirectiveDefinition.codeBlock(): CodeBlock {
185187
}
186188

187189
private fun GQLDirectiveLocation.codeBlock(): CodeBlock {
188-
return CodeBlock.of("%T", ClassName("com.apollographql.apollo.ast", name))
190+
return CodeBlock.of("%T", ClassName("com.apollographql.apollo.ast", "GQLDirectiveLocation", name))
189191
}
190192

191193
private fun SirObjectDefinition.codeBlock(): CodeBlock {
@@ -273,11 +275,34 @@ private fun buildCommon(
273275
}
274276

275277
private fun SirDirective.codeBlock(): CodeBlock {
278+
return buildCode {
279+
add("%T(\n", AstDirective)
280+
withIndent {
281+
add("null,\n")
282+
add("%S,\n", name)
283+
add("listOf(\n")
284+
withIndent {
285+
arguments.forEach {
286+
add("%L,\n", it.codeBlock())
287+
}
288+
}
289+
add(")\n")
290+
}
291+
add(")\n")
292+
}
276293
return CodeBlock.of("%T(null, %S, %L)", AstDirective, name, arguments.map { it.codeBlock() }.joinToCode(prefix = "listOf(", suffix = ")"))
277294
}
278295

279296
private fun SirArgument.codeBlock(): CodeBlock {
280-
return value.codeBlock()
297+
return buildCode {
298+
add("%T(\n", AstArgument)
299+
withIndent {
300+
add("null,\n")
301+
add("%S,\n", name)
302+
add("%L,\n", value.codeBlock())
303+
}
304+
add(")\n")
305+
}
281306
}
282307

283308
private fun GQLValue.codeBlock(): CodeBlock {

apollo-execution-processor/src/main/kotlin/com/apollographql/execution/processor/codegen/resolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal fun resolverBody(sirObjectDefinition: SirObjectDefinition, sirTargetFie
1616
if (sirTargetField.isFunction) {
1717
add("(\n")
1818
indent {
19-
add(sirTargetField.arguments.map { argumentCodeBlock(it) }.joinToCode(",\n", suffix = ",\n"))
19+
add(sirTargetField.arguments.map { argumentCodeBlock(it) }.joinToCode(",\n"))
2020
}
2121
add(")\n")
2222
}

apollo-execution-processor/src/main/kotlin/com/apollographql/execution/processor/definitions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ private class TypeDefinitionContext(
298298
private fun KSAnnotation.toSirDirective(directiveDefinition: SirDirectiveDefinition): SirDirective {
299299
return SirDirective(
300300
name = directiveDefinition.name,
301-
arguments = arguments.mapNotNull { it.toSirArgument(directiveDefinition) }
301+
arguments = arguments.mapNotNull { it.SirInputValueDefinition(directiveDefinition) }
302302
)
303303
}
304304

305-
private fun KSValueArgument.toSirArgument(directiveDefinition: SirDirectiveDefinition): SirArgument? {
305+
private fun KSValueArgument.SirInputValueDefinition(directiveDefinition: SirDirectiveDefinition): SirArgument? {
306306
val kotlinName = name?.asString()
307307
if (kotlinName == null) {
308308
logger.error("Arguments must be named", this)

apollo-execution-processor/src/main/kotlin/com/apollographql/execution/processor/sir/ServerIr.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ internal class SirDirective(
8484
val arguments: List<SirArgument>
8585
)
8686

87+
/**
88+
* The value of an argument in a directive
89+
*/
8790
internal class SirArgument(
8891
val name: String,
8992
val value: GQLValue

execution-tests/directives/graphql/schema.graphqls

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,157 @@ enum OptInLevel {
2525

2626
Error
2727
}
28+
29+
type __Schema {
30+
description: String
31+
32+
types: [__Type!]!
33+
34+
queryType: __Type!
35+
36+
mutationType: __Type
37+
38+
subscriptionType: __Type
39+
40+
directives: [__Directive!]!
41+
}
42+
43+
type __Type {
44+
kind: __TypeKind!
45+
46+
name: String
47+
48+
description: String
49+
50+
fields(includeDeprecated: Boolean = false): [__Field!]
51+
52+
interfaces: [__Type!]
53+
54+
possibleTypes: [__Type!]
55+
56+
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
57+
58+
inputFields(includeDeprecated: Boolean = false): [__InputValue!]
59+
60+
ofType: __Type
61+
62+
specifiedByURL: String
63+
}
64+
65+
enum __TypeKind {
66+
SCALAR
67+
68+
OBJECT
69+
70+
INTERFACE
71+
72+
UNION
73+
74+
ENUM
75+
76+
INPUT_OBJECT
77+
78+
LIST
79+
80+
NON_NULL
81+
}
82+
83+
type __Field {
84+
name: String!
85+
86+
description: String
87+
88+
args(includeDeprecated: Boolean = false): [__InputValue!]!
89+
90+
type: __Type!
91+
92+
isDeprecated: Boolean!
93+
94+
deprecationReason: String
95+
}
96+
97+
type __InputValue {
98+
name: String!
99+
100+
description: String
101+
102+
type: __Type!
103+
104+
defaultValue: String
105+
106+
isDeprecated: Boolean!
107+
108+
deprecationReason: String
109+
}
110+
111+
type __EnumValue {
112+
name: String!
113+
114+
description: String
115+
116+
isDeprecated: Boolean!
117+
118+
deprecationReason: String
119+
}
120+
121+
type __Directive {
122+
name: String!
123+
124+
description: String
125+
126+
locations: [__DirectiveLocation!]!
127+
128+
args(includeDeprecated: Boolean = false): [__InputValue!]!
129+
130+
isRepeatable: Boolean!
131+
}
132+
133+
enum __DirectiveLocation {
134+
QUERY
135+
136+
MUTATION
137+
138+
SUBSCRIPTION
139+
140+
FIELD
141+
142+
FRAGMENT_DEFINITION
143+
144+
FRAGMENT_SPREAD
145+
146+
INLINE_FRAGMENT
147+
148+
VARIABLE_DEFINITION
149+
150+
SCHEMA
151+
152+
SCALAR
153+
154+
OBJECT
155+
156+
FIELD_DEFINITION
157+
158+
ARGUMENT_DEFINITION
159+
160+
INTERFACE
161+
162+
UNION
163+
164+
ENUM
165+
166+
ENUM_VALUE
167+
168+
INPUT_OBJECT
169+
170+
INPUT_FIELD_DEFINITION
171+
}
172+
173+
directive @skip (if: Boolean!) on FIELD|FRAGMENT_SPREAD|INLINE_FRAGMENT
174+
175+
directive @include (if: Boolean!) on FIELD|FRAGMENT_SPREAD|INLINE_FRAGMENT
176+
177+
directive @deprecated (reason: String = "No longer supported") on FIELD_DEFINITION|ARGUMENT_DEFINITION|INPUT_FIELD_DEFINITION|ENUM_VALUE
178+
179+
directive @defer (label: String, if: Boolean! = true) on FRAGMENT_SPREAD|INLINE_FRAGMENT
180+
181+
directive @specifiedBy (url: String!) on SCALAR
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plugins {
2+
alias(libs.plugins.kgp.jvm)
3+
alias(libs.plugins.ksp)
4+
alias(libs.plugins.apollo.execution)
5+
}
6+
7+
apolloExecution {
8+
service("service") {
9+
packageName.set("com.example")
10+
}
11+
}
12+
13+
dependencies {
14+
implementation(libs.apollo.execution.runtime)
15+
}

0 commit comments

Comments
 (0)