fuse icon indicating copy to clipboard operation
fuse copied to clipboard

RFC: Support multiple keys on a node

Open mxstbr opened this issue 1 year ago • 0 comments

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 })
  )}
}))

mxstbr avatar Dec 01 '23 19:12 mxstbr