Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Arg with array of inputs #314

Closed
demouserforsale opened this issue Apr 18, 2019 · 11 comments
Closed

@Arg with array of inputs #314

demouserforsale opened this issue Apr 18, 2019 · 11 comments
Labels
Question ❔ Not future request, proposal or bug issue Solved ✔️ The issue has been solved

Comments

@demouserforsale
Copy link

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)

@MichalLytek
Copy link
Owner

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

@MichalLytek MichalLytek added Question ❔ Not future request, proposal or bug issue Solved ✔️ The issue has been solved labels Apr 18, 2019
@demouserforsale
Copy link
Author

@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?

@Numbers-sa
Copy link

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

@innoveltec
Copy link

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

Kind regards, Gerry

@MichalLytek
Copy link
Owner

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

@innoveltec
Copy link

ok it's working fine now. Great job!

@theRocket
Copy link

theRocket commented Dec 13, 2019

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)

@MichalLytek
Copy link
Owner

MichalLytek commented Dec 14, 2019

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.

@theRocket
Copy link

theRocket commented Dec 16, 2019

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?
}

@MichalLytek
Copy link
Owner

@theRocket See #453 😉

@theRocket
Copy link

Got it, thanks! I was just looking at Omit: https://www.typescriptlang.org/docs/handbook/utility-types.html#omittk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question ❔ Not future request, proposal or bug issue Solved ✔️ The issue has been solved
Projects
None yet
Development

No branches or pull requests

5 participants