Hello! We have several fragments in different file...
# apollo-kotlin
e
Hello! We have several fragments in different files that each query different fields, but they all need to be passed down the same argument, something like:
Copy code
fragment A on User {
    f1(capabilities: ["CAP1", "CAP2"])
}

fragment B on User {
    f2(capabilities: ["CAP1", "CAP2"])
}
Is there some way to define that argument once, to avoid having to maintain multiple definitions of it? The best idea I had was to use GQL variables, but these fragments are included transitively in many queries, so it's not a very scalable solution 😄
b
hmm I can't think of anything other that variables
m
That'd be a nice use case for parameterized fragments (with default arguments here)
Well actually not really because you'd still have to define the default value twice..
So we'd need more something like GraphQL preprocessor or so...
b
if these capabilities are usually the same for all clients, the default value could be set on the schema
e
Unfortunately, we have two clients, which can implement certain capabilities at different times 😞 A preprocessor would be nice, indeed. I guess for the time being it'll stay as it is 😄 Thanks for jumping in so quick!
An idea from a colleague, maybe macros would be easier to implement then a full blown preprocessor 😄 Because the bottom line of what we need is just a string interpolation.
b
yeah something very simple could be enough for your case 🙂
m
You could Gradle your way into this: add a
preprocessGraphQLFiles
that does string replace. That might or might not be worth the extra hoops
e
I created a hack for a similar situation in our app: we want all our queries with
debugFlags: Optional<List<DebugFlag>>
input to have default
= Optional.present(getCurrentDebugFlags())
. in our case, the easiest thing for me to do was to use a Gradle/AGP transform to rewrite the bytecode