Seeking assistance here! I recently made the switch from using rest
to gql
, but encountered an issue along the way. In my previous setup with rest
, I was able to retrieve the user's IP address using req.ip
. However, when trying to implement this in the resolve()
function of GraphQL, the value returned was undefined
. After some research, I came across the following solution:
const server = new GraphQLServer({
context: context => ({
...context,
db,
userIp: maybeGetUserIpAddress(context.request),
}),
});
const maybeGetuserIpAddress = (request): ?string => {
const headers = request.headers;
if (!headers) return null;
const ipAddress = headers['x-forwarded-for'];
if (!ipAddress) return null;
return ipAddress;
};
Unfortunately, this also resulted in returning undefined
.
Therefore, I have a couple of questions:
1. Could this issue be related to hosting my server locally?
2. What might be incorrect in the provided code snippet?