Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce result visiting #1718

Merged
merged 4 commits into from Jul 5, 2020
Merged

Introduce result visiting #1718

merged 4 commits into from Jul 5, 2020

Conversation

yaacovCR
Copy link
Collaborator

@yaacovCR yaacovCR commented Jul 2, 2020

When transforming a delegating schema (i.e. utilizing the transformSchema method within transforms passed to wrapSchema), one technique for result transformation is to defer the work until field resolution time, i.e. by wrapping the resolve method for the appropriate field within the delegating schema.

This approach saves a round of result tree traversal., but, unfortunately, causes several issues:

= return type inconsistencies that affect using these fields for stitching (see: #1725, yaacovCR#35)
= type merging issues when fields are wrapped only in some subschemas

WrapType, WrapFields, HoistField, ExtendSchema transformers have been converted to utilize their transformResult methods to visit the result.

This PR embraces that result visiting as an independent step when required and introduces some generic utility functions to make that easier.

To do, in separate future PRs:
= use result visiting to implement leaf value conversion #1634
= use result visiting to convert errors from subschemas to full paths, a la #1650 to solve #1047. This set of changes is now a breaking change because getErrors is exported, and likely should be deferred until the next major release, which probably (?) should coincide with the next major release of upstream graphql-js.

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 2, 2020

The latest changes of this PR are available as alpha in npm: 6.0.13-alpha-6dc5e024.0

Quickly update your package.json by running:

npx match-version @graphql-tools 6.0.13-alpha-6dc5e024.0

@yaacovCR yaacovCR force-pushed the visit-result branch 6 times, most recently from d918dda to 4f1ea0f Compare July 3, 2020 20:07
@yaacovCR yaacovCR requested a review from ardatan July 3, 2020 20:12
@yaacovCR yaacovCR changed the title Add result visiting Introduce result visiting Jul 3, 2020
@yaacovCR yaacovCR force-pushed the visit-result branch 2 times, most recently from e02fa9f to 803f032 Compare July 5, 2020 02:46
This changes introduces a few generic functions for revisiting data

= visitData can be used to recursively visit an ExecutionResult (or any object)

it takes two functions, one executed when entering the object, one when leaving

= visitResult can be used to visit a result by with a resultVisitorMap and/or an errorVisitorMap

visitResult visits the result using the request, so it knows the object type for every object within the map, as long as the result includes __typename info when requesting abstract types, and also knows the field name for each aliased key within the object.

* it executes the correct visitor from each objectVisitorMap included within the resultVisitorMap depending on the object type and field name
* it executes object visitors on the object itself when entering and leaving the object using the values from the __enter and __leave dummy field names within the objectVisitorMap
* it executes any visitors for leaf types included within the resultVisitorMap to provide a simple mechanism of visiting all fields of a certain leaf type

errors can also be visited -- these are meant to provide opportunities for transforming a GraphQLError, including the path, and so, if an errorVisitorMap is included, error visitors from the map will be collected for each field found in the path of the original error.
Adds result wrapping capability to the following generic transformers

= TransformCompositeFields
= TransformInterfaceFields
= TransformObjectFields
= TransformRootFields
= ExtendSchema
= MapFields

Adds result visiting usage to the following transfromers
= WrapFields
= WrapType
= HoistField

Result visiting was introduced within RenameTypes within the initial
commit.
@yaacovCR yaacovCR merged commit 6dc5e02 into master Jul 5, 2020
@yaacovCR yaacovCR deleted the visit-result branch July 5, 2020 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants