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

Idiomatic logging of requests and responses #216

Closed
0x6e6562 opened this issue Nov 21, 2016 · 5 comments
Closed

Idiomatic logging of requests and responses #216

0x6e6562 opened this issue Nov 21, 2016 · 5 comments

Comments

@0x6e6562
Copy link

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.

@helfer
Copy link
Contributor

helfer commented Nov 29, 2016

@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.

@0x6e6562
Copy link
Author

@helfer Thanks very much for the heads up - I think I'll go with a middleware to log this out.

@dandv
Copy link
Contributor

dandv commented Aug 8, 2018

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;
  },
});

@dandv
Copy link
Contributor

dandv commented May 18, 2019

apollo-server 2.1.1 and later

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.

@leehankyeol
Copy link

To anyone frustrated, you can actually pull the requestContext from formatResponse. #1414 (comment)

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

No branches or pull requests

4 participants