What's the idiomatic way to log gql requests and responses?
I've seen that you can supply a logFunction to the graphql-server-express instance, but this is fired once for each portion of the request with different keys to denote the query, the operation, variables and other data.
Is the idea that the supplier of the callback should accumulate these keys so that it in turn can generate a single log event for the application?
What I'd like to configure is a logger that pretty prints each client request as a single log entry.
@0x6e6562 IIRC the log function gets called a bunch of times with different keys. The one you're interested in is the info.query, info.variables etc. keys, which will have the info you're looking for. An alternative way to do logging is to add a middleware before the server.
@helfer Thanks very much for the heads up - I think I'll go with a middleware to log this out.
This has been working well for me:
const server = new Apollo.ApolloServer({
schema: ...,
formatError: error => {
logger.warn(error);
return error;
},
formatResponse: (response, query) => {
logger.info('GraphQL query and variables', {
query: query.queryString,
vars: query.variables,
});
return response;
},
});
Note that starting with apollo-server 2.2.1 (or possibly earlier), the method I described in my previous comment no longer works, because the query parameter is no longer passed to formatResponse.
I still haven't found clear documentation on how to log queries. Other hacks have been suggested in this StackOverflow question I asked.
To anyone frustrated, you can actually pull the requestContext from formatResponse. https://github.com/apollographql/apollo-server/pull/1414#issuecomment-553445945