You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9deb4fb
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/source/tutorial/data-source.md
+8-50Lines changed: 8 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,10 @@ description: Start here for the Apollo fullstack tutorial
5
5
6
6
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.
7
7
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
+
8
12
<h2id="rest-api">Connect a REST API</h2>
9
13
10
14
To get started, install the `apollo-datasource` and `apollo-datasource-rest` packages:
***apollo-datasource**: This is the generic data source package. It's good for connecting to non-REST data sources.
17
21
***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.
18
22
19
-
20
23
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.
21
24
22
25
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
122
125
123
126
<h2id="database">Connect a database</h2>
124
127
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.
126
129
127
130
Before connecting to SQLite, go ahead and install the `sequelize` package from npm:
128
131
129
132
```bash
130
133
npm install sequelize --save
131
134
```
132
135
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.
134
137
135
138
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:
136
139
@@ -282,54 +285,9 @@ In the various methods that we created and copied to the `UserAPI` class, you mu
282
285
283
286
<h4id="create-the-store">Create the Store</h4>
284
287
285
-
Create an `utils.js` file inside the `src` directory. Now, copy the code below and add to it.
Fork the [GitHub repo--Create A Store](insert-repo-link-here) for the store.
331
289
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.
333
291
334
292
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.
0 commit comments