graphql-spec icon indicating copy to clipboard operation
graphql-spec copied to clipboard

introduce ExecuteGroupedFieldSet, CollectRootFields and CollectSubfields

Open yaacovCR opened this issue 3 years ago • 8 comments

Rather than merging subSelectionSets of a field set using MergeSelectionSets and then calling CollectFields, this PR introduces CollectSubfields allows the field set's groupedSubfieldSet to be calculated directly.

The reference implementation already uses this algorithm so that this change actually aligns the specification to the reference implementation, and is ipso facto non-breaking.

Motivation: reformulating the specification in this manner may be helpful if the specification were ever to be altered such that additional state beyond the current selection set were to be required to calculate the response, i.e. if it were to be required to know the originating selectionSet of a given field within the fieldSet for determining when to communicate a reference signal. In such a scenario, it may still be quite possible to merge the set of requested data from a field set's subSelectionSets, but it may not be possible to express that merged data as an equivalent selectionSet.

In particular, currently:

{
  a {
    subfield1
  }
  ...ExampleFragment
}

fragment ExampleFragment on Query {
  a {
    subfield2
  }
  b
}

For the given set of fields:

a {
  subfield1
}
a {
  subfield2
}

These can currently be trivially merged as:

a {
  subfield1
  subfield2
}

However, the requested information for a in:

{
  a {
    subfield1
  }
  Ref1 { completed } : ...ExampleFragment
}

fragment ExampleFragment on Query {
  a {
    subfield2
  }
  b
}

cannot be contained in a merged selection set under A, because some of those fields will be related to Ref1 and some will not. The requsted information can still be merged, but it cannot be expressed in selection set format.

yaacovCR avatar Nov 06 '22 15:11 yaacovCR

Deploy Preview for graphql-spec-draft ready!

Name Link
Latest commit 6f1ad74b13ef821fbb610aef3957595138adfbb2
Latest deploy log https://app.netlify.com/sites/graphql-spec-draft/deploys/644aa92284b3ba00082c0f30
Deploy Preview https://deploy-preview-999--graphql-spec-draft.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

netlify[bot] avatar Nov 06 '22 15:11 netlify[bot]

review suggestions merged, but now depends on #1008 to fix formatting

yaacovCR avatar Jan 13 '23 08:01 yaacovCR

Additional use case for this PR is for https://github.com/graphql/graphql-js/pull/3820

...where we merge groupedFieldSets of TaggedFieldNodes, so we can't just use CollectFields(...MergeSelectionSets(...selectionSets)...) we need an independent CollectSubFields that operates of a taggedFieldNode group...

yaacovCR avatar Jan 13 '23 08:01 yaacovCR

In particular, this is used as the base for https://github.com/robrichard/graphql-spec/pull/10

yaacovCR avatar Jan 15 '23 14:01 yaacovCR

also used as the base for #1026

yaacovCR avatar May 23 '23 16:05 yaacovCR

I needed something similar for my incremental delivery changes; thought you might be interested to see how I solved this problem:

https://github.com/graphql/graphql-spec/compare/benjie/incremental-common?w=1

benjie avatar Jul 08 '23 09:07 benjie

I needed something similar for my incremental delivery changes; thought you might be interested to see how I solved this problem:

https://github.com/graphql/graphql-spec/compare/benjie/incremental-common?w=1

That looks very clean, if you want to submit it as an alternative to this PR, I would definitely +1 it :)

yaacovCR avatar Jul 09 '23 11:07 yaacovCR

@yaacovCR Thanks; I have now done so: https://github.com/graphql/graphql-spec/pull/1039

benjie avatar Aug 21 '23 11:08 benjie

Closing in favor of #1039

yaacovCR avatar Sep 18 '24 19:09 yaacovCR