Skip to content

Commit 855a8e9

Browse files
unicodeveloperpeggyrayzis
authored andcommitted
Connect data sources to Apollo Server
1 parent 21fab84 commit 855a8e9

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

‎docs/source/tutorial/data-source.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,25 @@ So, if the `userIds` variable is empty, then the `getUsersByLaunch` method retur
382382

383383
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!
384384

385-
In the next section of this tutorial, we'll write our resolvers!
385+
<h2 id="database">Connect Data Sources to Server</h2>
386+
387+
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.
388+
389+
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:
390+
391+
```js
392+
...
393+
...
394+
const LaunchAPI = require('./datasources/launch');
395+
const UserAPI = require('./datasources/user');
396+
397+
const server = new ApolloServer({
398+
typeDefs,
399+
dataSources: () => ({
400+
launchAPI: new LaunchAPI(),
401+
userAPI: new UserAPI(),
402+
})
403+
});
404+
```
405+
406+
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.

0 commit comments

Comments
 (0)