Type-graphql: Use ObjectType and InputType decorators together

Created on 17 Apr 2018  路  4Comments  路  Source: MichalLytek/type-graphql

I want to use ObjectType and InputType decorators in the same class.
But i'm getting this error at runtime:
Schema must contain unique named types but contains multiple types named "xyz".

Question Solved

Most helpful comment

This should work:

@InputType("PersonInput")
@ObjectType("PersonType")
export class Person {
  @Field()
  name: string;

  @Field()
  dateOfBirth: Date;
}

However, be aware that GraphQL has separate type for input for reasons.
According to the spec: http://facebook.github.io/graphql/October2016/#sec-Input-Objects

The Object type defined above is inappropriate for re鈥恥se here, because Objects can contain fields that express circular references or references to interfaces and unions, neither of which is appropriate for use as an input argument. For this reason, input objects have a separate type in the system.

So you should have different classes for input and output types, even if it partially violate DRY rule.
You can use implement Partial<XYZ> to have typesafe on fields (no field type mismatch between input and entity/output).

All 4 comments

This should work:

@InputType("PersonInput")
@ObjectType("PersonType")
export class Person {
  @Field()
  name: string;

  @Field()
  dateOfBirth: Date;
}

However, be aware that GraphQL has separate type for input for reasons.
According to the spec: http://facebook.github.io/graphql/October2016/#sec-Input-Objects

The Object type defined above is inappropriate for re鈥恥se here, because Objects can contain fields that express circular references or references to interfaces and unions, neither of which is appropriate for use as an input argument. For this reason, input objects have a separate type in the system.

So you should have different classes for input and output types, even if it partially violate DRY rule.
You can use implement Partial<XYZ> to have typesafe on fields (no field type mismatch between input and entity/output).

@winuxue Does the solution work for your problem? Can I close this issue for housekeeping purposes?

Closing for housekeeping purposes.
@winuxue please reopen the issue if the problem still exist.

When integrating it with typeorm entities using the Nullable HOC concept which u have mentioned in other issue getting the same this error Schema must contain uniquely named types but contains multiple types named "CreateGroupInput".

`export const GroupWrapper = (nullable: boolean) => {

@InputType(nullable ? "UpdateGroupInput" : "CreateGroupInput")

@ObjectType('GroupType')

@Entity()

class Group extends DefaultEntity {

@Field({
  nullable: !nullable,
})

@PrimaryGeneratedColumn("uuid")
_id: string;

@Field({
  nullable,
})

@Column({
  unique: true,
})

name: string;

}
return Group`;

`
export const GroupPlanWrapper = (nullable: boolean) => {

@InputType(nullable ? "UpdateGroupPlanInput" : "CreateGroupPlanInput")

@ObjectType("GroupPlanType")

@Entity("group_plan")

class GroupPlan extends DefaultEntity {

@Field((type) => Group)
@ManyToOne(
  () => Group,
  (group) => group._id,
  {
    cascade: true,
    eager: true,
  }
)
@JoinColumn({ name: "group_id" })

group: Promise<Group>;

@Field({ nullable })

@Column()

name: string;

return GroupPlan;
}`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tongtwist picture tongtwist  路  3Comments

limenutt picture limenutt  路  3Comments

MichalLytek picture MichalLytek  路  4Comments

glentakahashi picture glentakahashi  路  3Comments

avkonst picture avkonst  路  3Comments