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

How to add metadata into top level extensions property in GQL response? #2220

Open
achintha-weerasinghe opened this issue Oct 24, 2024 · 1 comment

Comments

@achintha-weerasinghe
Copy link

Documentation lacks details of adding data into the top level extensions property in the GQL response. I'm looking for an example where some data is copied from the context to the extensions just before sending the response to the client. Your help is appreciated. Thanks!

@benjie
Copy link
Member

benjie commented Nov 1, 2024

Indeed; this is not documented - help adding it to the documentation would be appreciated!

Fortunately it is possible to do it with a plugin like this:

const AddToResponseExtensionsPropertyPlugin: GraphileConfig.Plugin = {
  name: "AddToResponseExtensionsPropertyPlugin",

  grafast: {
    middleware: {
      execute(next, event) {
        return next.callback((error, result) => {
          if (error) throw error;
          const context = (event.args.contextValue ?? {}) as Grafast.Context;
          if (!isAsyncIterable(result)) {
            if (!result.extensions) result.extensions = {};
            result.extensions.number = context.number;
          }
          return result;
        });
      },
    },
  },
};

I don't think this middleware system has any documentation yet actually.

@benjie benjie moved this from 🌳 Triage to 📝 Docs Improvements in V5.0.0 Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📝 Docs Improvements
Development

No branches or pull requests

3 participants
@benjie @achintha-weerasinghe and others