Nexus resolve() returns PrismaPromise but gives error

1k views Asked by At

I am trying to build an apollo server with Nexus.js, Prisma and Typescript for a to-do app.

I created the Todo nexus type, and I wanted to create the query, but I'm having an error.
The definition:

import { extendType, objectType } from "nexus";

export const Todo = objectType({
  name: "Todo",
  definition(t) {
    t.id("id");
    t.nonNull.string("title");
    t.string("detail");
  },
});

export const TodoQuery = extendType({
  type: "Query",
  definition(t) {
    t.list.field("todos", {
      type: "Todo",
      resolve(_root, _args, ctx) {
        return ctx.db.todo.findMany(); // db is an instance of PrismaClient()
      },
    });
  },
});

The error:

Type '(_root: {}, _args: {}, ctx: Context) => PrismaPromise<Todo[]>' is not assignable to type 'FieldResolver<"Query", "todos">'.
  Type 'PrismaPromise<Todo[]>' is not assignable to type 'MaybePromise<({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null>'.
    Type 'PrismaPromise<Todo[]>' is not assignable to type 'PromiseLike<({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null>'.
      Types of property 'then' are incompatible.
        Type '<TResult1 = Todo[], TResult2 = never>(onfulfilled?: ((value: Todo[]) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = ({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null, TResult2 = never>(onfulfilled?: ((value: ({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null) => TResult1 | PromiseLike<...>) | null | undefined, ...'.
          Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
            Types of parameters 'value' and 'value' are incompatible.
              Type 'Todo[]' is not assignable to type '({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[]'.
                Type 'Todo' is not assignable to type '{ detail?: string | null | undefined; id?: string | null | undefined; title: string; }'.
                  Types of property 'id' are incompatible.
                    Type 'number' is not assignable to type 'string'.
1

There are 1 answers

0
kazuemon On

ID types in GraphQL are handled as String.
Probably, you are using number as the ID of the todo, so the type does not match, resulting in an error.

const SpecifiedScalars = {
  ID: 'string',
  String: 'string',
  Float: 'number',
  Int: 'number',
  Boolean: 'boolean',
}

from https://github.com/graphql-nexus/nexus/blob/11d028277e7fec241246237dbe31707c037407ee/src/typegenPrinter.ts#L44