Apollo-server: Unable to log request data (ip address)

Created on 9 Aug 2018  Â·  5Comments  Â·  Source: apollographql/apollo-server

I have a custom logging extension function:

class LogExtension {
  constructor(logger) {
    this.logger = logger;
  }

  requestDidStart({
    request,
    queryString,
    parsedQuery,
    operationName,
    variables,
  }) {
    const requestId = request.headers['X-Request-Id'] || '-';
    const ipAddress = ???;

    this.logger.info(
      {
        requestId,
        operationName,
        ipAddress,
      },
      'IP Address'
    );

    return errors => {
      if (errors && errors.length) {
        this.logger.error({
          requestId,
          operationName,
          key: 'errors',
          data: errors,
        });
      }
    };
  }
}

export default LogExtension;

Expected

To have access to either the node Request data or Koa ctx in relevant extension hooks.

Actual

The request is missing all headers and it doesn't have the normal node Request object values rendering it of little to no value.

Is there a way to access the actual Koa request ctx or otherwise get real request data?

Most helpful comment

@jbaxleyiii: while those support avenues may be more appropriate, they are also far harder to search. Almost two years later, a Google search for "get client IP apollo-server" produces this issue, and a StackOverflow question about getting the IP from a subscription (which doesn't answer this question).

All in all, at least 3 people on this issue (though likely 300+ total) have been wasting their time reading this issue and trying to figure out something as basic as getting the IP of the client. Does this have to be that complicated and bureaucratic to not be simply answered in this issue, not even with the relevant link to StackOverflow?

All 5 comments

Same issue here. Did you ever figure it out?

👋 I'll close this since this doesn't appear to be a bug with Apollo Server, but rather a question about how to use it or one of its components.

Rather than asking it here in GitHub Issues — where efforts are focused on fixing bugs and adding new features — I'd ask that you take this question to the _Apollo Server_ channel within the Apollo community on Spectrum.chat where there are community members who might be able to relate to a similar problem, or might be able to help you out more interactively. Thanks for your understanding!

@jbaxleyiii: while those support avenues may be more appropriate, they are also far harder to search. Almost two years later, a Google search for "get client IP apollo-server" produces this issue, and a StackOverflow question about getting the IP from a subscription (which doesn't answer this question).

All in all, at least 3 people on this issue (though likely 300+ total) have been wasting their time reading this issue and trying to figure out something as basic as getting the IP of the client. Does this have to be that complicated and bureaucratic to not be simply answered in this issue, not even with the relevant link to StackOverflow?

Anybody have any luck with this?

@pbandjs: I posted a link to the correct solution in my comment above.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hiucimon picture hiucimon  Â·  3Comments

mathroc picture mathroc  Â·  3Comments

jpcbarros picture jpcbarros  Â·  3Comments

attdona picture attdona  Â·  3Comments

bryanerayner picture bryanerayner  Â·  3Comments