graphql

mafcocinco 2020-06-08T04:05:51.154Z

Is there any way, perhaps via a function against the context, to know the field name that the current resolver is resolving? The selections-tree, critically, provides the list of selections below the current resolver. It seems it would be useful, in some contexts, to know the name of the field that is currently being resolved. Additionally, is there any way to know if the current field is being resolved as a single value or as a list? I find myself often writing two versions of what are ostensibly the same resolver: foo and foos, with the only different being that one returns a single instance of a foo object and the other a sequence of the same type.

2020-06-08T04:10:13.154900Z

(if you have foos foo is redundant)

mafcocinco 2020-06-08T04:12:08.156800Z

so if a field is resolved to a single instance of foo (rather than a list of foos), if the resolver for that field returns a list (maybe with only one instance of foo in the list), Lacinia will coerce the list into the single instance?

2020-06-08T04:20:44.157200Z

No

2020-06-08T04:22:04.158900Z

I just have a strong preference for collection oriented apis over single item apis, and if the same resolve works for both just use lists

mafcocinco 2020-06-08T04:33:30.160600Z

I see. I just disagree. There are definitely instances where a list of foo objects doesn’t make any sense. It might be more convenient from an implementation perspective but the point of having a schema is to communicate how a data model works to a user and telling them they get a list of things when, in reality, there is only ever going to be one thing in the list seems wrong to me. But, reasonable people could disagree.

mafcocinco 2020-06-08T04:34:54.161600Z

Plus, semantically, it is wrong. There are cases where one thing in a schema maps, uniquely, to something else. Seems like the schema should reflect that relationship.

mafcocinco 2020-06-08T04:35:38.162200Z

First part of my of my question still stands. Is there any way for a resolver to know what field it is currently resolving?

hlship 2020-06-08T15:05:47.163400Z

Not with the current APIs. If you're willing to deal with future disruption, you can check the code (or just print out the context) and see the keys that Lacinia uses to keep track of everything.

mafcocinco 2020-06-08T15:16:11.164900Z

Do you think either/both of these features (i.e. “Am I resolving a single instance or a list?” and “What field am I currently resolving?“) are features that are worth supporting? I.e. has anyone else asked for them besides me? 🙂

mafcocinco 2020-06-08T15:16:50.165600Z

The ability to know if we are resolving a list vs a single object is definitely one that would allow us to reduce duplication in our resolver code.

hlship 2020-06-08T16:51:02.166100Z

An API that identified the current field AND field and argument directives would be useful to many!

👍 3
2020-06-08T23:44:36.167200Z

yeh we extract those via (get-in (:com.walmartlabs.lacinia/selection context) [:field-definition :type]) in our resolvers , and keep an eye out for changes in newer releases , an api would be great tho