https://kotlinlang.org logo
p

Paddy O'Brien

09/14/2022, 8:15 PM
Alternatively, if there is a way to change the generated scalar name created during apollo code gen that’d also work
b

bod

09/14/2022, 9:16 PM
Hi! In Apollo Kotlin there's a
@targetName
directive you can use to instruct the codegen to use a different name. It was introduced here. Example usage here
p

Paddy O'Brien

09/15/2022, 3:06 AM
perfect thanks!
So I just gave this a try • I created an
extra.graphqls
file • It lives next to my
schema.json
• Its contents are
Copy code
# Remap names that will result in collisions, in particular when exported to Swift
extend scalar URL @targetName(name: "URL_")
• Definition in the schema is
Copy code
"type": {
  "kind": "NON_NULL",
  "name": null,
  "ofType": {
    "kind": "SCALAR",
    "name": "URL",
    "ofType": null
  }
},
• I cleared the gradle cache • I manually deleted the generated apollo sources • in kotlin im still getting
Copy code
public class URL {
  public companion object {
    public val type: CustomScalarType = CustomScalarType("URL", "kotlin.String")
  }
}
• in Swift im still getting
Copy code
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("URL")))
@interface NativeSyncURL : NativeSyncBase
- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer));
+ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead")));
@property (class, readonly, getter=companion) NativeSyncURLCompanion *companion __attribute__((swift_name("companion")));
@end;
• build.gradle
Copy code
service("admin") {
  useVersion2Compat("com.admin")
  schemaFile = file("src/commonMain/graphql/admin/schema.json")
  sourceFolder.set("admin")
  mapScalarToKotlinString("URL")
}
Apollogies for the info dump, saw in the Github issue you were waiting on info so im trying to be complete
im not seeing any errors in the log
This is in 3.5
b

bod

09/15/2022, 2:38 PM
Hmm what you did looks good - wondering why it doesn't work. The generated Kotlin file should be named
URL_
p

Paddy O'Brien

09/15/2022, 2:40 PM
Its still named URL
Is it possibly related to
useVersion2Compat
or the fact that we use
schema.json
and not
schema.graphqls
as mentioned in the issue?
b

bod

09/15/2022, 2:44 PM
It shouldn't (by the way, you can convert your schema to
.graphqls
, it is a bit nicer to work with!
./gradlew convertApolloSchema --from=src/main/graphql/schema.json --to=src/main/graphql/schema.graphqls
)
oh I know: it's because of
schemaFile =
, in that case the
extra.graphqls
is not automatically taken into account. You need to include it manually:
Copy code
schemaFiles.setFrom(setOf(file("src/main/graphql/schema.json"), file(file("src/main/graphql/extra.graphqls"))))
p

Paddy O'Brien

09/15/2022, 3:12 PM
gradle sync fails when I use that
Copy code
10:11 a.m.	Gradle sync failed: No signature of method: build_a7r62ca1lbwotzkp5jpoglq3a.apollo() is applicable for argument types: (build_a7r62ca1lbwotzkp5jpoglq3a$_run_closure2) values: [build_a7r62ca1lbwotzkp5jpoglq3a$_run_closure2@19625293]
				Possible solutions: apply(groovy.lang.Closure), apply(java.util.Map), apply(groovy.lang.Closure), apply(java.util.Map), split(groovy.lang.Closure) (34 s 133 ms)
b

bod

09/15/2022, 3:13 PM
wow, that stacktrace 😅 maybe it's the kts syntax that Groovy doesn't like? Does
setOf
exist in Groovy?
p

Paddy O'Brien

09/15/2022, 3:14 PM
I should note that you have 3
file(
in your example, I assumed the
file(file(
was a typo
b

bod

09/15/2022, 3:14 PM
woops! yes sorry about that
p

Paddy O'Brien

09/15/2022, 3:15 PM
k, I had removed the duplicate which had that error
it does have
set
or does it 😅
b

bod

09/15/2022, 3:19 PM
would this work?
Copy code
schemaFiles.setFrom(files("src/main/graphql/schema.json", "src/main/graphql/extra.graphqls"))
p

Paddy O'Brien

09/15/2022, 3:30 PM
That’s generated the right type. Thanks for working through it with me
b

bod

09/15/2022, 3:31 PM
Great! 🎉
5 Views