Reputation: 73
I have set up a GraphQL database using GraphQL.js and Express that fetches the data from a hardcoded JSON-Object. My JSON-Object looks like this:
exports.store = {
"categories":
{ ... },
"customers":
{ ... },
"vendors":
{ ... },
"items":
{ ... },
"products":
{ ... },
}
With multiple entries for every category and subfields for every entry depending on the category. I have written resolvers for all the categories and can for example get the data for a specific customer by id.
What I want to do now is something similiar to a WHERE-Clause in SQL. An entry for products looks like this:
"1": {
"id":1,
"name":"Coconut",
"category":{
"id":1,
"name":"Fruits"
},
"vendor":{
"id":32,
"name":"Exotic Fruits Company"
}
},
So now I would like to be able to do a query, that gives me all products that have a certain vendor specified by it's id. Can I somehow reuse the resolvers I have already written for this? Is it even possible to have an argument on a subfield that specified which of it's parents are to be returned?
Upvotes: 0
Views: 653
Reputation: 5569
I don't understand what you meant with to have an argument on a subfield that specified which of it's parents are to be returned
.
I read you want to have a resolver that returns only products with a specific vendorId
, this should work:
type Query {
products($vendorId: Int): [Product]
}
const resolvers = {
...
products: ({ vendorId }) => {
return store.products.values().filter(product => product.vendor.id === vendorId);
},
...
};
Please elaborate if that wasn't the case ;)
Upvotes: 1