Spring Boot GraphQL Tutorial #14 - DataFetchingEnvironment

preview_player
Показать описание
Every resolver is passed a DataFetchingEnvironment object which allows it to know more about what is being fetched and what arguments have been provided. To get access to this class, set it as the last parameter of your resolver method. It will be automatically injected.

Here are some of my favorite DataFetchingEnvironment use-cases from production. Methods:

getArguments() - this represents the arguments that have been provided on a field and the values of those arguments that have been resolved from passed in variables, literals and default argument values.

getContext() - the context is object is set up when the query is first executed and stays the same over the lifetime of the query. The context can be any value and is typically used to give each data fetcher some calling context needed when trying to get field data. For example the current user authorization context can be created and set at the very start of the query, then all resolvers can access this context and pass it to the downstream services or resources.

ExecutionStepInfo getExecutionStepInfo() - Explains how this resolver was executed. I.e. the steps executed prior. This is available in a nice path format. Have a look

DataFetchingFieldSelectionSet getSelectionSet() - the selection set represents the child fields that have been “selected” under neath the currently executing field. This can be useful to help look ahead to see what sub field information a client wants. I have a dedicated video on this.

ExecutionId getExecutionId() - each query execution is given a unique id. You can use this perhaps on logs to tag each individual query or use a correlation and span id.

DataLoaderRegistry() - This will provide access to the graphql dataloaders, use to solve the n+1 problem.

See you in the next episode, take it easy guys.

Cheers!
Philip

Рекомендации по теме
Комментарии
Автор

Hi Philip,

I’m always getting env.getcontext as null any idea why or what I’m missing

spandanank
Автор

Imagine if we have mapping and we want post api, how would we create using graphQL

jay-rathod-
Автор

Hey Philip,

Thanks for these videos!!! very helpful

1. Is it possible to pass the parameters and the DataFetchingEnvironment from GrapQLQuery resolver to the field resolver?

2. When we deploy the GraphQL app to openshift, will the autoConfiguration to read the graphqls files from the resource folder in the project still works?

vinodhoonur
Автор

Hi Philip, great videos. Do you have any plans on showing how the resolvers can interact and pull the data from an actual database? A tutorial on linking graphQL resolvers to actually querying your DB using JPA entities or repositories would be super helpful.

BrendenBishop