反应-波罗的动态突变文件[英] Dynamic mutation document for react-apollo

本文是小编为大家收集整理的关于反应-波罗的动态突变文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要动态更改我的突变文档,以便能够在单个突变中创建多个项目.因此,我拥有此功能createOrderName,它需要一个整数并能够创建正确的突变文档.例如. createOrderName(2)获取

mutation createOrderMut($input0: AddToOrderMenuItemConnectionInput!, $input1: AddToOrderMenuItemConnectionInput!) {
  input0: addToOrderMenuItemConnection (input:$input0) {
    changedOrderMenuItem {
      id
    }
  }
  input1: addToOrderMenuItemConnection (input:$input1) {
    changedOrderMenuItem {
      id
    }
  }
}

我的容器如下.

const CartContainer = compose(
  graphql(createOrderName(2), {
    props: ({ mutate }) => ({
      addToOrderMenuItem: (menus, orderId) => mutate({
        variables: createOrdersInput(menus, orderId)
      })
    })
  })
)(CartView)

现在,如何将整数值传递给该突变,以创建正确的突变文档?目前,它可以解决2,但是我需要更灵活,以便我可以创建任何数量的项目...

推荐答案

这听起来像是您正在使用的后端的不幸限制.进行批处理突变的正确方法是在服务器上拥有一个单个突变字段,该突变字段接受列表参数,其中所有要插入的项目.因此,Apollo并非旨在用标准react-apollo API支持这种动态查询生成.这是因为我们坚信使用静态查询要比在运行时生成字段要好得多: https://dev-blog.apollodata.com/5-benefits-of-static-static-graphql-queries-b7fa90b69a#.hp710vxe7

考虑到这种情况,听起来像是一代突变字符串动态是一个不错的选择.您可以直接使用阿波罗而不是通过graphql hoc来做到这一点.您可以使用withApollo hoc来做到这一点: http://.apollodata.com/react/giress-ordor-components.html#withapollo

import { withApollo } from 'react-apollo';

const MyComponent = ({ client }) => {
  function mutate() {
    const dynamicMutationString = // generate mutation string here

    client.mutate({
      mutation: gql`${dynamicMutationString}`,
      variables: { ... },
    }).then(...);
  }

  return <button onClick={mutate}>Click here</button>;
}

const MyComponentWithApollo = withApollo(MyComponent);

我们为此目的构建了此额外的API - 当标准内容不够时.

这是mutate btw的文档: http:http:http:http://dev.apollodata.com/core/apollo-client-api.html#apolloclient.mutate

其他推荐答案

我不确定是否可以通过您当前的实施来回答您的问题,因此我敦促您重新考虑突变定义并使用GraphQLList和GraphQLInputObject.

.

因此,根据需要突变的字段:

args: {
  input: {
    type: new GraphQLList(new GraphQLInputObjectType({
      name: 'inputObject',
      description: 'Your description here',
      fields: {
        id: { type: GraphQLInt }
      },
    })),
  },
},

这样,您可以在突变呼叫中提供n个对象,并在您的类型上获取列表:

{
  mutation myMutation {
    addToOrderMenuItemConnection(input: [{ id: 123 }, { id: 456 }]) {
      id
    }
  }
}

再次,我不熟悉您的最终目标,但我认为这将为您提供灵活性,以便将来更改/更新,因为您正在处理对象输入而不是个人参数,这也将(希望)将您与未来的破裂变化隔离.

本文地址:https://www.itbaoku.cn/post/1938129.html

问题描述

I need to change my mutation document dynamically to be able to create multiple items in a single mutation. So I have this function createOrderName that takes an integer and be able to create the correct mutation document. Eg. createOrderName(2) gets

mutation createOrderMut($input0: AddToOrderMenuItemConnectionInput!, $input1: AddToOrderMenuItemConnectionInput!) {
  input0: addToOrderMenuItemConnection (input:$input0) {
    changedOrderMenuItem {
      id
    }
  }
  input1: addToOrderMenuItemConnection (input:$input1) {
    changedOrderMenuItem {
      id
    }
  }
}

And my container is as follow.

const CartContainer = compose(
  graphql(createOrderName(2), {
    props: ({ mutate }) => ({
      addToOrderMenuItem: (menus, orderId) => mutate({
        variables: createOrdersInput(menus, orderId)
      })
    })
  })
)(CartView)

Now how can I pass an integer value to this mutation in order for it to create the correct mutation document? Currently it's fix to 2, but I need it to be more flexible so I can create any number of items...

推荐答案

This sounds like an unfortunate limitation of the backend you're working with. The right way to do a batch mutation would be to have a single mutation field on the server that accepts a list argument with all of the items you want to insert. Thus, Apollo isn't designed to support such dynamic query generation with the standard react-apollo API. That's because we strongly believe that working with static queries is much better than generating fields at runtime: https://dev-blog.apollodata.com/5-benefits-of-static-graphql-queries-b7fa90b0b69a#.hp710vxe7

Given the situation, though, sounds like generation the mutation string dynamically is a good option. You can do this by using Apollo directly instead of through the graphql HoC. You can use the withApollo HoC to do this: http://dev.apollodata.com/react/higher-order-components.html#withApollo

import { withApollo } from 'react-apollo';

const MyComponent = ({ client }) => {
  function mutate() {
    const dynamicMutationString = // generate mutation string here

    client.mutate({
      mutation: gql`${dynamicMutationString}`,
      variables: { ... },
    }).then(...);
  }

  return <button onClick={mutate}>Click here</button>;
}

const MyComponentWithApollo = withApollo(MyComponent);

We built this extra API for just this purpose - when the standard stuff is not enough.

Here are the docs for mutate btw: http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient.mutate

其他推荐答案

I'm not sure if I can answer your question with your current implementation, so I'm going to urge you to rethink your mutation definition and use GraphQLList and GraphQLInputObject.

So, depending on the fields you need to mutate:

args: {
  input: {
    type: new GraphQLList(new GraphQLInputObjectType({
      name: 'inputObject',
      description: 'Your description here',
      fields: {
        id: { type: GraphQLInt }
      },
    })),
  },
},

This way you can provide n-number of objects into your mutate call, and get a list back on your type:

{
  mutation myMutation {
    addToOrderMenuItemConnection(input: [{ id: 123 }, { id: 456 }]) {
      id
    }
  }
}

Again, I'm not 100% familiar with your end-goal, but I think this would give you flexibility for future changes/updates as well since you're dealing with an object input rather than individual arguments, which would also (hopefully) insulate you from future breaking-changes.