Now that we’ve set up the data source, we can start building the data source itself. In this section, we’ll tell the data source where data should come from.
NOTE: This example accesses data from a REST API, but the data could also come from a database or, really, anywhere that can give a response we can convert to JSON.
The first code change we need to make is to update the Connector to point to the API we want to use. This class sets up the requests themselves, plus helpers to enable caching.
In src/connector.js
, make the following change:
import { GraphQLConnector } from '@gramps/gramps-express';- // TODO: change `YourDataSourceConnector` to a descriptive name- export default class YourDataSourceConnector extends GraphQLConnector {+ export default class IMDBAPIConnector extends GraphQLConnector {/**- * TODO: describe this API endpoint+ * API for looking up movie info* @member {string}*/- apiBaseUri = `https://example.org/v2`;+ apiBaseUri = `https://www.theimdbapi.org/api`;}
NOTE: The
GraphQLConnector
class sets up a lot of core functionality in the background, including methods for sending queries and mutations, plus helpers for caching data. For more information on what’s happening behind the scenes, see the source code.
Next, update the tests for the connector by making the changes below in test/connector.test.js
:
import { GraphQLConnector } from '@gramps/gramps-express';import Connector from '../src/connector';- // TODO: Update the data source name.- const DATA_SOURCE_NAME = 'YourDataSource';+ const DATA_SOURCE_NAME = 'IMDBAPI';const connector = new Connector();describe(`${DATA_SOURCE_NAME}Connector`, () => {it('inherits the GraphQLConnector class', () => {expect(connector).toBeInstanceOf(GraphQLConnector);});it('uses the appropriate URL', () => {- // TODO: Update the data source API endpoint.- expect(connector.apiBaseUri).toBe(`https://example.org/v2`);+ expect(connector.apiBaseUri).toBe(`https://www.theimdbapi.org/api`);});});
Verify that this works by running yarn test
. Everything should pass:
@jlengstorf: ~/dev/gramps-graphql/data-source-imdbapi (master)$ yarn testyarn run v1.2.1$ npm run lint --silent && npm run test:unit --silentPASS test/resolvers.test.jsPASS test/connector.test.jsPASS test/model.test.jsPASS test/index.test.jsTest Suites: 4 passed, 4 totalTests: 16 passed, 16 totalSnapshots: 0 totalTime: 1.839sRan all test suites.--------------|----------|----------|----------|----------|----------------|File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |--------------|----------|----------|----------|----------|----------------|All files | 100 | 100 | 100 | 100 | |connector.js | 100 | 100 | 100 | 100 | |index.js | 100 | 100 | 100 | 100 | |model.js | 100 | 100 | 100 | 100 | |resolvers.js | 100 | 100 | 100 | 100 | |--------------|----------|----------|----------|----------|----------------|✨ Done in 4.83s.
Hey! That wasn’t bad, right? In the next section, we’ll show our data source how to access data by defining data access methods in the model.