Skip to content

PropertyDataFetcher doesn't follow it's specification #3813

@ooraini

Description

@ooraini

Describe the bug
Property data fetcher doesn't follow its specification:

This is the default data fetcher used in graphql-java, and it will examine maps, records and POJO java beans for values that match the desired name, typically the field name, or it will use a provided function to obtain values.
It uses the following strategies
If the source is null, return null
If the source is a Map, return map. get(propertyName)
If a function is provided, it is used
Find a public JavaBean getter method named `getPropertyName()` or `isPropertyName()`
Find any getter method named `getPropertyName()` or `isPropertyName()` and call method. setAccessible(true)
Find a public field named `propertyName`
Find any field named `propertyName` and call field. setAccessible(true)
Find a public Record like method named `propertyName()`
If this cant find anything, then null is returned
You can write your own data fetchers to get data from some other backing system if you need highly customised behaviour.

From the JavaDoc getter methods ARE checked before 'record like' methods, the problem is in PropertyFetchingImpl it uses LambdaFetchingSupport.createGetter which in turn looks for record like methods before getters.

Either the JavaDoc can be fixed or the implementation. I lean towards changing the implementation as(in my opinion) getter methods on records allow for more flexible resolution. I use the following pattern a lot in my code:

record User(Map<X,Y> files) {
  public Collection<Y> getFiles() {
    return files.values();
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions