Skip to content

Instantly share code, notes, and snippets.

@BrunoQuaresma
Last active July 24, 2019 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrunoQuaresma/fe436fb78215eb944dfc7e6fd853950b to your computer and use it in GitHub Desktop.
Save BrunoQuaresma/fe436fb78215eb944dfc7e6fd853950b to your computer and use it in GitHub Desktop.
Faunadb-JS types spec

Type improvements for FaunaDB JS driver

This is a draft where we can think of how to improve the FaunaDB JS driver type system, so if you have any suggestions on how we can improve this, feel free to comment and submit your changes.

1. Acessible values in the root path

Current

import faunadb from "faunadb";

type Note = {
  ref: faunadb.values.Ref;
  ts: number;
  data: {
    content: string;
  };
};

Proposal

import faunadb, { Ref } from "faunadb";

type Note = {
  ref: Ref;
  ts: number;
  data: {
    content: string;
  };
};

2. Generic type for documents

Current

import faunadb from "faunadb";

type Note = {
  ref: faunadb.values.Ref;
  ts: number;
  data: {
    content: string;
  };
};

Proposal

import faunadb, { Document } from "faunadb"

type Note = Document<{
  data: {
    content: string
  }
}>

3. Custom return for query()

Current

// The return type of query() is 'object'
import faunadb, { query as q } from "faunadb";

const result = client.query(q.Get(q.Ref(...)));

console.log(result.ref.id) // Throws an error: Property 'ref' does not exist on type 'object'

Proposal

import faunadb, { query as q, Document } from "faunadb";

type Note = Document<{
  data: {
    content: string
  }
}>

const note: Note = client.query<Note>(q.Get(q.Ref(...)));

console.log(note.ref.id) // Works good!

4. Generic type for paginated data

Current

# query() returns {} or any
import faunadb, { query as q } from "faunadb";

const result: {} = client.query(q.Paginate(q.Match(...)));

Proposal

import faunadb, { query as q, Document, Page } from "faunadb";

type Note = Document<{
  data: {
    content: string
  }
}>

const notes = client.query<Page<Note>>(q.Paginate(q.Match(...)));
@thebrianbug
Copy link

Really looking forward to this! Especially the client.query getting a real type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment