1

I have a loader function in order to fetch chefs from a database. So that loader receive an array of ids and what's is strange is that the ids have a readonly type.

When I try to pass that read-only type to the database query, it gives an error.

enter image description here

How can I fix the type definition?

Source code: https://github.com/LauraBeatris/graphql-with-nextjs/blob/master/pages/api/loader.ts

Laura Beatris
  • 1,782
  • 7
  • 29
  • 49
  • 1
    According to the error, the value `"id"` is not a valid argument to the `whereIn` method you're calling. This method is specific to whatever library you're using to create the `databaseClient` (I'm guessing Knex) -- so this problem is not really specific to either DataLoader or GraphQL. – Daniel Rearden Jun 21 '20 at 00:02
  • 1
    Since DataLoader's batch load function uses generic types, you probably need to explicitly provide a type for your `ids` parameter (i.e. `ids: string[]`). I'm guessing without that, TypeScript can't properly match the correct overload signature for `whereIn`. – Daniel Rearden Jun 21 '20 at 00:11

1 Answers1

1

I fixed that according to @DanielRearden comment.

The function that the DataLoader instance receives uses generic types, so we're able to pass a type to the ids argument and then use it inside of the whereIn knex method.

 new DataLoader((ids: string[]) => (
    databaseClient
      .table("chefs")
      .whereIn("id", ids)
      .select("*")
      .then(rows => ids.map(id => rows.find(row => row.id === id)))
  ))
Laura Beatris
  • 1,782
  • 7
  • 29
  • 49