Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise error in Ghost example #30

Closed
jakelowen opened this issue Feb 11, 2019 · 1 comment
Closed

Promise error in Ghost example #30

jakelowen opened this issue Feb 11, 2019 · 1 comment
Labels
question Further information is requested

Comments

@jakelowen
Copy link

Using code copied straight from ghost example. UserSource byIdLoader throws a tslint error:

Code:

dataSources/UserLoader

import DataLoader from "dataloader";
import { byColumnLoader } from "../utils/loaderUtils";
import { Context } from "./Context";
import { dbt } from "../generated";

export class UserSource {
  constructor(protected ctx: Context) {}

  byIdLoader = new DataLoader<string, dbt.Users>(ids => {
    return byColumnLoader(this.ctx, "users", "id", ids);
  });

  byId(id: string) {
    return this.byIdLoader.load(id);
  }
}

utils/loaderUtils

import _ from "lodash";
import { DBTables } from "../generated/db-tables";
import { Context } from "../dataSources/Context";

export function byColumnLoader<
  Tbl extends keyof DBTables,
  Key extends Extract<keyof DBTables[Tbl], string>,
  KeyType extends Extract<DBTables[Tbl][Key], string | number>
>(
  ctx: Context,
  table: Tbl,
  key: Key,
  keys: KeyType[]
): PromiseLike<DBTables[Tbl][]> {
  return ctx
    .knex(table)
    .select(`${table}.*`)
    .whereIn(`${table}.${key}`, keys)
    .then((rows: DBTables[Tbl][]) => {
      const keyed: Record<KeyType, DBTables[Tbl]> = _.keyBy(rows, key);
      return keys.map(k => {
        return keyed[k] || new Error(`Missing row for ${table}:${key} ${k}`);
      });
    });
}

Gives tslint error:

Argument of type '(ids: string[]) => PromiseLike<Users[]>' is not assignable to parameter of type 'BatchLoadFn<string, Users>'.
  Type 'PromiseLike<Users[]>' is missing the following properties from type 'Promise<(Users | Error)[]>': catch, [Symbol.toStringTag], finallyts(2345)

image

@tgriesser
Copy link
Member

I think you missed the dependencies. The example uses a fork of dataloader which includes this typings patch graphql/dataloader#146 that was never merged. If you change byColumnLoader to be an async function (async byColumnLoader) it should work as well.

@tgriesser tgriesser added the question Further information is requested label Feb 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants