I am new to GraphQL and am not sure how to implement queries on sub fields.
For example, say we have the following types and queries:
type Author {
joinedDate: String,
quote: String
}
type Post {
id: String,
author: Author,
text: String,
timeStamp: String,
location: String
}
type Query {
posts(authorId: String): [Post]
}
Clients could then make requests like:
// gets everything
{
posts(authorId: Steve)
}
// just gets time, text, and location
{
posts(authorId: Steve) {
timeStamp
text
location
}
}
The root object could then implemented like so:
const root = {
posts: (authorId) => {
return dataService.getPosts(authorId)
}
}
My question is how would you implement queries/filters on the sub fields. For example:
// just gets time, text, and location for specific date range
{
posts(authorId: Steve) {
timeStamp(minDate: 01012015, maxDate: 10102018)
text
location
}
}
How would I define that root method? Would I need to filter the full list of posts manually in the posts
root method?
const root = {
// would this method even have access to the date args?
posts: (authorId, minDate, maxDate) => {
const newList = filterTheList(
dataService.getPosts(authorId),
minDate,
maxDate
)
return newList
}
}
Thanks for reading!