Hi, I received the following duplication error for...
# apollo-kotlin
s
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?
Copy code
alwaysGenerateTypesMatching.set(["Query"])
in parent module build file in apollo {} block seems to be working
๐Ÿ‘ 1
m
Yep, good catch. We'll add the code sample for groovy
๐Ÿ™ 1
s
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
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:
Copy code
apollo {
  service("service1")ย {
    packageName.set("com.service1")
  }
}
module2:
Copy code
apollo {
  service("service2")ย {
    packageName.set("com.service2")
  }
}
๐Ÿ™Œ 1
s
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
Wrap everything under a
service {}
block
๐Ÿ‘€ 1
What you're doing is using the default service in all modules there
s
Oh okay, but I wonder why it was working before with just one child
Adding another one seemed to break it
m
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
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
schemaFile.set()
should be in the core module if you're using multi module
Also,
package
is suspicious. It should be
packageName
iirc
s
whoops think that is a typo in trying to copy over and anonymize
๐Ÿ‘ 1
m
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()
๐Ÿ‘Œ 1
s
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
Interesting. Let's take things one after the other. First question is: how come you have different schemas?
s
Good question, We have different graphql services and endpoints, a legacy one, a federated one, and a one for favoriting things.
m
So all these endpoints have different types, right?
Even if they share "some" business logic
s
May be some overlap, since a lot of the monolith/legacy service is being migrated to federated. some by proxy, some reimagined
m
In all cases, different endpoints means different services for apollo so you should certainly use different services
s
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
Sounds good ๐Ÿ‘
s
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
Exactly, looks like you need only one apollo "service" (for the federated endpoint)
Why not put the query in the "core" module?
s
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
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
That unfortunately is not possible due to our team structure and scale
m
I mean a single module where the apollo plugin is applied
๐Ÿ™Œ 1
s
Ah yes, my bad
m
Nono sorry, that wasn't clear
s
So let me try removing the plugin from the 2 children
m
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
K 1
And add a simple configuration there:
Copy code
apollo {
   packageName.set("com.example")
}
(no apolloMetata, no generateApolloMetadata, this can all come later)
s
And then the other modules don't need to have apollo configurations like apollo {} at all?
๐Ÿ‘ 1
m
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:
Copy code
feature1/src/main/graphql/schema.json
and your first query:
Copy code
feature1/src/main/graphql/MyQuery.graphql
And then generate code:
Copy code
./gradlew :feature1:generateApolloSources
(or hit the play button in Android Studio)
s
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
"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
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
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
Yes, we have a core module already
m
Is this the module with the query?
(from the other team ?)
s
yes
m
But I'm assuming you don't want your query to go inside that module?
(that would be much easier to start with)
s
Yes, well i am trying with all the queries in core and I still can't get it to work
m
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
Ok, I will try from scratch and just add our team's query next to team1's
m
๐Ÿ‘ let me know how that goes
๐Ÿ™ 1
s
Thank you Martin, you're the best!
m
Sure thing!
181 Views