Closed tab00 closed 5 years ago
The ApolloServer
constructor now has a dataSources
option. e.g.:
import { DSBooks } from "./DSBooks.js"
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
dsBooks: new DSBooks()
}),
})
Thanks for pointing this out. The simplest thing to do would be to move all data related things from the context to the dataSources, correct? The resolvers should stay the same, because the datasources are accessible via the context again.
I think so, but I'm still a beginner, so you may gain a better understanding as you work on updating your project. I hope to learn a lot from your updated tutorial.
Came across this just now... pretty sure their distinction about DataSources is purely in regards to consuming an existing REST API
Yes. That's what I had in mind when I read about this after I left my comment here. But forgot to reply again. I think for the database usage the context is still used as status quo. Thank you for confirming this @brantchoate
A lot more details confirmed in a thread here as well - https://github.com/apollographql/apollo-server/issues/1260
specifically this comment - https://github.com/apollographql/apollo-server/issues/1260#issuecomment-454021041
This project and tutorial looks great, however it may already be outdated as there is now DataSources (https://www.apollographql.com/docs/apollo-server/features/data-sources.html) in Apollo which fosters better separation of application and DB. This should be the way forward.
A problem is that there isn't much documentation or examples for using DataSources with a DB (only REST source). Here is a simple example developed by one of the Apollo developers: https://github.com/apollographql/fullstack-workshop-server/blob/datasources/src/datasources/LikesDB.js
Here is another simple example app that uses DataSources, React-Apollo components and Meteor: https://github.com/tab00/test-meteor-apollo
Note that neither of these implement batching and caching (which a production-ready app should do).
Could you please update your code and tutorial so that it uses the latest Apollo concepts and syntax?