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

Introducing new transform to override schema field config #2772

Merged
merged 5 commits into from
Oct 5, 2021

Conversation

santino
Copy link
Contributor

@santino santino commented Sep 7, 2021

Description

GraphQL Mesh, and underlying GraphQL Tools, make it easy and convenient to extend schemas, merge schemas and types, add middlewares to resolvers (composers), and much more.

However, it seems there is not really a way to override parts of a schema, such as overriding types for fields.
This transform introduces this functionality.
The transform was initially meant to hoist fields, but during development, it became much more than that.

You can transform a field by selecting the base type and field name (current) and the target type and field name (to be).
This is very simple when you want to hoist fields.
However, the transform also allows you to pass additional typeDefs so that you can create new GraphQL Types to be used as a target (to be).
The transform provides built-in functionality to hoist field values or replace the entire fieldConfig (including the resolver function) with the targeted one.
However, it also allows adding a composer to each rule so that custom logic can be built on each resolver function.
The composer is fundamental for instance when hoisting values for fields that have been renamed, especially on a bare schema, because the transform cannot be aware of the shape of the raw resolver result; so manual adjustments are necessary.
Obviously, the composer can be used to completely ignore the original resolver for a field and execute a new resolver function instead; which might be necessary when overriding fields in general (so beyond the hoisting scope).

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

I have added unit tests for the new transform.
These make a good example of how the transform can be used.

I plan to add more unit tests to cover scenarios where multiple config options are combined together.

Checklist:

  • I have followed the CONTRIBUTING doc and the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests and linter rules pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Further comments

This PR is still a draft as it requires an agreement to be reached on the proposed transform.
Current transform only implements bare mode, we should define if wrap is required and estimate effort to get it working.

I believe this transform has huge potential.
Moving forward we can extend functionalities to override input types and arguments for instance.

@changeset-bot
Copy link

changeset-bot bot commented Sep 7, 2021

🦋 Changeset detected

Latest commit: b7f244a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 74 packages
Name Type
@graphql-mesh/replace-field Minor
@graphql-mesh/types Minor
@graphql-mesh/website Minor
@graphql-mesh/config Patch
@graphql-mesh/runtime Patch
@graphql-mesh/utils Patch
@graphql-mesh/cache-file Patch
@graphql-mesh/cache-inmemory-lru Patch
@graphql-mesh/cache-localforage Patch
@graphql-mesh/cache-redis Patch
@graphql-mesh/cache-store Patch
@graphql-mesh/graphql Patch
@graphql-mesh/grpc Patch
@graphql-mesh/json-schema Patch
@graphql-mesh/mongoose Patch
@graphql-mesh/mysql Patch
@graphql-mesh/neo4j Patch
@graphql-mesh/odata Patch
@graphql-mesh/openapi Patch
@graphql-mesh/postgraphile Patch
@graphql-mesh/soap Patch
@graphql-mesh/thrift Patch
@graphql-mesh/tuql Patch
@graphql-mesh/transform-cache Patch
@graphql-mesh/transform-encapsulate Patch
@graphql-mesh/transform-extend Patch
@graphql-mesh/transform-federation Patch
@graphql-mesh/transform-filter-schema Patch
@graphql-mesh/transform-mock Patch
@graphql-mesh/transform-naming-convention Patch
@graphql-mesh/transform-prefix Patch
@graphql-mesh/transform-rename Patch
@graphql-mesh/transform-resolvers-composition Patch
@graphql-mesh/transform-snapshot Patch
@graphql-mesh/transform-type-merging Patch
@graphql-mesh/merger-bare Patch
@graphql-mesh/merger-federation Patch
@graphql-mesh/merger-stitching Patch
@graphql-mesh/cli Patch
@graphql-mesh/container Patch
odata-msgraph-programmatic-ts Patch
odata-msgraph-programmatic Patch
postgres-geodb-example Patch
typescript-location-weather-example Patch
openapi-react-weatherbit Patch
json-machete Patch
@graphql-mesh/store Patch
neo4j-example Patch
federation-handler-example Patch
graphql-file-upload-example Patch
covid-mesh Patch
type-merging-batching-example Patch
grpc-example Patch
grpc-reflection-example Patch
hasura-openbrewery-geodb Patch
json-schema-hello-world Patch
json-schema-example Patch
json-schema-fhir Patch
json-schema-subscriptions Patch
mongoose-example Patch
mysql-employees Patch
mysql-rfam Patch
odata-microsoft-graph-example Patch
odata-trippin-example Patch
javascript-wiki Patch
openapi-stackexchange Patch
openapi-stripe Patch
openapi-subscriptions Patch
openapi-youtrack Patch
country-info-example Patch
soap-demo Patch
soap-netsuite Patch
thrift-calculator Patch
chinook Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@santino santino force-pushed the transform-replace-field branch from 2578390 to e59ef5d Compare September 7, 2021 20:41
@santino santino force-pushed the transform-replace-field branch from 3f2ac37 to 12834b0 Compare October 5, 2021 09:42
@ardatan
Copy link
Owner

ardatan commented Oct 5, 2021

@santino I think we can merge this :) You can add a changeset so we can release

@santino
Copy link
Contributor Author

santino commented Oct 5, 2021

Hey @ardatan,
I have added changeset and introduced documentation for this new transform.

Currently, this supports only bare mode.
We should work on potentially introducing wrap mode, but I figured out it requires a lot of custom code, and probably would be better to introduce a transform in graphql-tools (or in mesh, but still graphql-tools like) to cover this functionality.

In the meantime, this can be merged and released.

@santino santino force-pushed the transform-replace-field branch from 043362a to b7f244a Compare October 5, 2021 22:01
@ardatan ardatan merged commit 8c9b709 into ardatan:master Oct 5, 2021
@ardatan
Copy link
Owner

ardatan commented Oct 5, 2021

Thanks @santino !

@santino santino deleted the transform-replace-field branch October 6, 2021 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants