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

Unable to log request data (ip address) #1506

Closed
pho3nixf1re opened this issue Aug 9, 2018 · 7 comments
Closed

Unable to log request data (ip address) #1506

pho3nixf1re opened this issue Aug 9, 2018 · 7 comments

Comments

@pho3nixf1re
Copy link

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?

@TidyIQ
Copy link

TidyIQ commented Jun 4, 2019

Same issue here. Did you ever figure it out?

@jbaxleyiii jbaxleyiii added the 🚧👷‍♀️👷‍♂️🚧 in triage Issue currently being triaged label Jul 8, 2019
@jbaxleyiii
Copy link
Contributor

👋 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!

@abernix abernix removed 🚧👷‍♀️👷‍♂️🚧 in triage Issue currently being triaged labels Jul 9, 2019
@dandv
Copy link
Contributor

dandv commented May 13, 2020

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

@pbandjs
Copy link

pbandjs commented Jun 23, 2020

Anybody have any luck with this?

@dandv
Copy link
Contributor

dandv commented Jun 27, 2020

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

@ghostoy
Copy link

ghostoy commented Jul 5, 2022

As I walked through the code, it's impossible to get the client ip on plugin side. The only arguement passed to plugin side per request is GraphQLRequestContext<BaseContext>. The raw http request was filtered out by convertNodeHttpToRequest, which left only headers, method and url, before passed as http of context.

@jbaxleyiii Please consider to add the raw http request as part of graphql context, so that plugins could obtain the original client ip from http.

@glasser
Copy link
Member

glasser commented Jul 8, 2022

You have access to the raw request in your own context function.

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

8 participants