I'm running Apollo server on top of an eventually-consistent architecture, which is causing schema errors when a referenced object is not found in the store/cache. I'm looking for the best way to deal with this issue while maintaining a strict schema (no unnecessarily optional fields).
For example, say I have the schema:
type Article {
id: ID!
title: String!
author: Author!
}
type Author {
id: ID!
name: String!
}
type Query {
mostPopularArticles(limit: Int): [Article!]!
}
My mostPopularArticles
code simply returns an ordered list of IDs, which get resolved into articles by an Article.__resolveObject
resolver. Similarly, I've an Author
resolver. Both of these look up objects by ID from a key/value store.
The problem is, the Article
and Author
stores may not always be consistent, e.g. due to different cache invalidation cycles. In that case, mostPopularArticles
might return an article whose referenced author is not yet in the Author
store.
My first idea would be to just discard the entries that cause the validation errors in the list, returning the rest of the (valid) items, but I'm not sure if Apollo offers a configuration setting or other way to hook in and do so.
I'd also rather avoid validating authors in my article-listing code, since that seems unnecessarily coupled. Ideally, I could have queries that fetch lists of article IDs, which are then automatically resolved by my type resolvers.
What is the best practice for this situation?