RESTDataSource with TypeScript
See original GitHub issueI’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:
- Created 4 years ago
- Comments:11 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
How did you guys solve this? Would like to know!
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!!