You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My take on custom resolvers is that they are an excellent way to generate calculated fields, i.e., a field output that doesn't require storing additional data in the database.
However, as far as I can tell (and maybe I'm missing something, but I can't find any documentation and my apollo sandbox also seems to confirm this), fields that use custom resolvers do not have the same support in terms of query-ability, primarily with regards to the the "filter" functions with the appropriate logical operators (AND, OR, GT, LT, etc.) or the "sort" function (ASC, DESC). The type nature of GraphQL ensures that the type returned by the Custom Resolver matches the type defs, so this isn't a question of "what kind of data is the resolver returning".
What would be great is to include these capabilities for custom resolved fields.
Here is an example:
Say I have a Person type with a required dateOfBirth field. This is the only piece of information I need to store in the DB regarding their age.
//typedefs.js
type Person {
firstName: String!
lastName: String
dateOfBirth: Date!
}
If I want to calculate their age, I can do so with a custom resolver
//typedefs.js
type Person {
...
dateOfBirth:Date!
age: Number @customResolver (requires: "dateOfBirth")
}
// resolver.js
Person: {
age (source) {
const today = new Date()
const ageInMs= today - source.DateOfBirth
const ageInYrs = Math.floor( ageInMs / 1000 / 60 / 60 / 24 / 365)
return ageInYrs
}
}
And now the magic sauce...
When querying the Person type, I should be able to use "filter" or "sort" logic against the age field; right now, I have to do a combination of querying against the dateOfBirth field and running the math/filtering/sorting on the client, which is far from ideal.
i came up with a kind-of workaround leveraging the @cypher directive, and provides the sort capabilities, but still not the filter capabilities. Looks something like this:
type Person {
...
dateOfBirth:Date!
age: Number @cypher(statement:
"""
return duration.between(date(this.dateOfBirth),date()).years as age
""",
columnName: "age"
)
}
This obviously only work for if the built in cypher functions can handle the calculation required, but it kind of works for simple ones, like some regular math, date math, string manipulation (toLower, toUpper, etc.), etc.
My take on custom resolvers is that they are an excellent way to generate calculated fields, i.e., a field output that doesn't require storing additional data in the database.
However, as far as I can tell (and maybe I'm missing something, but I can't find any documentation and my apollo sandbox also seems to confirm this), fields that use custom resolvers do not have the same support in terms of query-ability, primarily with regards to the the "filter" functions with the appropriate logical operators (AND, OR, GT, LT, etc.) or the "sort" function (ASC, DESC). The type nature of GraphQL ensures that the type returned by the Custom Resolver matches the type defs, so this isn't a question of "what kind of data is the resolver returning".
What would be great is to include these capabilities for custom resolved fields.
Here is an example:
Say I have a Person type with a required dateOfBirth field. This is the only piece of information I need to store in the DB regarding their age.
If I want to calculate their age, I can do so with a custom resolver
And now the magic sauce...
When querying the Person type, I should be able to use "filter" or "sort" logic against the age field; right now, I have to do a combination of querying against the dateOfBirth field and running the math/filtering/sorting on the client, which is far from ideal.
Possible??
The text was updated successfully, but these errors were encountered: