Type-graphql: @Arg with array of inputs

Created on 18 Apr 2019  路  11Comments  路  Source: MichalLytek/type-graphql

Hi there! I had a problem with @arg.

I wanted to use @Arg with array of input objects, but get error like this:
You need to provide explicit type for GetOrderResolver#addOrder parameter #1

Is there a way to do something like this?
@Arg('items') items: ItemInput[] (I know syntax like this wouldn't work)

Question Solved

Most helpful comment

@Arg('items', type => [ItemInput]) items: ItemInput[]

All 11 comments

@Arg('items', type => [ItemInput]) items: ItemInput[]

@Arg('items', type => [ItemInput]) items: ItemInput[]

Thanks, that was fast! Was it in documentation and I simply couldn't find it or it wasn't here?

Im trying to replicate the same thing but however i'm not winning

for me it is also not working. @Args('postCreateInputList', type => [PostCreateInput])
postCreateInputList: [PostCreateInput] is giving me the following error:

Argument of type '(type: any) => (typeof PostCreateInput)[]' is not assignable to parameter of type 'PipeTransform | Type>'.
Type '(type: any) => (typeof PostCreateInput)[]' is not assignable to type 'Type>'.
Type '(type: any) => (typeof PostCreateInput)[]' provides no match for the signature 'new (...args: any[]): PipeTransform'.
Overload 3 of 4, '(options: ArgsOptions, ...pipes: (PipeTransform | Type>)[]): any', gave the following error.
Argument of type '"postCreateInputList"' is not assignable to parameter of type 'ArgsOptions'.
@Args('postCreateInputList', type => [PostCreateInput])

Kind regards, Gerry

@innoveltec Because you use NestJS and it has a different syntax - { name: "name", type: () => [Type]}

ok it's working fine now. Great job!

What is the difference between @Args() and @Arg()? I have seen many examples with the latter, but I am using NestJS and only the plural seems to work.

I'm looking for the syntax @innoveltec wound up with, considering @MichalLytek provided curly braces. My example is this:

async createHouseholdwithAddy(@Args('data') houseAdd: HouseAddyCreate)

Where HouseAddyCreate is an intersection type of two @InputType() classes, like:
type HouseAddyCreate = HouseholdCreate & AddressCreate;
but the error is this (only at run time):
(node:5340) UnhandledPromiseRejectionWarning: Error: You need to provide explicit type for HouseholdResolver#createHouseholdwithAddy parameter #0 ! at Object.findType (/Users/rjr/Documents/Proteus/MiddleLayerServers/prisma/proteuserver2/node_modules/type-graphql/dist/helpers/findType.js:17:15) at Object.getParamInfo (/Users/rjr/Documents/Proteus/MiddleLayerServers/prisma/proteuserver2/node_modules/type-graphql/dist/helpers/params.js:9:49) at Arg (/Users/rjr/Documents/Proteus/MiddleLayerServers/prisma/proteuserver2/node_modules/type-graphql/dist/decorators/Arg.js:9:159)

What is the difference between @Args() and @Arg()?

@Args() doesn't allow to pass an arg name as a parameter and it only works with @ArgsType classes.
@Arg() is for single named arguments and accepts input types or scalars, not @ArgsType.

However, NestJS uses only @Args(), so @Arg("name") = @Args("name") in that case.

type HouseAddyCreate = HouseholdCreate & AddressCreate;

Types doesn't exist on runtime - you need to create an @InputType class that describes all the fields and types using decorators.

Thanks for the added info, Michal! I did try your suggestion which was to create a new InputType and I added the second type as a field. This works! However, can you advise how I might remove duplicate fields, since it is not quite an intersection.

@InputType()
export class HouseholdWAddy extends HouseholdCreate {
  @Field(type => AddressCreate)
  addy: AddressCreate

  //TO-DO:
  // need to drop addresses requirement for HouseholdCreate (since creating)
  // and user within AddressCreate class since redundant
  // it's possible addressline2 is showing up as required?
}

@theRocket See #453 馃槈

Was this page helpful?
0 / 5 - 0 ratings