Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "filtering" and "sorting" support to fields that rely on custom resolvers #5005

Open
btroop opened this issue Apr 13, 2024 · 1 comment
Labels
feature request New feature or request

Comments

@btroop
Copy link

btroop commented Apr 13, 2024

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.

//clientQuery.js

query Person($where: PersonWhere) {
  persons(where: $where) {
    firstName
    lastName
  }
}


{
  "where": {
    "age_GT": 18
  }
}

Possible??

@btroop btroop added the feature request New feature or request label Apr 13, 2024
@btroop
Copy link
Author

btroop commented Apr 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant