Skip to content
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

Update tests to all vars #419

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/src/main/java/graphql/nadel/engine/util/CollectionUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ inline fun <K, E> Sequence<E>.strictAssociateBy(crossinline keyExtractor: (E) ->
*/
@JvmName("mapFromPairs")
fun <K, V> mapFrom(entries: Collection<Pair<K, V>>): Map<K, V> {
val map = HashMap<K, V>(entries.size)
val map = LinkedHashMap<K, V>(entries.size)
map.putAll(entries)
require(map.size == entries.size) {
@Suppress("SimpleRedundantLet") // For debugging purposes if you want to visit the values
Expand All @@ -61,7 +61,7 @@ fun <K, V> mapFrom(entries: Collection<Pair<K, V>>): Map<K, V> {
*/
@JvmName("mapFromPairs")
fun <K, V> mapFrom(entries: Sequence<Pair<K, V>>): Map<K, V> {
val map = HashMap<K, V>()
val map = LinkedHashMap<K, V>()
var count = 0
entries.forEach {
map[it.first] = it.second
Expand Down Expand Up @@ -90,7 +90,7 @@ fun <K, V> Collection<Pair<K, V>>.toMapStrictly(): Map<K, V> {
*/
@JvmName("mapFromEntries")
fun <K, V> mapFrom(entries: Collection<Map.Entry<K, V>>): Map<K, V> {
val map = HashMap<K, V>(entries.size)
val map = LinkedHashMap<K, V>(entries.size)
entries.forEach(map::put)
require(map.size == entries.size)
return map
Expand All @@ -114,7 +114,7 @@ fun <K, V> MutableMap<K, V>.put(entry: Pair<K, V>) {
* Inverts the [Map] such that the values are now the keys and the keys are now the values.
*/
fun <K, V> Map<K, V>.invert(): Map<V, K> {
val map = HashMap<V, K>(this.size)
val map = LinkedHashMap<V, K>(this.size)
forEach { (key, value) ->
map[value] = key
}
Expand Down
1 change: 1 addition & 0 deletions test/src/test/kotlin/graphql/nadel/tests/EngineTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private val singleTestToRun = (System.getenv("TEST_NAME") ?: "")
* NadelExecutionInput.Builder#transformExecutionHints method in your test hook class.
*/
private val defaultHints = NadelExecutionHints.newHints()
.allDocumentVariablesHint { true }
.build()

private val sep = "-".repeat(50)
Expand Down
66 changes: 65 additions & 1 deletion test/src/test/kotlin/graphql/nadel/tests/TestFixtureUpdater.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package graphql.nadel.tests

import com.fasterxml.jackson.module.kotlin.readValue
import graphql.language.AstPrinter
import graphql.nadel.tests.yaml.YamlComment.Position.Block
import graphql.nadel.tests.yaml.YamlComment.Position.End
import graphql.nadel.tests.yaml.YamlComment.Position.Inline
import graphql.nadel.tests.yaml.collectComments
import graphql.nadel.tests.yaml.traverseYaml
import graphql.nadel.tests.yaml.writeYamlTo
import graphql.normalized.ExecutableNormalizedOperationFactory.createExecutableNormalizedOperationWithRawVariables
import graphql.normalized.ExecutableNormalizedOperationToAstCompiler.compileToDocument
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaGenerator
import graphql.schema.idl.SchemaParser
import graphql.schema.idl.errors.SchemaProblem
import java.io.File

// For autocomplete navigation
Expand Down Expand Up @@ -43,11 +50,68 @@ private fun update(fixturesDir: File) {
}
}

private class TestProcessingException(message: String, cause: Throwable) : RuntimeException(message, cause)

fun update(testFixture: TestFixture): TestFixture {
return testFixture
.copy(
// serviceCalls = getUpdatedServiceCalls(testFixture),
)
}

private fun getUpdatedServiceCalls(testFixture: TestFixture): List<ServiceCall> {
val runtimeWiring = RuntimeWiring.newRuntimeWiring()
.wiringFactory(GatewaySchemaWiringFactory())
.build()

val underlyingSchemas = testFixture.underlyingSchema
.mapValues { (_, schemaText) ->
val typeDefs = SchemaParser().parse(schemaText)
try {
SchemaGenerator().makeExecutableSchema(typeDefs, runtimeWiring)
} catch (e: SchemaProblem) {
throw TestProcessingException("Unable to process ${testFixture.name}", e)
}
}

return testFixture.serviceCalls
.map { call ->
try {
val underlyingSchema = underlyingSchemas[call.serviceName]
?: throw IllegalArgumentException("No schema found for ${call.serviceName}")

val ern = createExecutableNormalizedOperationWithRawVariables(
underlyingSchema,
call.request.document,
call.request.operationName,
call.request.variables,
)

val compileResult = compileToDocument(
underlyingSchema,
ern.operation,
call.request.operationName,
ern.topLevelFields
) { _, _, _ -> true }

call.copy(
request = call.request.copy(
query = AstPrinter.printAst(compileResult.document),
variables = compileResult.variables,
),
)
} catch (e: Exception) {
TestProcessingException(
message = "Unable to process service calls for ${testFixture.name}",
cause = e,
).printStackTrace()

call
}
}
}

fun writeOut(fixtureFile: File, testFixture: TestFixture) {
private fun writeOut(fixtureFile: File, testFixture: TestFixture) {
val commentsFromOldYaml = collectComments(fixtureFile).toMutableMap()

val rootNodes = traverseYaml(yamlObjectMapper.writeValueAsString(testFixture)) { path, node ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class `transformer-on-hydration-fields` : EngineTestHook {
field: ExecutableNormalizedField,
state: Any,
): NadelTransformFieldResult {
val transformedArgs = mapOf("id" to NormalizedInputValue("String", StringValue("transformed-id")))
val transformedArgs = mapOf("id" to NormalizedInputValue("ID", StringValue("transformed-id")))
return transformer.transform(field.children)
.let {
field.toBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ serviceCalls:
- serviceName: "MyService"
request:
query: |
query myQuery {
hello(arg: {})
query myQuery($v0: Arg) {
hello(arg: $v0)
}
variables: { }
variables:
v0: { }
operationName: "myQuery"
# language=JSON
response: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ serviceCalls:
- serviceName: "service"
request:
query: |
query {
rename__issue__issueById: issueById(id: "57") {
query ($v0: ID) {
rename__issue__issueById: issueById(id: $v0) {
__typename__rename__key: __typename
rename__key__id: id
}
}
variables: { }
variables:
v0: "57"
# language=JSON
response: |-
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ serviceCalls:
- serviceName: "service"
request:
query: |
query {
issue(id: "57") {
query ($v0: ID) {
issue(id: $v0) {
__typename__rename__key: __typename
rename__key__id: id
}
}
variables: { }
variables:
v0: "57"
# language=JSON
response: |-
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,20 @@ serviceCalls:
- serviceName: "IssueService"
request:
query: |
query {
issuesByIds(id: ["issue-1", "issue-2", "issue-3"]) {
query ($v0: [ID!]) {
issuesByIds(id: $v0) {
__typename__deep_rename__name: __typename
deep_rename__name__detail: detail {
detailName
}
batch_hydration__issue__id: id
}
}
variables: { }
variables:
v0:
- "issue-1"
- "issue-2"
- "issue-3"
# language=JSON
response: |-
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,20 @@ serviceCalls:
- serviceName: "IssueService"
request:
query: |
query {
issuesByIds(id: ["issue-1", "issue-2", "issue-3"]) {
query ($v0: [ID!]) {
issuesByIds(id: $v0) {
__typename__deep_rename__name: __typename
deep_rename__name__detail: detail {
detailName
}
batch_hydration__issue__id: id
}
}
variables: { }
variables:
v0:
- "issue-1"
- "issue-2"
- "issue-3"
# language=JSON
response: |-
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,16 @@ serviceCalls:
- serviceName: "IssueService"
request:
query: |
query {
issueById(id: "issue-1") {
query ($v0: ID!) {
issueById(id: $v0) {
__typename__deep_rename__name: __typename
deep_rename__name__detail: detail {
detailName
}
}
}
variables: { }
variables:
v0: "issue-1"
# language=JSON
response: |-
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ serviceCalls:
- serviceName: "IssueService"
request:
query: |
query {
issueById(id: "issue-1") {
query ($v0: ID!) {
issueById(id: $v0) {
__typename__deep_rename__name: __typename
deep_rename__name__detail: detail {
detailName
}
}
}
variables: { }
variables:
v0: "issue-1"
# language=JSON
response: |-
{
Expand All @@ -111,15 +112,16 @@ serviceCalls:
- serviceName: "IssueService"
request:
query: |
query {
issueById(id: "issue-2") {
query ($v0: ID!) {
issueById(id: $v0) {
__typename__deep_rename__name: __typename
deep_rename__name__detail: detail {
detailName
}
}
}
variables: { }
variables:
v0: "issue-2"
# language=JSON
response: |-
{
Expand All @@ -136,15 +138,16 @@ serviceCalls:
- serviceName: "IssueService"
request:
query: |
query {
issueById(id: "issue-3") {
query ($v0: ID!) {
issueById(id: $v0) {
__typename__deep_rename__name: __typename
deep_rename__name__detail: detail {
detailName
}
}
}
variables: { }
variables:
v0: "issue-3"
# language=JSON
response: |-
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ serviceCalls:
- serviceName: "IssueService"
request:
query: |
query {
query ($v0: ID!) {
issue {
__typename__deep_rename__name: __typename
deep_rename__name__detail: detail {
detailName(userId: "USER-01")
detailName(userId: $v0)
}
}
}
variables: { }
variables:
v0: "USER-01"
# language=JSON
response: |-
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Pretty sure this test doesn't actually make any sense, we should delete it if so
name: "deep rename with more unions"
enabled: true
overallSchema:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Pretty sure this test doesn't actually make any sense, we should delete it if so
name: "deep rename with unions"
enabled: true
overallSchema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ serviceCalls:
- serviceName: "MyService"
request:
query: |
query myQuery($v0: JSON, $v1: JSON!) {
hello(arg: {payload : $v0}, arg1: $v1)
query myQuery($v0: InputWithJson, $v1: JSON!) {
hello(arg: $v0, arg1: $v1)
}
variables:
v0:
name: "Bobert"
age: "23"
payload:
name: "Bobert"
age: "23"
v1:
interests:
- "photography"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ serviceCalls:
- serviceName: "MyService"
request:
query: |
query myQuery($v0: JSON) {
hello(arg: {payload : $v0})
query myQuery($v0: InputWithJson) {
hello(arg: $v0)
}
variables:
v0:
48x48: "file.jpeg"
payload:
48x48: "file.jpeg"
operationName: "myQuery"
# language=JSON
response: |-
Expand Down
Loading