StarpTech / apollo-datasource-http

Optimized JSON HTTP Data Source for Apollo Server
MIT License
73 stars 32 forks source link

Intended usage from resolver functions? #28

Closed mbrowne closed 2 years ago

mbrowne commented 2 years ago

Hi, I read the docs for this repo and also the Apollo docs for data sources, and I'm just wondering if this is the intended usage from a resolver function:

    async getArtwork({ id }) {
        return (await this.get('/artwork/' + id)).body
    }

This is of course for a simple case, where only the body of the response matters, and not response headers, etc. It seems a bit inconvenient to have to access .body on the result for every method, so I'm thinking about creating a wrapper get() method:

    async get(path, requestOptions) {
        const response = await super.get(path, requestOptions)
        if (response.statusCode !== 200) {
            // handle errors
        }
        return response.body
    }

Would that be consistent with how this library is meant to be used, or am I missing something?

StarpTech commented 2 years ago

Hi @mbrowne I'm not sure if I understand your question. This library is an HTTP client. Everything else is up to you. By default this library throws on HTTP errors (>= 200 && <= 399). You can overwrite that behavior by overwriting onResponse. The thrown error is converted to an ApolloError and internal details aren't exposed.

mbrowne commented 2 years ago

Ok, so if I understand correctly, there's no built-in mechanism to await the result and return the body from it, but there is a built-in (default) mechanism to handle errors. I was comparing this library to apollo-datasource-rest, where it's slightly more abstract, making the syntax simpler. So I was just confirming that this library is intended to be a bit lower-level (which obviously also gives you more control).

StarpTech commented 2 years ago

where it's slightly more abstract, making the syntax simpler. So I was just confirming that this library is intended to be a bit lower-level (which obviously also gives you more control).

Yes