Hey guys. We writing shared queries to be used by ...
# apollo-kotlin
v
Hey guys. We writing shared queries to be used by two clients. This means that a lot of usage of directives. My question is, is there a way to have all apollo clients attach a directive for a queries/mutations/subscriptions? Or do we have to make sure that all of our Queries provide that argument?
b
Hi! Maybe you could automatically manipulate / process your operations before they are sent to the backend, with an
ApolloInterceptor
?
m
Would you have an example of such a directive? Typically directives are part of the query but are not really passed as arguments. They're just "part of the query"
v
We have 2 apps that share graphql operation with some slight differences that we use directives to define. One app will always have the value be false and another will always have the value be true. Although we can add it to almost every query that would need it I'm wondering if there is a way to do it in a generic way.
Copy code
query Meta() {
    ... SomeFragment
}

fragment SomeFragment on Whatever {
    attribute1 @include(if: $isApp1)
    attribute2 @skip(if: $isApp1)
}
this case is simple but imagine that
SomeFragment
is a super common fragment is used almost everywhere
m
Gotcha. You could transform the query on the fly like @bod was suggesting. It'll require making sure that you don't add a variable if it's never used because GraphQL is very strict about unused variables and will most likely error if that happens
v
how do I go about doing that? I couldn't figure out with
ApolloInterceptor
we are on
2.5.11
currently
m
I was going to say implementing your custom
HttpRequestComposer
might work. But that's not in 2.x
v
I tried last night using an OkHttp interceptor. And could actually add a variable to the body but then kept getting this from the server
Copy code
"message": "Variable \"isTsb\" is not defined by operation \"StartupQuery\"."
m
Yep, you'll need to add it to the query document as well:
Copy code
query Meta($isApp1: Boolean!, $isApp2: Boolean!)
v
ugh but with Persisted Queries it will break right?
m
The trick is not adding it if none of
isApp1
or
isApp2
is used
Auto Persisted Queries will be fine
v
we don't have auto PQs
sounds tricky af maybe it's more simple to just add it to all operations 🤷
m
If you're using something like Apollo safelisting or using OperationOutput to register your ids then indeed modifying the query document at runtime will break
Depends the number of operations you have
Another way to look at this would be to define a "pre-processing" Gradle task that automatically modifies the queries
💡 1
Because it's compile-time, it'll work with safelisting or other whitelisting mecanisms