-
Notifications
You must be signed in to change notification settings - Fork 651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[apollo-execution] Allow to pass arguments to the root types #5352
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e569fe
Allow to pass arguments to the root types
martinbonnin 4a2cdb3
simplify
martinbonnin c064a6d
Remove unused context class
BoD 698118e
Revert adapter simplification
BoD a6698f7
Add a lock for safety
BoD 68065dc
Revert lock, use AtomicReference
BoD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
81 changes: 81 additions & 0 deletions
81
...otlin/com/apollographql/apollo3/compiler/codegen/kotlin/ExecutableSchemaBuilderBuilder.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,81 @@ | ||
package com.apollographql.apollo3.compiler.codegen.kotlin | ||
|
||
import com.apollographql.apollo3.compiler.capitalizeFirstLetter | ||
import com.apollographql.apollo3.compiler.ir.IrTargetObject | ||
import com.apollographql.apollo3.compiler.ir.asKotlinPoet | ||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.CodeBlock | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.LambdaTypeName | ||
import com.squareup.kotlinpoet.MemberName | ||
import com.squareup.kotlinpoet.ParameterSpec | ||
import com.squareup.kotlinpoet.joinToCode | ||
|
||
internal class ExecutableSchemaBuilderBuilder( | ||
private val context: KotlinContext, | ||
private val serviceName: String, | ||
private val mainResolver: ClassName, | ||
private val adapterRegistry: MemberName, | ||
private val irTargetObjects: List<IrTargetObject>, | ||
) : CgFileBuilder { | ||
val simpleName = context.layout.capitalizedIdentifier("${serviceName}ExecutableSchemaBuilder") | ||
override fun prepare() { | ||
|
||
} | ||
|
||
override fun build(): CgFile { | ||
return CgFile(packageName = context.layout.executionPackageName(), fileName = simpleName, funSpecs = listOf(funSpec()) | ||
) | ||
} | ||
|
||
private fun funSpec(): FunSpec { | ||
val rootIrTargetObjects = listOf("query", "mutation", "subscription").map { operationType -> | ||
irTargetObjects.find { it.operationType == operationType } | ||
} | ||
|
||
return FunSpec.builder(simpleName) | ||
.returns(KotlinSymbols.ExecutableSchemaBuilder) | ||
.apply { | ||
addParameter(ParameterSpec.builder("schema", KotlinSymbols.Schema).build()) | ||
rootIrTargetObjects.forEach { irTargetObject -> | ||
if (irTargetObject != null) { | ||
addParameter( | ||
ParameterSpec.builder( | ||
name = "root${irTargetObject.operationType?.capitalizeFirstLetter()}Object", | ||
type = LambdaTypeName.get(parameters = emptyList(), returnType = irTargetObject.targetClassName.asKotlinPoet()) | ||
).apply { | ||
if (irTargetObject.isSingleton) { | ||
defaultValue(CodeBlock.of("{ %L }", irTargetObject.targetClassName.asKotlinPoet())) | ||
} else if (irTargetObject.hasNoArgsConstructor) { | ||
defaultValue(CodeBlock.of("{ %L() }", irTargetObject.targetClassName.asKotlinPoet())) | ||
} | ||
}.build()) | ||
} | ||
} | ||
} | ||
.addCode( | ||
CodeBlock.builder() | ||
.add("return %L()\n", KotlinSymbols.ExecutableSchemaBuilder) | ||
.add(".schema(schema)\n") | ||
.add(".resolver(%L)\n", mainResolver) | ||
.add(".adapterRegistry(%L)\n", adapterRegistry) | ||
.add(".roots(%L.create(", KotlinSymbols.Roots) | ||
.apply { | ||
rootIrTargetObjects.map { irTargetObject -> | ||
if (irTargetObject == null) { | ||
CodeBlock.of("null") | ||
} else { | ||
CodeBlock.of("root${irTargetObject.operationType?.capitalizeFirstLetter()}Object") | ||
} | ||
}.joinToCode(", ") | ||
.let { | ||
add(it) | ||
} | ||
} | ||
.add("))\n") | ||
.build() | ||
) | ||
.build() | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that works? This locks the writer but readers from the HTTP request thread won't use the lock and might iterate the list while it is modified
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you're right! I liked the
AtomicReference
idea but didn't like that it's JVM only. But maybe that's ok.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AtomicFu has KMP versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah great! 🎉
I think I'll use JVM
AtomicReference
to avoid adding the AtomicFu dependency, but with the knowledge that we can use it to support more targets.