Skip to content

Commit d05bbb2

Browse files
committed
feat(base): data source template 💥
- Adds templates for npm and DevOps - TODOs for all items that need to be edited - Simplest possible example data source to limit how much boilerplate editing is required - Starts with test coverage at 💯 - Provides testing helpers for common resolver testing patterns - Comes with docs! https://ibm.biz/graphql-data-source closes bluemix/graphql#125
1 parent d63d5cf commit d05bbb2

15 files changed

+175
-1029
lines changed

.travis.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.

package.json renamed to package.TEMPLATE.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
2-
"name": "@console/graphql-data-source-base",
3-
"version": "0.0.0-semantic-release",
2+
"name": "@console/graphql-data-source-[DATA_SOURCE_NAME]",
43
"description": "This is a data source for the Bluemix GraphQL µ-service.",
4+
"author": "Your Name <[email protected]>",
5+
"repository": {
6+
"type": "git",
7+
"url": "[email protected]:bluemix/[REPO_NAME].git"
8+
},
9+
"version": "0.0.0-semantic-release",
510
"main": "src/index.js",
611
"directories": {
712
"test": "test"
@@ -21,14 +26,9 @@
2126
"test:unit": "NODE_ENV=test LOG4JS_LEVEL='OFF' jest --coverage",
2227
"test": "npm run lint --silent && npm run test:unit --silent"
2328
},
24-
"repository": {
25-
"type": "git",
26-
"url": "[email protected]:bluemix/graphql-data-source-base.git"
27-
},
2829
"keywords": [
2930
"graphql"
3031
],
31-
"author": "Jason Lengstorf <[email protected]> (https://lengstorf.com)",
3232
"license": "ISC",
3333
"dependencies": {
3434
"@console/graphql-data-source-helpers": "^1.0.2",

sonar-project.properties

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/connector.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { GraphQLConnector } from '@console/graphql-data-source-helpers';
22

3-
export default class CloudFoundryAppConnector extends GraphQLConnector {
3+
// TODO: change `YourDataSourceConnector` to a descriptive name
4+
export default class YourDataSourceConnector extends GraphQLConnector {
45
/**
5-
* Env-specific API endpoint for Cloud Foundry.
6+
* TODO: describe this API endpoint
67
* @member {string}
78
*/
8-
apiBaseUri = `https://api.${process.env.cfDomain}/v2`;
9+
apiBaseUri = `https://example.org/v2`;
910
}

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import resolvers from './resolvers';
33
import Connector from './connector';
44
import Model from './model';
55

6+
/*
7+
* For more information on the main data source object, see
8+
* https://ibm.biz/graphql-data-source-main
9+
*/
610
export default {
7-
// This is used to reference the model (e.g. context.YourModel.getAll())
8-
context: 'CloudFoundryApp',
11+
// TODO: Rename the context to describe the data source.
12+
context: 'YourDataSource',
913
model: new Model({ connector: new Connector() }),
1014
schema,
1115
resolvers,

src/model.js

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,44 @@ import {
33
BluemixGraphQLError,
44
} from '@console/graphql-data-source-helpers';
55

6-
const formatEnvVars = unformattedVars => {
7-
const envVars = {};
6+
/*
7+
* For more information on data source models, see
8+
* https://ibm.biz/graphql-data-source-model
9+
*/
810

9-
unformattedVars.forEach(keyValuePair => {
10-
envVars[keyValuePair.key] = keyValuePair.value;
11-
});
12-
13-
return envVars;
14-
};
15-
16-
export default class CloudFoundryAppModel extends GraphQLModel {
11+
// TODO: change `YourDataSourceModel` to a descriptive name
12+
export default class YourDataSourceModel extends GraphQLModel {
1713
/**
18-
* Loads GitHub data for a given user
19-
* @param {String} username the username to load
20-
* @return {Promise} resolves with the loaded user data
14+
* Loads a thing by its ID
15+
* @param {String} id the ID of the thing to load
16+
* @return {Promise} resolves with the loaded user data
2117
*/
22-
getAppByGuid(appGuid) {
18+
getById(id) {
2319
return this.connector
24-
.get(`/apps/${appGuid}/summary`)
25-
.catch(res => this.handleError(res));
26-
}
27-
28-
updateAppByGuid(appGuid, updates) {
29-
// Only include env vars if they are passed-in/defined to avoid overriding them.
30-
const safeUpdates =
31-
updates.environment_json !== undefined
32-
? {
33-
...updates,
34-
environment_json: formatEnvVars(updates.environment_json),
35-
}
36-
: updates;
37-
38-
return this.connector
39-
.put(`/apps/${appGuid}`, safeUpdates)
20+
.get(`/data/${id}`)
4021
.catch(res => this.handleError(res));
4122
}
4223

24+
/**
25+
* Throws a BluemixGraphQLError using information from the error response.
26+
*
27+
* @see https://ibm.biz/graphql-helpers
28+
*
29+
* @param {object} response an error response
30+
* @return {void}
31+
*/
4332
handleError(response) {
33+
// TODO: map your endpoint’s error response to the BluemixGraphQL
4434
throw BluemixGraphQLError({
35+
// An HTTP status code (e.g. 404).
4536
statusCode: response.statusCode,
37+
// A human-readable description of what went wrong (e.g. "Page not found").
4638
description: response.error.description,
39+
// An error code for looking up troubleshooting info (e.g. "MyApp_Err_NotFound").
4740
errorCode: response.error.error_code,
41+
// The endpoint that GraphQL was attempting to load (e.g. "https://api.example.org/user/123").
4842
targetEndpoint: response.options.uri,
43+
// The class where the error originated. (Don’t change this.)
4944
graphqlModel: this.constructor.name,
5045
});
5146
}

0 commit comments

Comments
 (0)