StellateHQ / fuse

Fuse: The fastest way to build and query great APIs with TypeScript

Home Page:https://fusedata.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RFC: Support multiple keys on a node

mxstbr opened this issue · 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 })
  )}
}))