添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

What's not working?

If you have an object in your SDL like so: date: DateTime , then the generated type is date?: Maybe<Scalars['DateTime']>; , where Scalars['DateTime'] is Date | string .

If you then use that object as a return type for a query or mutation, the resolver type for this attribute is generated to be date?: Date | null | undefined .

So then, even though you're not doing anything wrong, Typescript will give you this particularly ugly error:

Type '({ locationId, vibeCount, }: RequireFields<MutationgeneratePlaceVibesArgs, "locationId">) => Promise<Event[]>' is not assignable to type 'Resolver<ResolverTypeWrapper<{ __typename?: "Event" | undefined; date?: Date | null | undefined; location?: { __typename?: "Location" | undefined; address: string; business: { __typename?: "Business" | undefined; ... 6 more ...; website?: Maybe<...> | undefined; }; ... 7 more ...; updatedAt: string | Date; } | null ...'.
  Type 'Promise<Event[]>' is not assignable to type 'ResolverTypeWrapper<{ __typename?: "Event" | undefined; date?: Date | null | undefined; location?: { __typename?: "Location" | undefined; address: string; business: { __typename?: "Business" | undefined; ... 6 more ...; website?: Maybe<...> | undefined; }; ... 7 more ...; updatedAt: string | Date; } | null | undefin...'.
    Type 'Promise<Event[]>' is not assignable to type 'Promise<ResolverTypeWrapper<{ __typename?: "Event" | undefined; date?: Date | null | undefined; location?: { __typename?: "Location" | undefined; address: string; business: { __typename?: "Business" | undefined; ... 6 more ...; website?: Maybe<...> | undefined; }; ... 7 more ...; updatedAt: string | Date; } | null |...'.
      Type 'Event[]' is not assignable to type 'ResolverTypeWrapper<{ __typename?: "Event" | undefined; date?: Date | null | undefined; location?: { __typename?: "Location" | undefined; address: string; business: { __typename?: "Business" | undefined; ... 6 more ...; website?: Maybe<...> | undefined; }; ... 7 more ...; updatedAt: string | Date; } | null | undefin...'.
        Type 'Event' is not assignable to type 'ResolverTypeWrapper<{ __typename?: "Event" | undefined; date?: Date | null | undefined; location?: { __typename?: "Location" | undefined; address: string; business: { __typename?: "Business" | undefined; ... 6 more ...; website?: Maybe<...> | undefined; }; ... 7 more ...; updatedAt: string | Date; } | null | undefin...'.
          Type 'Event' is not assignable to type '{ __typename?: "Event" | undefined; date?: Date | null | undefined; location?: { __typename?: "Location" | undefined; address: string; business: { __typename?: "Business" | undefined; ... 6 more ...; website?: Maybe<...> | undefined; }; ... 7 more ...; updatedAt: string | Date; } | null | undefined; ... 7 more ...; ...'.
            Types of property 'date' are incompatible.
              Type 'Maybe<string | Date> | undefined' is not assignable to type 'Date | null | undefined'.
                Type 'string' is not assignable to type 'Date'.ts(2322)
          

Hey @arimendelow sorry for the late reply here—could you provide a more complete reproduction that I could take a look at?

E.g. given this sdl...

export const schema = gql`
  type UserExample {
    id: Int!
    email: String!
    name: String
    birthday: DateTime
  type Query {
    userExamples: [UserExample!]! @requireAuth
    userExample(id: Int!): UserExample @requireAuth
  input CreateUserExampleInput {
    email: String!
    name: String
    birthday: DateTime
  input UpdateUserExampleInput {
    email: String
    name: String
    birthday: DateTime
  type Mutation {
    createUserExample(input: CreateUserExampleInput!): UserExample! @requireAuth
    updateUserExample(id: Int!, input: UpdateUserExampleInput!): UserExample!
      @requireAuth
    deleteUserExample(id: Int!): UserExample! @requireAuth

Which is just the standard sdl generator SDL, I've got birthday: DateTime there. And then what do I need to do (in a service presumably)?

Hey @jtoar, sorry for the delay!! Totally missed your reply.

We can get a little more straightforward by focusing on the any model with createdAt, which will generally have: createdAt: DateTime! in its SDL and createdAt DateTime in its Prisma schema.

Say this is our User model in Prisma:

model User {
  createdAt DateTime @default(now())

And this is our corresponding User SDL:

  type User {
    createdAt: DateTime!

This is then the generated Typescript for the Prisma User model (in node_modules/.prisma/client/index.d.ts):

export type User = {
  createdAt: Date

And this is the generated Typescript for the GraphQL User type (in api/types/graphql.d.ts):

export type User = {
  createdAt: Scalars['DateTime'];

Where Scalars['DateTime'] is Date | string.

And string, of course, is not assignable to Date, so if you try to use increased type annotations to say "this GraphQL service resolver will return this Prisma object", you'll get the error I put in the bug description.

sure, @Tobbe ! what i'm referring to is use-cases where you try to use the Prisma Typescript types together with the GraphQL Typescript types, for example by returning an object typed with the GraphQL type in a service resolver.

For example, doing this:

import type {
  QueryResolvers,
  User,
} from 'types/graphql'
import { db } from 'src/lib/db'
export const user: QueryResolvers['user'] = async ({ id }) => {
  const user = (await db.user.findUnique({
    where: { id },
  })) as unknown as User
  return user

Will have this error:

Of course, this is a contrived example, but sometimes we do need to use type casting in more complex services, and it's counter intuitive that you can't use the GraphQL types to return from a GraphQL service.

This is not a high-priority bug by any means, and it would even be fair to close it as "as designed" if that's how you feel :)