question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RESTDataSource with TypeScript

See original GitHub issue

I’m using apollo-server-hapi and I’m defining my server like this:

  interface MyDataSources {
    injuryAPI: InjuryDataSource,
    appointmentAPI: AppointmentDataSource,
  }

  const server = new ApolloServer({
    schema,
    dataSources: (): DataSources<MyDataSources> => ({
      injuryAPI: new InjuryDataSource(),
      appointmentAPI: new AppointmentDataSource(),
    }),
    context: ({ request }): GraphQLCustomContext => ({
      credentials: { token: request.headers['Authorization'] || '' },
    }),
  });

This works fine, but then I lose type safety on the resolvers - dataSources is of type any.

If I create my server like this I get the type safety on the resolvers, but I receive the error fetch is undefined:

const server = new ApolloServer({
    schema,
    context: ({ request }): GraphQLCustomResolversContext => ({
      dataSources: {
        injuryAPI: new InjuryDataSource(),
        appointmentAPI: new AppointmentDataSource(),
      },
      credentials: { token: request.headers['Authorization'] || '' },
    }),
  });

Does anyone have a solution for using the RESTDataSource with TypeScript and getting type safety?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
mikefisher84commented, Nov 22, 2019

How did you guys solve this? Would like to know!

3reactions
EthanSKcommented, Apr 16, 2020

A config option in the codegen.yml file for specifying the datasource type would be very helpful. There is currently one for context type: contextType: ./context#MyContext but not for datasource type!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Apollo Server with Typescript - Learn it my way
In this article, I will demonstrate a way to test GraphQL endpoints of an Apollo Server with a RESTDataSource in Typescript.
Read more >
Data sources - Apollo GraphQL Docs
Data sources are classes that Apollo Server can use to encapsulate fetching data from a particular source, such as a database or a...
Read more >
Apollo Server - 7. GraphQL and TypeScript - YouTube
GraphQL # TypeScript #ApolloServerIn this video, we use the Apollo DataSource package to give our project a little more structure.
Read more >
How do you mock an Apollo Server RESTDataSource for unit ...
You can unit test your data source by mocking the RESTDataSource in apollo-datasource-rest as suggested in apollo-datasource-rest + Typescript + ...
Read more >
apollo-datasource-rest - npm
Start using apollo-datasource-rest in your project by running `npm i ... TypeScript icon, indicating that this package has built-in type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found