如何使用graphql发送id列表作为参数

问题描述 投票:0回答:1

我正在使用React,Graphql和Apollo。

我的功能如下:

updateSlots(updateSlots, data){
        const {slotIds, refetch} = this.props;
        const {disabled, printEntry, toggleSlotForm} = data;

        updateSlots({variables: {disabled: disabled, printEntry: printEntry, ids: slotIds}})
            .then(res => {
                if (res.data.updateSlots.ok) {

                    refetch();
                    this.setState({hasError: false, errorMessage: "", saved: true, slot: initialSlot()}, () => {
                        setTimeout(() => toggleSlotForm(), 3000)
                    });
                }
            })
            .catch(err => {
                debugger;
                let error_msg = err.message.replace("GraphQL error: ", '');
                this.setState({hasError: true, saved: false, errorMessage: error_msg});
                throw(error_msg);
            })

    }

mutation如下:

const UPDATE_SLOTS = gql`
  mutation updateSlots($disabled: Boolean!, $printEntry: Boolean!, $ids: String!) {
    updateSlots(disabled: $disabled, printEntry: $printEntry, ids: $ids) {
        ok
    }
  }
`;

在后端我使用石墨烯,突变如下:

class UpdateSlots(graphene.Mutation):
    class Arguments:
        disabled = graphene.Boolean(required=True)
        printEntry = graphene.Boolean(required=True)
        ids = graphene.String(required=True)

    ok = graphene.Boolean()

    @staticmethod
    def mutate(root, info, disabled, printEntry, ids):
        pdb.set_trace()
        ok = False

        try:
            ok = True
            for id in ids:
                print(id)
        except Exception as e:
            ok = False
            raise GraphQLError(str(e))

但我在ids variable得到如下字符串:

"['3692', '3695', '3704']"

代替 :

['3692', '3695', '3704']

如何发送一组id,如何在后端获取?

任何的想法?

reactjs graphql apollo react-apollo graphene-python
1个回答
0
投票

问题是因为idsString类型。

在你的schema中,ids论证的类型是String,所以为了能够拥有像['3692', '3695', '3704']这样的东西,你必须将参数定义为ListStrings

就像是:

class UpdateSlots(graphene.Mutation):
    class Arguments:
        disabled = graphene.Boolean(required=True)
        printEntry = graphene.Boolean(required=True)
        ids = graphene.List(graphene.String(required=True))

在变异中你还必须将param ids更新为:

const UPDATE_SLOTS = gql`
  mutation updateSlots($disabled: Boolean!, $printEntry: Boolean!, $ids: [String]!) {
    updateSlots(disabled: $disabled, printEntry: $printEntry, ids: $ids) {
        ok
    }
  }
`;

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.