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
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad