FG
๐Ÿ’ป Software๐Ÿ”Œ APIs & SDKs

RFC: Extra property on field definition to pass extra metadata

Fresh3 days ago
Mar 14, 20260 views
Confidence Score75%
75%

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

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Add Extra Metadata Property to GraphQL Field Definitions

Medium Risk

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. 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.

    javascript
    const MyType = new GraphQLObjectType({
      name: 'MyType',
      fields: {
        myField: {
          type: GraphQLString,
          metadata: { validate: true, description: 'This field requires validation' }
        }
      }
    });
  2. 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.

    javascript
    const 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. 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. 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.

    javascript
    describe('GraphQL Metadata Tests', () => {
      it('should access metadata correctly', () => {
        // Test implementation
      });
    });
  5. 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

AC

Alex Chen

2450 rep

Tags

graphqlapischemaenhancement