graphql-binding / graphql-static-binding

Generate static binding files for a GraphQL schema
MIT License
24 stars 7 forks source link

For optional fields, there is no constraint of type "null" #75

Open OniVe opened 6 years ago

OniVe commented 6 years ago

Bug Report

Current behavior

There is no possibility of setting the value "null" for arguments because of the restrictions on types

Reproduction

Scheme

type User {
  id: ID! @unique
  name: String!
}

type Post {
  id: ID! @unique
  isPublished: Boolean! @default(value: "false")
  title: String!
  text: String # Not required
  user: User # Not required
}

./generated/prisma.ts

// ...
export interface PostWhereInput {
  AND?: PostWhereInput[] | PostWhereInput
  OR?: PostWhereInput[] | PostWhereInput
  id?: ID_Input
  id_not?: ID_Input
  id_in?: ID_Input[] | ID_Input
  id_not_in?: ID_Input[] | ID_Input
  id_lt?: ID_Input
  id_lte?: ID_Input
  id_gt?: ID_Input
  id_gte?: ID_Input
  id_contains?: ID_Input
  id_not_contains?: ID_Input
  id_starts_with?: ID_Input
  id_not_starts_with?: ID_Input
  id_ends_with?: ID_Input
  id_not_ends_with?: ID_Input
  isPublished?: Boolean
  isPublished_not?: Boolean
  title?: String
  title_not?: String
  title_in?: String[] | String
  title_not_in?: String[] | String
  title_lt?: String
  title_lte?: String
  title_gt?: String
  title_gte?: String
  title_contains?: String
  title_not_contains?: String
  title_starts_with?: String
  title_not_starts_with?: String
  title_ends_with?: String
  title_not_ends_with?: String
  text?: String // Must be: Nullable<String> where: type Nullable<T> = T | null
  text_not?: String // Must be: Nullable<String> where: type Nullable<T> = T | null
  text_in?: String[] | String
  text_not_in?: String[] | String
  text_lt?: String
  text_lte?: String
  text_gt?: String
  text_gte?: String
  text_contains?: String
  text_not_contains?: String
  text_starts_with?: String
  text_not_starts_with?: String
  text_ends_with?: String
  text_not_ends_with?: String
  user?: UserWhereInput // Must be: Nullable<UserWhereInput> where: type Nullable<T> = T | null
}
// ...
export type Query = {
// ...
posts: (args: { where?: PostWhereInput, orderBy?: PostOrderByInput, skip?: Int, after?: String, before?: String, first?: Int, last?: Int }, info?: GraphQLResolveInfo | string) => Promise<Post[]>
// ...
}

Resolver usage

myResolver(parent, args, context, info) {
  return context.db.query.posts(
    { 
      where: {
        isPublished: true,
        user: null // Error: The type "null" can not be assigned for the type "UserWhereInput | undefined".
      }
    },
      info
  );
}

Expected behavior?

No typescript validation errors.

danielkcz commented 6 years ago

I tried the quick fix of adding | null here, but it's much more entangled there with all those recursive calls. It would probably require some different approach.

https://github.com/graphql-binding/graphql-static-binding/blob/8ddd494704ee224850e6c825c4ea5da1bb464814/src/generators/graphcool-ts.ts#L262