Skip to content

How get selectionSet from DataFetchingEnvironment if selection is FragmentSpread #303

Closed

Description

@shendepu

DataFetchingEnvironment.fields has information of selected fields.

But when I run below query, I can't find a way to know the field selectionSet at emplPositionClasses field. Because it is explicit defined fragment, so the instance of selection in emplPositionClasses selectionSet is FragmentSpread. and there is no methods to get selectionSet on FragmentSpread.

For the other two type of selection, Field and InlineFragment, there are methods to get selectionSet, so inline fragment has no issue.

the field emplPositionClasses type is EmplPositionClassConnection

query {
  mantle {
    emplPositionClasses {
    	...EmplPositionClassesFragment
    }
  }
}

fragment EmplPositionClassesFragment on EmplPositionClassConnection {
  edges {
    node {
      id
      emplPositionClassId
      title
      description
    }
  }
}

So is it reasonable to also add getSelectionSet() method on class FragmentSpread?

Activity

  1. dminkovsky commented on Jan 18, 2017

    @dminkovsky
    Contributor
  2. shendepu commented on Jan 18, 2017

    @shendepu
    Author
  3. dminkovsky commented on Jan 18, 2017

    @dminkovsky
    Contributor

    Field and InlineFragment has it.

    Yes, they have them because these classes represent field and inline fragment nodes in the GraphQL query language. In the query language, those types of nodes can have a selection set. But the FragmentSpread, which models a fragment spread as it occurs in a written GraphQL query, by definition does not have a selection set.

    See the Facebook reference implementation/modeling: FieldNode vs InlineFragmentNode vs FragmentSpreadNode. FieldNode and InlineFragmentNode have selection sets. FragmentSpreadNode does not.

    But yes, of course, it is convenient to have those fragment definitions and their selection sets during execution. The fragment definitions are available in the ExecutionContext, which should be exposed in the DataFetchingEnvironment.

    Related perhaps #301 #260

  4. shendepu commented on Jan 19, 2017

    @shendepu
    Author

    I only find latest java with graphql-java-2017-01-15T22-16-50.jar. It seems #301 not in there yet. So I will need to wait the latest build to give a try.

  5. misterzirillo commented on Jan 21, 2017

    @misterzirillo
    Contributor

    From stepping through the code I can see that the actual ExecutionContext is never passed to DataFetchingEnvironment (or the new DataFetchingEnvironmentImpl) as the context parameter (line 31 of ExecutionStrategy). Instead executionContext.getRoot() is passed as that param. Is this intentional? Changing it to pass the ExecutionContext would give us access to fragment definitions.

  6. dminkovsky commented on Jan 21, 2017

    @dminkovsky
    Contributor

    Hi @mrcirillo, thanks for investigating. Yes, this is intentional. executionContext.getRoot() in this case returns what is the "context" inside the DataFetcher, aka the root of the whole GraphQL execution, aka what you passed to GraphQL#execute() as the source.

    Indeed, we can pass the whole ExecutionContext into the DataFetcher via the DataFetchingEnvironment to get the fragment definitions. I think we should make this change.

  7. misterzirillo commented on Jan 21, 2017

    @misterzirillo
    Contributor

    I've already done it :) I'll make a PR soon.

  8. dminkovsky commented on Jan 21, 2017

    @dminkovsky
    Contributor

    Oh cool. Thanks. Something like https://github.com/graphql/graphql-js/blob/v0.8.2/src/type/definition.js#L560? (aka the DataFetchingEnvironment)

  9. dminkovsky commented on Jan 21, 2017

    @dminkovsky
    Contributor

    Also please see d1af3ac the most recent commit on master. Thank you!

  10. added a commit that references this issue on Apr 27, 2017
    e64f128
  11. added a commit that references this issue on May 29, 2017
    e3f8487
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions