Interface DataFetchingEnvironment

All Superinterfaces:
IntrospectionDataFetchingEnvironment
All Known Implementing Classes:
DataFetchingEnvironmentImpl, DelegatingDataFetchingEnvironment

@PublicApi @NullMarked public interface DataFetchingEnvironment extends IntrospectionDataFetchingEnvironment
A DataFetchingEnvironment instance of passed to a DataFetcher as a execution context and its the place where you can find out information to help you resolve a data value given a graphql field input
  • Method Details

    • getSource

      <T> @Nullable T getSource()
      This is the value of the current object to be queried. Or to put it differently: it is the value of the parent field.

      For the root query, it is equal to {getRoot()

      Specified by:
      getSource in interface IntrospectionDataFetchingEnvironment
      Type Parameters:
      T - you decide what type it is
      Returns:
      can be null for the root query, otherwise it is never null
    • getArguments

      Map<String,Object> getArguments()
      Specified by:
      getArguments in interface IntrospectionDataFetchingEnvironment
      Returns:
      the arguments that have been passed in via the graphql query
    • containsArgument

      boolean containsArgument(String name)
      Returns true of the named argument is present
      Parameters:
      name - the name of the argument
      Returns:
      true of the named argument is present
    • getArgument

      <T> @Nullable T getArgument(String name)
      Returns the named argument
      Specified by:
      getArgument in interface IntrospectionDataFetchingEnvironment
      Type Parameters:
      T - you decide what type it is
      Parameters:
      name - the name of the argument
      Returns:
      the named argument or null if it's not present
    • getArgumentOrDefault

      <T> T getArgumentOrDefault(String name, T defaultValue)
      Returns the named argument or the default value
      Type Parameters:
      T - you decide what type it is
      Parameters:
      name - the name of the argument
      defaultValue - the default value if the argument is not present
      Returns:
      the named argument or the default if it's not present
    • getContext

      @Deprecated(since="2021-07-05") <T> @Nullable T getContext()
      Deprecated.
      - use getGraphQlContext() instead
      Returns a legacy context argument that is set up when the GraphQL.execute(graphql.ExecutionInput) method is invoked.

      This is an info object which is provided to all DataFetchers, but never used by graphql-java itself.

      Type Parameters:
      T - you decide what type it is
      Returns:
      can be null
    • getGraphQlContext

      GraphQLContext getGraphQlContext()
      Returns a shared context argument that is set up when the GraphQL.execute(graphql.ExecutionInput) )} method is invoked.

      This is an info object which is provided to all DataFetchers.

      Returns:
      can NOT be null
    • getLocalContext

      <T> @Nullable T getLocalContext()
      This returns a context object that parent fields may have returned via DataFetcherResult.getLocalContext() which can be used to pass down extra information to fields beyond the normal getSource()

      This differs from getGraphQlContext() in that it's field specific and passed from parent field to child field, whilst getGraphQlContext() is global for the whole query.

      If the field is a top level field then 'localContext' equals null since it's never be set until those fields execute.

      Type Parameters:
      T - you decide what type it is
      Returns:
      can be null if no field context objects are passed back by previous parent fields
    • getRoot

      <T> @Nullable T getRoot()
      This is the source object for the root query.
      Type Parameters:
      T - you decide what type it is
      Returns:
      can be null
    • getFieldDefinition

      GraphQLFieldDefinition getFieldDefinition()
      Returns:
      the definition of the current field
    • getFields

      @Deprecated(since="2018-12-20") List<Field> getFields()
      Deprecated.
      Returns:
      the list of fields
    • getMergedField

      MergedField getMergedField()
      It can happen that a query has overlapping fields which are querying the same data. If this is the case they get merged together and fetched only once, but this method returns all of the Fields from the query.

      Most of the time you probably want to use getField().

      Example query with more than one Field returned:

       
      
            query Foo {
                bar
                ...BarFragment
            }
      
            fragment BarFragment on Query {
                bar
            }
       
       
      Returns:
      the list of fields currently queried
    • getField

      Field getField()
      Returns:
      returns the field which is currently queried. See also getMergedField().
    • getFieldType

      GraphQLOutputType getFieldType()
      Returns:
      graphql type of the current field
    • getExecutionStepInfo

      ExecutionStepInfo getExecutionStepInfo()
      Returns:
      the field ExecutionStepInfo for the current data fetch operation
    • getParentType

      GraphQLType getParentType()
      Specified by:
      getParentType in interface IntrospectionDataFetchingEnvironment
      Returns:
      the type of the parent of the current field
    • getGraphQLSchema

      GraphQLSchema getGraphQLSchema()
      Specified by:
      getGraphQLSchema in interface IntrospectionDataFetchingEnvironment
      Returns:
      the underlying graphql schema
    • getFragmentsByName

      Map<String,FragmentDefinition> getFragmentsByName()
      Returns:
      the FragmentDefinition map for the current data fetch operation
    • getExecutionId

      ExecutionId getExecutionId()
      Returns:
      the ExecutionId for the current data fetch operation
    • getSelectionSet

      Returns:
      the DataFetchingFieldSelectionSet for the current data fetch operation
    • getQueryDirectives

      QueryDirectives getQueryDirectives()
      This gives you access to the directives related to this field
      Returns:
      the QueryDirectives for the currently executing field
      See Also:
    • getDataLoader

      <K, V> @Nullable org.dataloader.DataLoader<K,V> getDataLoader(String dataLoaderName)
      This allows you to retrieve a named dataloader from the underlying DataLoaderRegistry
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      dataLoaderName - the name of the data loader to fetch
      Returns:
      the named data loader or null
      See Also:
      • DataLoaderRegistry.getDataLoader(String)
    • getDataLoaderRegistry

      org.dataloader.DataLoaderRegistry getDataLoaderRegistry()
      Returns:
      the DataLoaderRegistry in play
    • getLocale

      Locale getLocale()
      Returns:
      the current Locale instance used for this request
    • getOperationDefinition

      OperationDefinition getOperationDefinition()
      Returns:
      the current operation that is being executed
    • getDocument

      Document getDocument()
      Returns:
      the current query Document that is being executed
    • getVariables

      Map<String,Object> getVariables()
      This returns the variables that have been passed into the query. Note that this is the query variables themselves and not the arguments to the field, which is accessed via getArguments()

      The field arguments are created by interpolating any referenced variables and AST literals and resolving them into the arguments.

      Also note that the raw query variables are "coerced" into a map where the leaf scalar and enum types are called to create input coerced values. So the values you get here are not exactly as passed via ExecutionInput.getVariables() but have been processed.

      Returns:
      the coerced variables that have been passed to the query that is being executed
    • toInternal

      default Object toInternal()
      A method that should only be used by the GraphQL Java library itself. It is not intended for public use.
      Returns:
      an internal representation of the DataFetchingEnvironment