Skip to content

Commit

Permalink
Add integration tests and fix a few codegen errors
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Oct 20, 2024
1 parent a6e7cea commit 561205a
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ jobs:
- uses: gradle/actions/setup-gradle@dbbdc275be76ac10734476cc723d82dfe7ec6eda #v3.4.2
- run: |
./gradlew build
./gradlew -p execution-tests build
./gradlew -p sample-ktor build
./gradlew -p sample-http4k build
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.apollographql.execution.processor.codegen

import com.apollographql.apollo.ast.*
import com.apollographql.execution.processor.codegen.KotlinSymbols.AstArgument
import com.apollographql.execution.processor.codegen.KotlinSymbols.AstBooleanValue
import com.apollographql.execution.processor.codegen.KotlinSymbols.AstDirective
import com.apollographql.execution.processor.codegen.KotlinSymbols.AstDirectiveDefinition
Expand Down Expand Up @@ -45,6 +46,7 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.joinToCode
import com.squareup.kotlinpoet.withIndent

internal class SchemaDocumentBuilder(
val context: KotlinExecutableSchemaContext,
Expand Down Expand Up @@ -185,7 +187,7 @@ private fun SirDirectiveDefinition.codeBlock(): CodeBlock {
}

private fun GQLDirectiveLocation.codeBlock(): CodeBlock {
return CodeBlock.of("%T", ClassName("com.apollographql.apollo.ast", name))
return CodeBlock.of("%T", ClassName("com.apollographql.apollo.ast", "GQLDirectiveLocation", name))
}

private fun SirObjectDefinition.codeBlock(): CodeBlock {
Expand Down Expand Up @@ -273,11 +275,34 @@ private fun buildCommon(
}

private fun SirDirective.codeBlock(): CodeBlock {
return buildCode {
add("%T(\n", AstDirective)
withIndent {
add("null,\n")
add("%S,\n", name)
add("listOf(\n")
withIndent {
arguments.forEach {
add("%L,\n", it.codeBlock())
}
}
add(")\n")
}
add(")\n")
}
return CodeBlock.of("%T(null, %S, %L)", AstDirective, name, arguments.map { it.codeBlock() }.joinToCode(prefix = "listOf(", suffix = ")"))
}

private fun SirArgument.codeBlock(): CodeBlock {
return value.codeBlock()
return buildCode {
add("%T(\n", AstArgument)
withIndent {
add("null,\n")
add("%S,\n", name)
add("%L,\n", value.codeBlock())
}
add(")\n")
}
}

private fun GQLValue.codeBlock(): CodeBlock {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal fun resolverBody(sirObjectDefinition: SirObjectDefinition, sirTargetFie
if (sirTargetField.isFunction) {
add("(\n")
indent {
add(sirTargetField.arguments.map { argumentCodeBlock(it) }.joinToCode(",\n", suffix = ",\n"))
add(sirTargetField.arguments.map { argumentCodeBlock(it) }.joinToCode(",\n"))
}
add(")\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ private class TypeDefinitionContext(
private fun KSAnnotation.toSirDirective(directiveDefinition: SirDirectiveDefinition): SirDirective {
return SirDirective(
name = directiveDefinition.name,
arguments = arguments.mapNotNull { it.toSirArgument(directiveDefinition) }
arguments = arguments.mapNotNull { it.SirInputValueDefinition(directiveDefinition) }
)
}

private fun KSValueArgument.toSirArgument(directiveDefinition: SirDirectiveDefinition): SirArgument? {
private fun KSValueArgument.SirInputValueDefinition(directiveDefinition: SirDirectiveDefinition): SirArgument? {
val kotlinName = name?.asString()
if (kotlinName == null) {
logger.error("Arguments must be named", this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ internal class SirDirective(
val arguments: List<SirArgument>
)

/**
* The value of an argument in a directive
*/
internal class SirArgument(
val name: String,
val value: GQLValue
Expand Down
154 changes: 154 additions & 0 deletions execution-tests/directives/graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,157 @@ enum OptInLevel {

Error
}

type __Schema {
description: String

types: [__Type!]!

queryType: __Type!

mutationType: __Type

subscriptionType: __Type

directives: [__Directive!]!
}

type __Type {
kind: __TypeKind!

name: String

description: String

fields(includeDeprecated: Boolean = false): [__Field!]

interfaces: [__Type!]

possibleTypes: [__Type!]

enumValues(includeDeprecated: Boolean = false): [__EnumValue!]

inputFields(includeDeprecated: Boolean = false): [__InputValue!]

ofType: __Type

specifiedByURL: String
}

enum __TypeKind {
SCALAR

OBJECT

INTERFACE

UNION

ENUM

INPUT_OBJECT

LIST

NON_NULL
}

type __Field {
name: String!

description: String

args(includeDeprecated: Boolean = false): [__InputValue!]!

type: __Type!

isDeprecated: Boolean!

deprecationReason: String
}

type __InputValue {
name: String!

description: String

type: __Type!

defaultValue: String

isDeprecated: Boolean!

deprecationReason: String
}

type __EnumValue {
name: String!

description: String

isDeprecated: Boolean!

deprecationReason: String
}

type __Directive {
name: String!

description: String

locations: [__DirectiveLocation!]!

args(includeDeprecated: Boolean = false): [__InputValue!]!

isRepeatable: Boolean!
}

enum __DirectiveLocation {
QUERY

MUTATION

SUBSCRIPTION

FIELD

FRAGMENT_DEFINITION

FRAGMENT_SPREAD

INLINE_FRAGMENT

VARIABLE_DEFINITION

SCHEMA

SCALAR

OBJECT

FIELD_DEFINITION

ARGUMENT_DEFINITION

INTERFACE

UNION

ENUM

ENUM_VALUE

INPUT_OBJECT

INPUT_FIELD_DEFINITION
}

directive @skip (if: Boolean!) on FIELD|FRAGMENT_SPREAD|INLINE_FRAGMENT

directive @include (if: Boolean!) on FIELD|FRAGMENT_SPREAD|INLINE_FRAGMENT

directive @deprecated (reason: String = "No longer supported") on FIELD_DEFINITION|ARGUMENT_DEFINITION|INPUT_FIELD_DEFINITION|ENUM_VALUE

directive @defer (label: String, if: Boolean! = true) on FRAGMENT_SPREAD|INLINE_FRAGMENT

directive @specifiedBy (url: String!) on SCALAR
15 changes: 15 additions & 0 deletions execution-tests/integration/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
alias(libs.plugins.kgp.jvm)
alias(libs.plugins.ksp)
alias(libs.plugins.apollo.execution)
}

apolloExecution {
service("service") {
packageName.set("com.example")
}
}

dependencies {
implementation(libs.apollo.execution.runtime)
}
Loading

0 comments on commit 561205a

Please sign in to comment.