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

Add transforms to wrap, extract, and rename fields #1183

Closed
yaacovCR opened this issue Aug 13, 2019 · 0 comments
Closed

Add transforms to wrap, extract, and rename fields #1183

yaacovCR opened this issue Aug 13, 2019 · 0 comments
Labels

Comments

@yaacovCR
Copy link
Collaborator

yaacovCR commented Aug 13, 2019

Original post edited to reflect changes in graphql-tools-fork v6.3.2 that support error mapping.

This is now live in graphql-tools-fork v6.3.2.

Examples are within the tests.

For instance, the following extracts fields from an enclosing type in the original schema:

    transformedPropertySchema = transformSchema(propertySchema, [
      new ExtendSchema({
        typeDefs: `
          extend type Property {
            locationName: String
          }
        `,
        resolvers: {
          Property: {
            locationName: createMergedResolver({ fromPath: ['location', 'name'] }),
          },
        },
        fieldNodeTransformerMap: {
          'Property': {
            'locationName':
              fieldNode => wrapFieldNode(renameFieldNode(fieldNode, 'name'), ['location']),
          },
        },
      }),
    ]);

The following wraps fields in multiple new layers:

    transformedPropertySchema = transformSchema(propertySchema, [
      new ExtendSchema({
        typeDefs: `
          extend type Property {
            outerWrap: OuterWrap
          }
          type OuterWrap {
            innerWrap: InnerWrap
          }
          type InnerWrap {
            id: ID
            name: String
          }
        `,
        resolvers: {
          Property: {
            outerWrap: (parent, args, context, info) => ({
              innerWrap: {
                id: createMergedResolver({ toPath: ['innerWrap', 'id'] })(parent, args, context, info),
                name: createMergedResolver({ toPath: ['innerWrap', 'name'] })(parent, args, context, info),
              },
            }),
          },
        },
        fieldNodeTransformerMap: {
          'Property': {
            'outerWrap': (fieldNode, fragments) => extractFields({ fieldNode, path: ['innerWrap'], fragments }),
          },
        },
      }),

Feedback is welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants