https://kotlinlang.org logo
Title
s

Sam Michael

01/28/2022, 6:32 AM
Hi, I received the following duplication error for Apollo generated types, wondering where to add the 'alwaysGenerateTypesMatching' config?
Execution failed for task ':checkServiceApolloDuplicates'.
duplicate Type 'ResolverKey(kind=SchemaType, id=Query)' generated in modules: Somthing,SomethingElse
Use 'alwaysGenerateTypesMatching' in a parent module to generate the type only once
Found it in the docs: https://www.apollographql.com/docs/kotlin/advanced/multi-modules/
// shared/build.gradle.kts
apollo {
// For an example if ReviewInput clashes
alwaysGenerateTypesMatching.set(listOf("ReviewInput"))
// You can also pass Regex patterns
alwaysGenerateTypesMatching.set(listOf(".*Input"))
}
Hmm, wait still getting a gradle error, probably have to change example for groovy language build file maybe?
alwaysGenerateTypesMatching.set(["Query"])
in parent module build file in apollo {} block seems to be working
👍 1
m

mbonnin

01/28/2022, 7:42 AM
Yep, good catch. We'll add the code sample for groovy
🙏 1
s

Sam Michael

02/03/2022, 3:54 PM
Sorry to be back again for more, but having some issues still, thought it was fixed. Basically we have two different POC's going on in our App in two different modules, both using the same schema. I tried suppressing the type generation but still failing the
':checkServiceApolloDuplicates'
gradle task. I guess maybe because there is no parent module, they are both totally separate, so there is no correct module to suppress from. I tried also registering as separate services as I saw you suggest in another issue: https://spectrum.chat/apollo/apollo-android/apollo-multi-module-with-multiple-apps-in-a-project~eaa042ad-49db-470e-b044-d5423891ad70 What is the best way to have 2 separate services using apollo in the same project? Thanks
m

mbonnin

02/03/2022, 4:19 PM
Hey ! No need to apologize for sending feedback 🙂 . This is super helpful!
About the issue, did you give 2 different names to your services? module1:
apollo {
  service("service1") {
    packageName.set("com.service1")
  }
}
module2:
apollo {
  service("service2") {
    packageName.set("com.service2")
  }
}
🙌 1
s

Sam Michael

02/03/2022, 4:21 PM
I tried but gradle sync kept failing, let me try again like your example above
👍 1
Went back to trying all as part of default service (since they are all using same schema): Still getting this error, which is weird because I am doing the same thing with my new child module as was being done before with another child module
Multiple schemas found:
Use different services for different schemas
// core apollo { package.set(“core”) generateKotlinModels.set(true) generateApolloMetadata.set(true) } // child1 apolloMetadata(“core”) apollo { package.set(“child1) }  // child2 apolloMetadata(“core”) apollo { package.set(“child2”) }
m

mbonnin

02/03/2022, 7:08 PM
Wrap everything under a
service {}
block
👀 1
What you're doing is using the default service in all modules there
s

Sam Michael

02/03/2022, 7:10 PM
Oh okay, but I wonder why it was working before with just one child
Adding another one seemed to break it
m

mbonnin

02/03/2022, 7:11 PM
Ah sorry, I missed the part where you explicitly wanted to use the default service
Do you have multiple schema files in your tree?
If yes you can specify the one you want with
schemaFile.set()
👀 1
s

Sam Michael

02/03/2022, 7:13 PM
I have another schema file in top level but it wasn't causing an issue before with just the two modules before, adding a third now it says i have multiple schemas
Definitely can try schemaFile.set(), do I do in all the modules or just parent
We are transitioning from our old endpoint to a new federated graphql service, along with apollo client
but the legacy graphql service shouldn't be detected by apollo plugin at all i dont think
since schema for that is in the root of the project folder
Thanks again for all your help!
💜 1
m

mbonnin

02/03/2022, 7:15 PM
schemaFile.set()
should be in the core module if you're using multi module
Also,
package
is suspicious. It should be
packageName
iirc
s

Sam Michael

02/03/2022, 7:16 PM
whoops think that is a typo in trying to copy over and anonymize
👍 1
m

mbonnin

02/03/2022, 7:17 PM
Also Iirc, the 'multiple schema files found' error should list the files found
(i'm on mobile right now but can double check when back at the computer)
(back at the computer) So yea, since everything shares the same schema, I think it makes sense to use multi-module but it's hard to tell without more context. If you're doing a migration, maybe you'd be better of generating duplicate classes under a different package name and migrating progressively
In all cases, the "multiple schema" error should go away with
schemaFile.set()
:nice: 1
s

Sam Michael

02/03/2022, 8:09 PM
I must be a problem child, I am still getting that error even after setting schemaFile would it be better if I combine all the schemas with list of all the schemas? schemaFile.set(listOf("schema1, schema2, schema3"))
m

mbonnin

02/03/2022, 8:14 PM
Interesting. Let's take things one after the other. First question is: how come you have different schemas?
s

Sam Michael

02/03/2022, 8:19 PM
Good question, We have different graphql services and endpoints, a legacy one, a federated one, and a one for favoriting things.
m

mbonnin

02/03/2022, 8:19 PM
So all these endpoints have different types, right?
Even if they share "some" business logic
s

Sam Michael

02/03/2022, 8:20 PM
May be some overlap, since a lot of the monolith/legacy service is being migrated to federated. some by proxy, some reimagined
m

mbonnin

02/03/2022, 8:20 PM
In all cases, different endpoints means different services for apollo so you should certainly use different services
s

Sam Michael

02/03/2022, 8:21 PM
Well, for now we are using our retrofit client with the others, but moving to apollo-kotlin for the new federated service
the schema files prior to apollo-kotlin readoption are mostly there for js-graphql plugin to work
m

mbonnin

02/03/2022, 8:22 PM
Sounds good 👍
s

Sam Michael

02/03/2022, 8:22 PM
So all could technically be one apollo "service", but I am messing something up in the config I guess
Before we had a core module, and one test pilot module with a single query, I am adding another example from my team, but getting stuck
m

mbonnin

02/03/2022, 8:23 PM
Exactly, looks like you need only one apollo "service" (for the federated endpoint)
Why not put the query in the "core" module?
s

Sam Michael

02/03/2022, 8:24 PM
Yeah, that is a good point, that was a mistaken example I was following I guess, the other team did it that way
and just force of habit I guess
m

mbonnin

02/03/2022, 8:24 PM
It's usually just easier to start with a single module
The multi-module apollo support (as in https://www.apollographql.com/docs/kotlin/advanced/multi-modules/) is interesting when your build grows and you want independant modules to save some build time
But if you're starting up, I'd recommend sticking with a single module
s

Sam Michael

02/03/2022, 8:25 PM
That unfortunately is not possible due to our team structure and scale
m

mbonnin

02/03/2022, 8:26 PM
I mean a single module where the apollo plugin is applied
🙌 1
s

Sam Michael

02/03/2022, 8:26 PM
Ah yes, my bad
m

mbonnin

02/03/2022, 8:26 PM
Nono sorry, that wasn't clear
s

Sam Michael

02/03/2022, 8:26 PM
So let me try removing the plugin from the 2 children
m

mbonnin

02/03/2022, 8:27 PM
You can have a build with 1000 Gradle modules but you're always going to add the first query to one of these modules
So you can choose this module to apply the "com.apollographql.apollo3" plugin
:kotlin-intensifies: 1
And add a simple configuration there:
apollo {
   packageName.set("com.example")
}
(no apolloMetata, no generateApolloMetadata, this can all come later)
s

Sam Michael

02/03/2022, 8:28 PM
And then the other modules don't need to have apollo configurations like apollo {} at all?
👍 1
m

mbonnin

02/03/2022, 8:29 PM
They shouldn't need anything indeed
👍 1
Not as long as you don't generate any code in those other modules
You might want to do this later but usually this is not required until you have a lot of queries
Assuming the module you want to add your first query to is named "feature1", you can add the schema in your module:
feature1/src/main/graphql/schema.json
and your first query:
feature1/src/main/graphql/MyQuery.graphql
And then generate code:
./gradlew :feature1:generateApolloSources
(or hit the play button in Android Studio)
s

Sam Michael

02/03/2022, 8:47 PM
Shoot, now I see the way we built our architecture I don't think it lets me move the queries into the feature1 module. So confused
Or maybe just my computer overheating and build files are all scrambled and I should take a break lol, life of an android developer
m

mbonnin

02/03/2022, 8:48 PM
"Invalidate caches and restart" 😅
I don't think it lets me move the queries into the feature1 module
Not sure I understand. You already have multiple queries? Where are they coming from ? The retrofit implementation you were mentioning before?
I thought this was a "new" implementation using the federated schema so that you'd have to write new queries for this endpoint
s

Sam Michael

02/03/2022, 8:50 PM
There is already 1 other team that started ahead of me and made a module with 1 query, so I was trying to emulate their pattern
m

mbonnin

02/03/2022, 8:50 PM
Ah ok
Then yea, if you want multi-module already, you'll have to go through the hoops of
generateApolloMetadata
and
apolloMetadata
First thing you'll have to do is to create the "core" module and make that other team module use it
s

Sam Michael

02/03/2022, 8:52 PM
Yes, we have a core module already
m

mbonnin

02/03/2022, 8:53 PM
Is this the module with the query?
(from the other team ?)
s

Sam Michael

02/03/2022, 8:53 PM
yes
m

mbonnin

02/03/2022, 8:54 PM
But I'm assuming you don't want your query to go inside that module?
(that would be much easier to start with)
s

Sam Michael

02/03/2022, 8:54 PM
Yes, well i am trying with all the queries in core and I still can't get it to work
m

mbonnin

02/03/2022, 8:54 PM
What is the error you're getting?
If there's one query and it's working, you should be able to add a second
.graphql
file next to the first one without even touching the Gradle configuration
s

Sam Michael

02/03/2022, 8:56 PM
Ok, I will try from scratch and just add our team's query next to team1's
m

mbonnin

02/03/2022, 8:56 PM
👍 let me know how that goes
:thank-you: 1
s

Sam Michael

02/03/2022, 8:57 PM
Thank you Martin, you're the best!
m

mbonnin

02/03/2022, 8:58 PM
Sure thing!