Skip to content

Instantly share code, notes, and snippets.

@dave-nicholas
Last active January 4, 2018 13:05
Show Gist options
  • Save dave-nicholas/05f698dd951f8a9fdfbf5df29fa8ee62 to your computer and use it in GitHub Desktop.
Save dave-nicholas/05f698dd951f8a9fdfbf5df29fa8ee62 to your computer and use it in GitHub Desktop.
apollo server resolvers for blog article
const resolvers = {
River: {
async streams(river) {
return await Streams.find({ riverId: river.id }).toArray();
}
},
Stream: {
async river(stream) {
return await Streams.findOne({ riverId: stream.riverId });
},
async brooks(stream) {
return await Brooks.find({ streamId: stream.id }).toArray();
}
},
Brook: {
async stream(brook) {
return await Brooks.findOne({ streamId: brook.streamId });
}
},
Query: {
async river(_, { id }) {
return await River.find({ id }).toArray(); //find using mongodb
},
async rivers() {
return River.find({}).toArray(); //find using mongodb
},
async BrooksByRiverName(_, { name }) {
//This search uses algolia to defer full text searches
const { hits } = await index.search(name);
return !!hits.length
? Brook.find({ id: hits.map(hit => hit.brookId) }).toArray()
: null;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment