graphql-nexus / nexus

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

Not possible to define sourceType with another objectType #1129

Open lmanerich opened 2 years ago

lmanerich commented 2 years ago

Related to the already fixed issue: https://github.com/graphql-nexus/nexus/issues/1006

It was created a new field to define the resolver item response object. But it is only possible for primitive data types. Also, would be great to inherite the sourceType by "t.string()" method, so we only need to declare complex data structures. Which can be resolved to NexusGenRootTypes[${sourceType}]

objectType({
    name: 'Post',
    definition(t) {
        t.id('id');
        t.string('label', {
            resolve: PostResolver.label,
            sourceType: 'string',
        });
        t.field('user', {
            type: 'User',
            resolve: PostResolver.user,
            sourceType: 'User',
        });
    },
}

Ends up generating:

export interface NexusGenObjects {
  Query: {};
  Post: { // root type
    id?: string | null; // ID
    label: string
    user: User ///// NOT DEFINED
  }

As a workaround it is possible to declare the type as

sourceType: "NexusGenRootTypes['User']",
lmanerich commented 2 years ago

If you are trying to make a nullable field you would also need to do this in the workaround version:

sourceType: "NexusGenRootTypes['User'] | undefined",