Graphql: IntersectionType is not composable (wrong properties)

Created on 10 Jun 2020  路  1Comment  路  Source: nestjs/graphql

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request

Current behavior


When using IntersectionType together with other type mapping utility functions the generated class will provide wrong properties.

Expected behavior


Hava a look at the code example. There you will see three Classes Foobar, FoobarCreate and FoobarUpdate. At the buttom you will find a class Test and a constructor where a variable of type FoobarUpdate gets initialised. I have commented the wrong class properties there.

Minimal reproduction of the problem with instructions

import { ObjectType, Field, ID, InputType, PartialType, OmitType, IntersectionType, PickType } from '@nestjs/graphql';

@ObjectType()
export class Foobar {
    @Field(() => ID)
    id: string;

    @Field(() => String)
    name: string;

    @Field(() => String)
    required: string;

    @Field(() => String, {nullable: true})
    optional?: string;

}

@InputType() // { required: string, optional?: string }
export class FoobarCreate extends OmitType(Foobar, ['id', 'name'], InputType) {} 

@InputType() // { id: string, required?: string, optional?: string }
export class FoobarUpdate extends IntersectionType(PartialType(FoobarCreate), PickType(Foobar, ['id'], InputType)) {}

class Test {

    constructor() {

        const foobarUpdate: FoobarUpdate = {
            id: 'some-id', // works as expected
            name: 'some-name', // should not be part of FoobarUpdate class (but is a required property)
            required: 'some-required-value', // should be optional property (but is a required property)
            optional: "some-value" // works as expected
        }

    }
}

Environment


Nest version: 7.1.4
Node version: v12.16.2  

Most helpful comment

https://docs.nestjs.com/graphql/mapped-types#composition

image

This part:

 ['email'] as const

is very important.

Please, test your code with as const added (to both OmitType and PickType calls).

>All comments

https://docs.nestjs.com/graphql/mapped-types#composition

image

This part:

 ['email'] as const

is very important.

Please, test your code with as const added (to both OmitType and PickType calls).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radarsu picture radarsu  路  3Comments

zhenwenc picture zhenwenc  路  4Comments

vnenkpet picture vnenkpet  路  3Comments

liudonghua123 picture liudonghua123  路  3Comments

fenos picture fenos  路  5Comments