Apollo-server: TypeError: Cannot read property 'fetch' of undefined

Created on 20 Oct 2019  路  6Comments  路  Source: apollographql/apollo-server

When trying to add a RESTDataSource all requests made within this class fail.

Why is this?

The link belows allows you to test the code i wrote.
CodeSandBox Link
Version: latest

Expected behaviour
Being able to make a get Request to the dummy API
Actual behaviour
Request fails with the following error:
"errors": [ { "message": "Cannot read property 'fetch' of undefined", "locations": [ { "line": 2, "column": 3 } ], "path": [ "hello" ], "extensions": { "code": "INTERNAL_SERVER_ERROR", "exception": { "stacktrace": [ "TypeError: Cannot read property 'fetch' of undefined", " at API.<anonymous> (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:158:63)", " at Generator.next (<anonymous>)", " at /sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:8:71", " at new Promise (<anonymous>)", " at __awaiter (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:4:12)", " at trace (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:153:78)", " at API.<anonymous> (/sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:196:24)", " at Generator.next (<anonymous>)", " at /sandbox/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:8:71", " at new Promise (<anonymous>)" ] } } } ], "data": { "hello": null } }
When logging this in the API class this is the result:
API { httpFetch: undefined, memoizedResults: Map {}, baseURL: 'https://jsonplaceholder.typicode.com/' }

All 6 comments

Hey @experimentalPrivateRepos, thanks for submitting an issue (and especially the reproduction!)

I forked your sandbox and found that removing the initialize override, or restoring it to the same behavior as the RESTDataSource resolved the issue.

Thank you @trevor-scheer, for helping! It works now

@trevor-scheer
Could you please elaborate on details of your answer?

There in NO any initialize, neither method nor property:

const {RESTDataSource} = require('apollo-datasource-rest');

class LaunchAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'https://api.spacexdata.com/v3/';
  }

  async getAllLaunches() {
    const response = await this.get('launches');
    console.log('[getAllLaunches][response]', response);
    return Array.isArray(response) ?
      response.map(launch => this.launchReducer(launch)) : [];
  }

  getAllLaunchesPromise(callback) {
    this.get('launches')
      .then(resp => {
        const data = Array.isArray(resp) ?
          resp.map(launch => this.launchReducer(launch)) : [];
        callback({
          error: null,
          data
        });
      })
      .catch(error => {
        callback({
          error,
          data: null
        })
      });
  }

  launchReducer(launch) {
    return {
      id: launch.flight_number || 0,
      cursor: `${launch.launch_date_unix}`,
      site: launch.launch_site && launch.launch_site.site_name,
      mission: {
        name: launch.mission_name,
        missionPatchSmall: launch.links.mission_patch_small,
        missionPatchLarge: launch.links.mission_patch,
      },
      rocket: {
        id: launch.rocket.rocket_id,
        name: launch.rocket_name,
        type: launch.rocket.rocket_type
      }
    }
  }
}

module.exports = LaunchAPI;

Hi @trevor-scheer . According to the official tutorial mentioned here,
Screen Shot 2020-03-17 at 10 17 27 AM

Also, here's how it is done in the official tutorial.

By your following comment, do you intend that the tutorial is out of date?

Hey @experimentalPrivateRepos, thanks for submitting an issue (and especially the reproduction!)

I forked your sandbox and found that removing the initialize override, or restoring it to the same behavior as the RESTDataSource resolved the issue.

I had the same problem with apollo-datasource-rest combined with graphql-yoga, but I was able to get it work.

You can see my comments about it here

Same here.
If can help :

constructor() {
    super();
    this.baseURL = CHAT_API_HOST;
    this.initialize({} as DataSourceConfig<any>); // <===== this one resolve the issue
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dobesv picture dobesv  路  3Comments

attdona picture attdona  路  3Comments

nevyn-lookback picture nevyn-lookback  路  3Comments

mathroc picture mathroc  路  3Comments

manuelfink picture manuelfink  路  3Comments