webiny / webiny-js

Open-source serverless enterprise CMS. Includes a headless CMS, page builder, form builder, and file manager. Easy to customize and expand. Deploys to AWS.
https://www.webiny.com
Other
7.29k stars 601 forks source link

feat(handler-graphql): add support for resolver decoration #4199

Open Pavel910 opened 1 month ago

Pavel910 commented 1 month ago

Changes

This PR adds support for GraphQL resolver decoration., and improves GraphQL related types (mostly typeDefs and resolvers).

// Decorator #1
const lowerCaseName = createResolverDecorator<any, any, Context>(
  (resolver) => async (parent, args, context, info) => {
    // Run the original resolver
    const name = await resolver(parent, args, context, info);

    // Return a modified value.
    return name.toLowerCase();
  }
);

// Decorator #2
const listBooks = createResolverDecorator(() => async () => {
  return [{ name: "Article 1" }];
});

// Target fields to decorate
const decorator1 = createGraphQLSchemaPlugin({
  resolverDecorators: {
    "Query.books": [listBooks],
    "Book.name": [lowerCaseName],
  },
});

How Has This Been Tested?

Jest tests.