DonJuanEco
DonJuanEco

Reputation: 143

How to return the http status code using "graphql-request"?

I've been playing around with "graphql-request" and I like it because of it's simplicity. Is there any way of returning the http status code from my request also? Currently the following doesn't work (response.status):

const {request} = require('graphql-request');

const query = `{
    Post(id: 1) {
        id
        title
        views
        User {
            name
        }
        Comments {
            date
            body
        }
    }
}`;

     request('http://localhost:3000', query)
        .then(response => console.log(response.status))
        .catch(err => { throw new Error(err); });

The Documentation doesn't cover returning status codes. Hopefully it's possible. Thanks.

Upvotes: 0

Views: 1302

Answers (1)

Matt Morgan
Matt Morgan

Reputation: 5313

You can't, the way it's written now.

Status codes are only returned in the event of a network error. If you look at the source code for this package, you can see that a status code is not returned on Fetch#ok:

However, there's nothing stopping you from forking the project and adding the status code to the data object. You could even make a PR back to the project. Maybe they'll merge it :)

Upvotes: 1

Related Questions