Reputation: 1590
I need to know the array item index when it's not returned by downstream response. Suppose my schema looks like
schema:
type Cart {
items: [Item]
}
type Item {
name: String
index: Int
}
data comes:
{items: [{name: apple}, {name: mango},]}
resolvers look like:
Cart: {
items(obj, context, info) {
return obj;
}
}
Item: {
name(obj, context, info) {
return obj.name;
}
index(obj, context, info) {
// how to get it?
}
}
How to get ith index
in items array?
Upvotes: 2
Views: 3416
Reputation: 2932
If you want to reutrn multiple item for one card in item then
type Cart {
items: [Item]!
}
type Item {
name: String!
index: Int!
}
Query {
cardItems: [Cart]!
}
resolver {
cardItems : () => {
//Perform opration and make result like this to run query properly
return [{ items: [{ name: 'name1', index: 0 }, ...]}]
//Which is array of items contain name and index
}
}
// Query to get result
{
cardItems {
items {
name
index
}
}
}
But if you wan to specific index of item in a cart you should taken id or index unique key and make anohter query
Query {
itemById(itemId: String!): Item
}
resolver {
itemById({ itemId }) {
//perform opration and return item with respeact to itemId
return { name: 'string', index: 1}
},
}
//Query to get result
{
itemById(itemId: "1"){
name
index
}
}
resolver reutrn index or itemId based result.
Upvotes: 2