简体   繁体   中英

Define GraphQL object type in .graphql files not working

I'm trying to make a mutation in type-graphql and one of the arguments is Object so:

@Mutation(() => Boolean)
async createProduct(
    @Arg('name', () => String) name: string,
    @Arg('price', () => Int) price: number,
    @Arg('images', () => [InputImages]) images: [ImagesArray],
) {
    // Code
}

So i made an input type for argument "images":

@InputType()
class InputImages {
  @Field(() => String)
  filename: string;
}

And an Object type:

@ObjectType()
export class ImagesArray {
    @Field(() => String)
    filename: string
}

Now i am trying to use GraphQL Code Generator and i need to write queries and mutations on client side as a .graphql file extension, so i have:

mutation CreateProduct(
    $name: String!
    $price: Int!
    $images: [ImagesArray]!
) {
    createProduct(
        name: $name
        price: $price
        images: $images
    );
}

And now when i try to run "npm run gen" says:

GraphQLDocumentError: Unknown type "ImagesArray".

I tried to add on top:

type ImagesArray {
    filename: String
}

Does not work again, any idea?

  1. You don't need ImagesArray type in args:
@Arg('images', () => [InputImages]) images: InputImages[],
  1. Update your mutation type name:
mutation CreateProduct(
    $name: String!
    $price: Int!
    $images: [InputImages!]!
) {
  # ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM