apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

The first argument to this.get and other verbs must be non-null.

Open yossisp opened this issue 5 years ago • 1 comments

resolveURL function in apollo-datasource-rest/src/RESTDataSource.ts (https://github.com/apollographql/apollo-server/blob/39e678c58f984a52c8d5299449c333bda9f1e0be/packages/apollo-datasource-rest/src/RESTDataSource.ts#L76) calls path.startsWith('/') therefore path must be non-null (as can also be seen in Typescript definition of RequestOptions). However, there're use cases where only root url needs to be accessed in the API so this.get() can be called without any additional pathname or query params. This should be mentioned in the docs.

yossisp avatar Jul 01 '20 08:07 yossisp

this is the use case I recently ran into:

import { RESTDataSource } from 'apollo-datasource-rest'

class MyAPI extends RESTDataSource {
  constructor() {
    super()
    this.baseURL = 'https://myapi.com'
    this.rootUrl = '/'
  }

    async getPromotion() {
        return this.get(this.rootUrl)
    }
}

In order to make the MyAPI work I had to use this.rootUrl because the first argument to this.get must be non-null. Of course, I'd rather call return this.get(). But in the absence of such option at least it should be mentioned in the docs that the first argument must be non-null.

yossisp avatar Jul 01 '20 08:07 yossisp

Hey @yossisp, sorry for the delay. The REST datasource package has migrated to https://github.com/apollographql/datasource-rest. If this change is still interesting to you I'd be happy to review a PR over there.

trevor-scheer avatar Oct 11 '22 22:10 trevor-scheer