https://kotlinlang.org logo
#apollo-kotlin
Title
# apollo-kotlin
j

james

03/15/2023, 6:05 AM
Trying to solve a huge boilerplate headache and I’m wondering if someone can provide any insights 🙏 For reasons out of my control, the GraphQL endpoint I’m talking to cannot handle named fragments. Inline fragments work fine, but named fragments cause the resolver to throw an error. This means I have a ton of near-identical mapping functions for many of the same fields used across different queries — they just have to be repeated because they’re all extension functions on classes which are nested (local) to the query in which they are used. I know the logical answer here is “fix the GraphQL endpoint”, but that’s not an option for me unfortunately so I’m looking for any other advice that might be helpful in creating some kind of reusable code in this situation.
m

mbonnin

03/15/2023, 8:31 AM
Oh wow that's a good one! I don't have a silver bullet there. Fragments are the way to handle this. You could have an interceptor that rewrites named fragments into inline ones I think that'd work. You can use
apollo-ast
for that.
j

james

03/15/2023, 11:49 PM
Thanks Martin! This sounds like an interesting trick that just might work! If I’m understanding correctly, this means Apollo would still codegen all the appropriate classes, and engineers could have reusable mappers for fragments like we normally would with other GraphQL endpoints, but this endpoint would never see the named version of those fragments?
m

mbonnin

03/15/2023, 11:50 PM
Yes, it's like rewriting on the fly
What the developer writes
Copy code
query GetCat {
  animal {
    ...catFragment
  }
}
fragment catFragment on Cat {
  name
  meow
}
what is sent to the server:
Copy code
query GetCat {
  animal {
    ... on Cat {
      name
      meow
    }
  }
}
j

james

03/16/2023, 12:13 AM
Nice, that’s exactly what I thought you meant, just wanted to verify 😄 I will try and find some time to investigate this over the next week or so. I need to keep moving forward with the project I’m on at the moment so can’t build something right away, but this could solve a boilerplate headache for our entire Android team 🤞 Thanks again Martin, I’ll report back if I have some success
2 Views