Ad

How To Send List Of Ids As Param With Graphql

I'm using React, Graphql and Apollo.

I have function as follows:

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);
            })

    }

And mutation is as follows:

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

And at the backend I use graphene and the mutation is as follows:

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))

But I get string in ids variable as follows:

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

instead of :

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

How can I send an array of ids and how can I get it on the backend?

Any idea?

Ad

Answer

The problem is because ids is of type String.

In your schema the type of the argument ids is String, so in order to be able to have something like ['3692', '3695', '3704'] you would have to define the argument into a List of Strings.

Something like:

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

And in the mutation you would also have to update the param ids into:

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

Hope it helps.

Ad
source: stackoverflow.com
Ad