From 855a8e9b5145b8dcdf61bd37a68b2aba0bf6e2d2 Mon Sep 17 00:00:00 2001 From: unicodeveloper Date: Thu, 4 Oct 2018 21:11:57 +0100 Subject: [PATCH] Connect data sources to Apollo Server --- docs/source/tutorial/data-source.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/source/tutorial/data-source.md b/docs/source/tutorial/data-source.md index 96aeabf..4864019 100644 --- a/docs/source/tutorial/data-source.md +++ b/docs/source/tutorial/data-source.md @@ -382,4 +382,25 @@ So, if the `userIds` variable is empty, then the `getUsersByLaunch` method retur If no users were found, the `getUsersByLaunch` method returns an empty array, else an array of users with their respective `id`, `email` and `avatar` is returned! -In the next section of this tutorial, we'll write our resolvers! \ No newline at end of file +

Connect Data Sources to Server

+ +Now that we have defined our data sources, they need to be passed as options to the `ApolloServer` constructor so that our resolvers can access them. + +Open `index.js` file, require the `launch` and `user` data source files, and modify the `ApolloServer` constructor to have the `dataSources` key as shown below: + +```js +... +... +const LaunchAPI = require('./datasources/launch'); +const UserAPI = require('./datasources/user'); + +const server = new ApolloServer({ + typeDefs, + dataSources: () => ({ + launchAPI: new LaunchAPI(), + userAPI: new UserAPI(), + }) +}); +``` + +Apollo Server will put the data sources on the context for every request, so you can access them from your resolvers. In the next section of this tutorial, we'll write the resolvers for our app and you'll learn more about `context` and how to access the data sources on them. \ No newline at end of file