What's the story of using directives like @include...
# apollo-kotlin
s
What's the story of using directives like @include inside fragments? I have read https://github.com/graphql/graphql-spec/issues/204 but it seems like perhaps it's not part of the spec? Passing a parameter from the query to inside the fragment does not look like something that I can do, right? Is my best bet just having the things that I want to annotate with the directive outside of the fragment and then just adding it manually in the places I want to?
m
You can definitely use variables inside fragments. Just it's implicit:
Copy code
fragment CatDetails {
  meow @include(if: $loud)
}

query GetAnimal($loud: Boolean!) {
  animal {
    # this is valid
    ...CatDetails
  }
}
The spec proposal is about expliciting the variables, which is easier to read (and also allows using different names inside fragments to avoid clashes)
s
Aha, so it should "just work ™️", but I need to be careful to actually have that parameter present in all the places where I call that fragment. And there's no real way to default/fallback to "false" for example if some caller does not have the right parameter coming in. My use case is that I want this directive inside a fragment, and I use that fragment in some places, and optimally I want it to be opt-in. But adding a
$loud: Boolean! = false
to all the callers should be what I am looking for here! Compilation will simply fail if the parameter is not there I assume.
m
Exactly
s
Thanks a lot for the help Martin 🤗
💙 1
a
We have this same problem. Shared fragments accrosss 20+ queries and we want to use include or skip to feature toggle certain fields. We have to declare top level variables in all consuming operations even if not directly used on that page. Parameterized fragments sound good , but also nice that the build fails if you forgot to include one top level, to discover all locations to update
🦠 1