RFC: Extra property on field definition to pass extra metadata
Problem
I'm currently adding extra properties to some graphql object field definitions, like the following: [code block] And then using them later on via the `info` argument inside some middlewares (using `graphql-middleware`): [code block] For more details, see the following medium post: graphql mutation arguments validation using yup --- The thing is, this is relying on internal behavior. The following code spreads all properties given to the field: https://github.com/graphql/graphql-js/blob/81719749e01f030cfb3a01a97e7e4bfc534bb08f/src/type/definition.js#L720-L724 Is that something expected to not change? If yes, then no need for any other extra property or for this issue. ๐ But if this is something that can change in future versions, I would love the possibility of having an extra field for that extra metadata. I'm available to work on adding this, if it's approved.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Add Extra Metadata Property to GraphQL Field Definitions
The current implementation of GraphQL field definitions allows for additional properties to be added, but relies on internal behavior that may change in future versions. This can lead to issues when trying to access these properties in middlewares, as they are not officially supported.
Awaiting Verification
Be the first to verify this fix
- 1
Define a New Metadata Property
Modify the GraphQL field definition to include an official 'metadata' property that can store additional information without relying on internal behavior.
javascriptconst MyType = new GraphQLObjectType({ name: 'MyType', fields: { myField: { type: GraphQLString, metadata: { validate: true, description: 'This field requires validation' } } } }); - 2
Update GraphQL Middleware to Access Metadata
Modify the middleware to access the new 'metadata' property instead of relying on the spread operator. This ensures that the middleware can function correctly regardless of changes in the internal implementation.
javascriptconst myMiddleware = (resolve, parent, args, context, info) => { const metadata = info.fieldNodes[0].selectionSet.selections[0].metadata; if (metadata && metadata.validate) { // Perform validation logic here } return resolve(parent, args, context); }; - 3
Document the New Property
Update the documentation to reflect the addition of the 'metadata' property in field definitions, including examples of how to use it effectively.
- 4
Test Compatibility with Existing Implementations
Run tests to ensure that existing GraphQL schemas and middlewares work as expected with the new 'metadata' property. This includes checking for backward compatibility.
javascriptdescribe('GraphQL Metadata Tests', () => { it('should access metadata correctly', () => { // Test implementation }); }); - 5
Submit a Pull Request for Review
Once the changes are made and tested, submit a pull request to the GraphQL repository for review. Include details about the changes and their purpose.
Validation
Confirm that the new 'metadata' property is accessible in middlewares and that existing functionalities are unaffected. Run unit tests and integration tests to validate the changes.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep