fuse
fuse copied to clipboard
RFC: Support multiple keys on a node
Summary
Many nodes are uniquely identifiable by more than one key. Common example: BlogPost.id
and BlogPost.slug
.
Proposed Solution
First idea:
const BlogPostNode = node({
keys: ['id', 'slug'], // Array
// keys arg is an array of { id?: string, slug?: string }
// if possible, even better if it's a union { id: string } | { slug: string }
load: async (keys) => {
return keys.map(({ id, slug }) => id ? getById(id) : getBySlug(slug) )
}
})
addNodeFields(User, (t) => ({
highlightedBlogPost: t.field({
type: BlogPostNode,
// Can return either { id: string } | { slug: string }
resolve: (parent) => ({ slug: parent.highlightedBlogPostSlug })
)}
}))