Hey, I am looking into setting up Data Builders wh...
# apollo-kotlin
s
Hey, I am looking into setting up Data Builders while using custom scalar and multiple modules. When I mock out the query data in a feature module and try and run the test I get an error of
Don't know how to instantiate leaf UnixTime
. This is one of my scalar types, so I created a fake resolver, mostly using the example in the docs. This fixes up the one error, but then causes another one of
Don't know how to instantiate leaf CardOrderStatus
. This now is a Enum from our schema and if I mock it out, it causes another error saying it can’t find next enum from our schema. When I look at the value of
__Schema.all
, it is just an empty list, but looking at all the apollo generated code, I see the
__Schema.all
file is in the module that the
schema.graphqls
file is in, and all the types for
CardOrderStatus
and all the other enums types are in a other feature module. Is there a way to have all the types be in the
__Schema.all
list so I don’t have to mock them all out?
Data Builder:
Copy code
val data = FeatureScreenQuery.Data(resolver = MyFakeResolver()) {
    user = buildUser {
        id = "aaaaaaa=="
    }
}
Fake Resolver:
Copy code
class MyFakeResolver : FakeResolver {
    private val delegate = DefaultFakeResolver(__Schema.all)

    override fun resolveLeaf(context: FakeResolverContext): Any {
        return when (context.mergedField.type.leafType().name) {
            "UnixTime" -> UnixTime(123.toLong())
            "CardOrderStatus" -> CardOrderStatus.PROCESSED
            else -> delegate.resolveLeaf(context)
        }
    }
    // ... everything else is the same from the example
}
__Schema file:
Copy code
public object __Schema {
  public val all: List<CompiledNamedType> = listOf(
      )


  public fun possibleTypes(type: CompiledNamedType): List<ObjectType> =
      com.apollographql.apollo3.api.possibleTypes(all, type)
}
m
Howdy 👋 . Can you try setting
Copy code
apollo {
  alwaysGenerateTypesMatching.set(listOf("/*"))
}
in your schema module?
s
It doesn’t seem to like that very much, when I try a do a build
No signature of method: build_brv5thspzhdwg0pgic89546fl.apollo() is applicable for argument types: (build_brv5thspzhdwg0pgic89546fl$_run_closure3) values: [build_brv5thspzhdwg0pgic89546fl$_run_closure3@1dcd648d]
Possible solutions: apply(groovy.lang.Closure), apply(java.util.Map), apply(groovy.lang.Closure), apply(java.util.Map), split(groovy.lang.Closure)
m
Woops sorry, I missed a
.set()
, editing now...
(edited)
s
Same thing. and for the record I am on 3.7.0
m
Damn, must be a groovy thing. Wait a minute, trying to remember how to make a list in groovy 😅
Maybe this?
Copy code
apollo {
  alwaysGenerateTypesMatching.set(["/*"])
}
s
So that worked to get gradle to sync and for it to build, but when I rerun the test it still is missing the type
s
Can’t help but ask if you’ve run
./gradlew generateApolloSources
after the change to make sure everything is generated with the new config
m
Running the test should re-generate the source (or it means there's another issue)
Do you have
generateDataBuilders
in all your modules (including the schema one)?
Copy code
apollo {
  alwaysGenerateTypesMatching.set(["/*"])
  generateDataBuilders.set(true)
}
s
I just did a full clean and rebuild and made sure the types were all regenerated, and they were. But the test still fails
m
What's your stacktrace now?
s
Copy code
Don't know how to instantiate leaf CardOrderStatus
java.lang.IllegalStateException: Don't know how to instantiate leaf CardOrderStatus
	at com.apollographql.apollo3.api.DefaultFakeResolver.resolveLeaf(fakeResolver.kt:244)
	at com.example.wallet.MyFakeResolver.resolveLeaf(ExampleUnitTest.kt:25)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfNonNullType(fakeResolver.kt:197)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:150)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:145)
	at com.apollographql.apollo3.api.FakeResolverKt.buildField(fakeResolver.kt:117)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfNonNullType(fakeResolver.kt:194)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:150)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:145)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfNonNullType(fakeResolver.kt:170)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:150)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:145)
	at com.apollographql.apollo3.api.FakeResolverKt.buildField(fakeResolver.kt:117)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfNonNullType(fakeResolver.kt:183)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:150)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:139)
	at com.apollographql.apollo3.api.FakeResolverKt.buildField(fakeResolver.kt:117)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfNonNullType(fakeResolver.kt:183)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFieldOfType(fakeResolver.kt:150)
	at com.apollographql.apollo3.api.FakeResolverKt.buildFakeObject(fakeResolver.kt:98)
	at com.apollographql.apollo3.api.FakeResolverKt.buildData(fakeResolver.kt:289)
	at WalletScreen.WalletScreenQuery$Companion.Data(WalletScreenQuery.kt:205)
	at com.example.wallet.ExampleUnitTest.addition_isCorrect(ExampleUnitTest.kt:54)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
	at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
	at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
And yup I do have
generateDataBuilders
in all of my modules
m
Is
CardOrderStatus
in your
__Schema.all
?
s
Nope
Copy code
public object __Schema {
  public val all: List<CompiledNamedType> = listOf(
      )


  public fun possibleTypes(type: CompiledNamedType): List<ObjectType> =
      com.apollographql.apollo3.api.possibleTypes(all, type)
}
m
That must be the issue then. Let me see if I can do a quick reproducer
Seems to work for me unfortunately
I call
./gradlew generateApolloSources
and I get the
Direction
enum in
com/example/schema/type/__Schema.kt
Copy code
public object __Schema {
  public val all: List<CompiledNamedType> = listOf(
        com.example.schema.type.Direction.type, com.example.schema.type.Query.type,
            com.example.schema.type.Tag.type)
Is there anything different in your setup? For now, only look at the schema module (the one that contains the schema). Others shouldn't matter much at this point
s
I am looking right now
I think I just figured it out! I had
Copy code
alwaysGenerateTypesMatching.set(["/*"])
But then in your example it was
Copy code
alwaysGenerateTypesMatching.set([".*"])
and that seemed to fix it
m
Oh gosh so sorry about that, thanks for finding this 🙏
s
All good, Thanks for you help!!
m
Sure thing!
274 Views