简体   繁体   English

GraphQL 用于在突变字段上映射数组的语法

[英]GraphQL Syntax for mapping array on mutation field

I'm looking for advice on syntax.我正在寻找有关语法的建议。 In my query, I need my backend (Graphlite, Symfony) to get the child items.在我的查询中,我需要我的后端(Graphlite、Symfony)来获取子项。 The way I get the regular item is as below, and works correctly.我得到常规项目的方式如下,并且工作正常。 I'm not well versed enough in GraphQL / Apollo / VueJS / GraphQLite to know which technology owns the "{id: $itemId}" syntax, nor do I know what it is called, so I can't find any info about it.我对 GraphQL / Apollo / VueJS / GraphQLite 不够精通,无法知道哪种技术拥有“{id: $itemId}”语法,我也不知道它叫什么,所以我找不到任何有关它的信息.

insertItem: gql` mutation insertItem(
    $itemId: Uuid!,
    $childItemIds: [Uuid!]!
) {
    insertItem(
        item: {id: $itemId},
        childItems:  { id: { in : { $childItemIds} }
    ){
       // ... stuff
    }
}`

So, given that the {id: $itemId} correctly works for getting an item, I assume there is some graphQL syntax that would work to apply the childItemIds to get the childItems.因此,鉴于 {id: $itemId} 正确地用于获取项目,我假设有一些 graphQL 语法可以应用 childItemIds 来获取 childItems。 Is there a name for this type of mapping?这种映射有名称吗? What would the syntax be?语法是什么?

As per discussion with @UjinT34, since this couldn't be done inline with GraphQL, I created an array of ItemInput before calling the mutation:根据与@UjinT34 的讨论,由于这不能与 GraphQL 内联完成,我在调用突变之前创建了一个 ItemInput 数组:

          for (var i = 0, len = item.childItems.length; i < len; i++) {
            childItems.push({ id : item.childItems[i].id } )
          }

The original query then became:原来的查询变成了:

insertItem:`` gql` mutation insertItem(

    $itemId: Uuid!,
    $childItemIds: [ItemInput!]!
) {
    insertItem(
        item: {id: $itemId},
        childItems:  $childItemIds
    ){
       // ... stuff
    }
}

Once I understood that the {id: $item} wasn't a magic GQL/GraphQLite syntax and was simply an inline javascript object being created, the solution became fairly simple.一旦我明白{id: $item}不是神奇的 GQL/GraphQLite 语法,而只是一个内联 javascript object 正在创建,解决方案就变得相当简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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