Skip to content

Commit 9deb4fb

Browse files
unicodeveloperpeggyrayzis
authored andcommitted
Add more meat to advantages of data sources
1 parent 3cd6af0 commit 9deb4fb

1 file changed

Lines changed: 8 additions & 50 deletions

File tree

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

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ description: Start here for the Apollo fullstack tutorial
55

66
Apollo data sources provide the best experience for fetching and caching data from REST endpoints, web services, and databases. It's a new pattern for loading data from various sources, with built-in support for deduplication, caching, and error handling.
77

8+
When layering GraphQL over your REST endpoints, Apollo data sources enable **partial query caching**-––a policy that provides autom atic caching of HTTP requests based on the caching headers returned from the backend and also allows you override the backend cache policy, by providing the option of setting an explicit `ttl` value in your data source.
9+
10+
In addition, Apollo data sources grants your app a well-structured way of organizing and encapsulating data fetching logic. Rather than bloating your resolver functions with logic, you can wrap access to a particular backend data source inside data source classes.
11+
812
<h2 id="rest-api">Connect a REST API</h2>
913

1014
To get started, install the `apollo-datasource` and `apollo-datasource-rest` packages:
@@ -16,7 +20,6 @@ npm install apollo-datasource apollo-datasource-rest --save
1620
* **apollo-datasource**: This is the generic data source package. It's good for connecting to non-REST data sources.
1721
* **apollo-datasource-rest**: This package exposes the `RESTDataSource` class that is responsible for fetching data from a given REST API. To define a data source for the REST endpoint, extend the `RESTDataSource` class and implement the data fetching methods that your resolvers require.
1822

19-
2023
Create a new `datasources` folder inside the `src` directory. This folder will contain our data source files. Now, create `launch.js` within the `datasources` directory.
2124

2225
The REST API endpoint we'll use for our app is `https://api.spacexdata.com/v2/`. Go ahead and add the endpoint as the base URL as shown in the code below:
@@ -122,15 +125,15 @@ The `getLaunchById` method takes in a flight number and returns the data for a p
122125

123126
<h2 id="database">Connect a database</h2>
124127

125-
A data store is needed for saving and fetching user information. It's also important for user trips. Let's make use of [SQLite](https://www.sqlite.org) for our app's database. SQLite is a self-contained, light-weight, zero-configuration and embedded SQL database engine.
128+
A data store is needed for saving and fetching user information. It's also important for user trips. We'll make use of [SQLite](https://www.sqlite.org) for our app's database. SQLite is a self-contained, light-weight, zero-configuration and embedded SQL database engine.
126129

127130
Before connecting to SQLite, go ahead and install the `sequelize` package from npm:
128131

129132
```bash
130133
npm install sequelize --save
131134
```
132135

133-
**Sequelize** is an ORM for Node.js that supports several relational database management systems such as MySQL, MariaDB, PostgreSQL, SQLite and MSSQL. In this tutorial, we'll make use of it for the SQLite database.
136+
**Sequelize** is an ORM for Node.js that supports several relational database management systems such as MySQL, MariaDB, PostgreSQL, SQLite and MSSQL.
134137

135138
Now, create a `store.sqlite` file in the root directory. Once you have done that, change from the root directory to the `src/datasources` directory:
136139

@@ -282,54 +285,9 @@ In the various methods that we created and copied to the `UserAPI` class, you mu
282285

283286
<h4 id="create-the-store">Create the Store</h4>
284287

285-
Create an `utils.js` file inside the `src` directory. Now, copy the code below and add to it.
286-
287-
_src/utils.js_
288-
289-
```js
290-
const SQL = require('sequelize');
291-
292-
module.exports.createStore = () => {
293-
const Op = SQL.Op;
294-
const operatorsAliases = {
295-
$in: Op.in,
296-
};
297-
298-
const db = new SQL('database', 'username', 'password', {
299-
dialect: 'sqlite',
300-
storage: './store.sqlite',
301-
operatorsAliases,
302-
});
303-
304-
const users = db.define('user', {
305-
id: {
306-
type: SQL.INTEGER,
307-
primaryKey: true,
308-
autoIncrement: true,
309-
},
310-
createdAt: SQL.DATE,
311-
updatedAt: SQL.DATE,
312-
email: SQL.STRING,
313-
token: SQL.STRING,
314-
});
315-
316-
const trips = db.define('trip', {
317-
id: {
318-
type: SQL.INTEGER,
319-
primaryKey: true,
320-
autoIncrement: true,
321-
},
322-
createdAt: SQL.DATE,
323-
updatedAt: SQL.DATE,
324-
launchId: SQL.INTEGER,
325-
userId: SQL.INTEGER,
326-
});
327-
328-
return { users, trips };
329-
};
330-
```
288+
Fork the [GitHub repo--Create A Store](insert-repo-link-here) for the store.
331289

332-
In the code above, the `createStore` function sets up a new SQL instance that connects to the SQLite database. A `database`, `username`, and `password` values are passed as arguments. And an object specifying the `dialect`, location of the SQLite database and operator aliases is also passed as an argument to the SQL instance.
290+
In `src/utils.js` file, the `createStore` function sets up a new SQL instance that connects to the SQLite database. A `database`, `username`, and `password` values are passed as arguments. And an object specifying the `dialect`, location of the SQLite database and operator aliases is also passed as an argument to the SQL instance.
333291

334292
The `users` and `trips` tables have now been defined with their respective fields. And an object containing `users` and `trips`is returned within the `createStore` function to enable us access the ORM methods later on, in the body of our data source.
335293

0 commit comments

Comments
 (0)