graphql-nexus / nexus

Code-First, Type-Safe, GraphQL Schema Construction
https://nexusjs.org
MIT License
3.4k stars 274 forks source link

Need more control over typegen #729

Open ashwins93 opened 3 years ago

ashwins93 commented 3 years ago

I'm trying to match some input object types generated by Nexus with those that are generated by Prisma (it makes it convenient to just pass on the args to prisma client). However there is a problem. How do I generate a equivalent nexus type for the following prisma type?

export type StringFilter = {
    equals?: string
    in?: Enumerable<string>
    notIn?: Enumerable<string>
    lt?: string
    lte?: string
    gt?: string
    gte?: string
    contains?: string
    startsWith?: string
    endsWith?: string
    mode?: QueryMode
    not?: NestedStringFilter | string
  }

here's what I tried

export const StringFilter = inputObjectType({
  name: 'StringFilter',
  definition(t) {
    t.string('equals')
    t.field('not', { type: StringFilter })
    t.string('in', { list: [false] })
    t.string('notIn', { list: [false] })
    t.string('lt')
    t.string('lte')
    t.string('gt')
    t.string('gte')
    t.string('contains')
    t.string('startsWith')
    t.string('endsWith')
    t.field('mode', {
      type: enumType({
        name: 'QueryMode',
        members: ['default', 'insensitive'],
      }),
    })
  },
})

but this generates a type as follows

  StringFilter = { // input type
    contains?: string | null; // String
    endsWith?: string | null; // String
    equals?: string | null; // String
    gt?: string | null; // String
    gte?: string | null; // String
    in?: Array<string | null> | null; // [String]
    lt?: string | null; // String
    lte?: string | null; // String
    mode?: NexusGenEnums['QueryMode'] | null; // QueryMode
    not?: NexusGenInputs['StringFilter'] | null; // StringFilter
    notIn?: Array<string | null> | null; // [String]
    startsWith?: string | null; // String
  }

Compare it with the one from prisma, every optional field has a | null attached to it. The only way to get rid of it is to make it required but that is not true, the arg must be optional but not null

How do I generate a type that is just contains?: string and not contains?: string | null?

godness84 commented 3 years ago

@ashwins93 still unsolved, look at this https://github.com/graphql-nexus/nexus/issues/819