Apollo-server: Error solving URL dynamically RestDataSource

Created on 28 Aug 2020  路  4Comments  路  Source: apollographql/apollo-server

Version
APOLLO RESTDATASOUCE = apollo-datasource-rest": "^0.9.3",
APOLLO SERVER = "apollo-server-lambda": "^2.16.1",
TYPESCRIPT = ""typescript": "^4.0.2""

I trying follow the documentation steps to set a dynamic url based on my ENV (setted in context)
But its show warning message 'baseURL' is defined as a property in class 'RESTDataSource<Context>', but is overridden here in 'TransactionsAPI' as an accessor.

Snippet:

export default class TransactionsAPI extends RESTDataSource<Context> {
  constructor() {
    super();
  }

  get baseURL() {
    if (this.context.env === "development") {
      return "https://dev.api_example.com.br";
    } else if (this.context.env === "production") {
      return "https://prod.api_example.com.br";
    }
  }
  async myFunction() {
   return this.post(`/patch`)
  }
}

Im following this steps
http://apollographql.com/docs/apollo-server/data/data-sources/#resolving-urls-dynamically

Thank you!

馃К data-sources

Most helpful comment

Anyone with this problem too?

All 4 comments

Anyone with this problem too?

I am also having same problem. Here is the error message

error TS2611: 'baseURL' is defined as a property in class 'RESTDataSource' but is overridden here as an accessor.

For, i have fixed my typescript version to 3.9.7 and it works with that version. It only happens when I migrate to typescript v4.0 as of some of the breaking changes in it related to Properties Overriding Accessors.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#properties-overriding-accessors-and-vice-versa-is-an-error

Anyone with this problem too?

I'm experiencing this problem.

TypeScript 4 has made overriding a field with a property an error, so how are typescript users supposed to follow the instructions for dynamically resolving the baseURL, which say to do exactly that!?

I want to write something like

class MyApiDataSource extends RESTDataSource {
  constructor() { super(); }
  get baseURL() {
    return this.context.config.myApi.url;
  }
}

But it's an error!

This trick is mentioned in the PR to TS that adds the new error: https://github.com/microsoft/TypeScript/pull/37894

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

abstract class Trick extends RESTDataSource {
  abstract baseURL: string;
}

export default class MyApiDataSource extends Trick {
  constructor() { super(); }
  get baseURL(): string {
    return this.context.config.myApi.url;
  }
}

Hope this helps in the meantime, until the apollo folks figure out how to properly solve this.

same, I hope solve it.

I resolved like below as temporarily, in use resolveURL.

  async resolveURL(request: RequestOptions) {
    if (!this.baseURL) {
      if (this.context.config === 'foo') {
        this.baseURL = 'https://first-of-endpoint.com/';
      }
      this.baseURL = 'https://second-of-endpoint.com/';
    }
    return super.resolveURL(request);
  }

but this one looks like not good. I think better to use get baseURL() essentially.

Was this page helpful?
0 / 5 - 0 ratings