graphql-community / koa-graphql

Create a GraphQL HTTP server with Koa.
MIT License
843 stars 61 forks source link

Read response object after koa-graphql has run #121

Closed mflores-verys closed 2 years ago

mflores-verys commented 5 years ago

I'd like to read the response object that's going to be sent back to the requestor. When I interrogate the response object that's passed when providing a function to koa-graphql, it seems it's before the final response has been composed.

i.e.


function middleware() {
 return graphqlHTTP(
    (request: Object, response: Object, params: Object): Object => {
      // read the response that will be provided to the requestor
      // the response at this point in time is incomplete, it seems. In fact, the status code will be 404.
      return {
        schema,
        graphiql: enableGraphiql || isNotProd,
        formatError: errorFormatterWithMetadata,
        pretty: prettifyJsonResponse,
        extensions: extensionWithMetadata,
        validationRules: additionalValidation,
      }
    },
  )
 }
}
chentsulin commented 2 years ago

From Koa perspective, you could have a middleware doing this job after graphqlHTTP has been run:

const app = new Koa();

app.use(async (ctx, next) => {
  await next();
  // read the response object here!
  console.log(ctx.response.body);
});

app.use(
  mount(
    '/graphql',
    graphqlHTTP({
      schema: MyGraphQLSchema,
      graphiql: true,
    }),
  ),
);