Skip to content

Commit 6b216a9

Browse files
committed
feat: initial OSS commit
1 parent df83406 commit 6b216a9

File tree

8 files changed

+61
-51
lines changed

8 files changed

+61
-51
lines changed

src/connector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLConnector } from '@console/gramps';
1+
import { GraphQLConnector } from '@gramps/gramps-express';
22

33
// TODO: change `YourDataSourceConnector` to a descriptive name
44
export default class YourDataSourceConnector extends GraphQLConnector {

src/model.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLModel, GrampsError } from '@console/gramps';
1+
import { GraphQLModel, GrampsError } from '@gramps/gramps-express';
22

33
/*
44
* For more information on data source models, see
@@ -13,32 +13,35 @@ export default class YourDataSourceModel extends GraphQLModel {
1313
* @return {Promise} resolves with the loaded user data
1414
*/
1515
getById(id) {
16-
return this.connector
17-
.get(`/data/${id}`)
18-
.catch(res => this.handleError(res));
16+
return this.connector.get(`/data/${id}`).catch(res =>
17+
this.throwError(res, {
18+
description: 'This is an example call. Add your own!',
19+
docsLink:
20+
'https://gramps-graphql.github.io/gramps-express/data-source/tutorial/',
21+
}),
22+
);
1923
}
2024

2125
/**
22-
* Throws a GrampsError using information from the error response.
23-
*
24-
* @see https://ibm.biz/graphql-helpers
25-
*
26-
* @param {object} response an error response
26+
* Throws a custom GrAMPS error.
27+
* @param {Object} error the API error
28+
* @param {Object?} customErrorData additional error data to display
2729
* @return {void}
2830
*/
29-
handleError(response) {
30-
// TODO: map your endpoint’s error response to the GrampsError format.
31-
throw GrampsError({
32-
// An HTTP status code (e.g. 404).
33-
statusCode: response.statusCode,
34-
// A human-readable description of what went wrong (e.g. "Page not found").
35-
description: response.error.description,
36-
// An error code for looking up troubleshooting info (e.g. "MyApp_Err_NotFound").
37-
errorCode: response.error.error_code,
38-
// The endpoint that GraphQL was attempting to load (e.g. "https://api.example.org/user/123").
39-
targetEndpoint: response.options.uri,
40-
// The class where the error originated. (Don’t change this.)
31+
throwError(error, customErrorData = {}) {
32+
// TODO Edit these defaults to be helpful for people using your data source.
33+
const defaults = {
34+
statusCode: error.statusCode || 500,
35+
errorCode: `${this.constructor.name}_Error`,
36+
description: error.message || 'Something went wrong.',
37+
targetEndpoint: error.options ? error.options.uri : null,
4138
graphqlModel: this.constructor.name,
39+
docsLink: null,
40+
};
41+
42+
throw GrampsError({
43+
...defaults,
44+
...customErrorData,
4245
});
4346
}
4447
}

src/resolvers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export default {
1313
YourDataSource: (rootValue, { id }, context) =>
1414
new Promise((resolve, reject) => {
1515
// TODO: Update to use the model and call the proper method.
16-
context.YourDataSource.getById(id).then(resolve).catch(reject);
16+
context.YourDataSource
17+
.getById(id)
18+
.then(resolve)
19+
.catch(reject);
1720
}),
1821
},
1922

test/__mocks__/console-platform-key-value-store.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLConnector } from '@console/gramps';
1+
import { GraphQLConnector } from '@gramps/gramps-express';
22
import Connector from '../src/connector';
33

44
// TODO: Update the data source name.
File renamed without changes.
Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLModel } from '@console/gramps';
1+
import { GraphQLModel } from '@gramps/gramps-express';
22
import Model from '../src/model';
33
import Connector from '../src/connector';
44

@@ -16,18 +16,6 @@ const DATA_SOURCE_NAME = 'YourDataSource';
1616
const connector = new Connector();
1717
const model = new Model({ connector });
1818

19-
// TODO: Update this error to match the error format returned by your endpoint.
20-
const mockError = {
21-
statusCode: 401,
22-
error: {
23-
description: 'test error',
24-
error_code: 'TEST_ERROR',
25-
},
26-
options: {
27-
uri: 'https://example.org/',
28-
},
29-
};
30-
3119
describe(`${DATA_SOURCE_NAME}Model`, () => {
3220
it('inherits the GraphQLModel class', () => {
3321
expect(model).toBeInstanceOf(GraphQLModel);
@@ -42,11 +30,11 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
4230
expect(spy).toHaveBeenCalledWith('/data/1234');
4331
});
4432

45-
it('throws a BluemixGraphQLError if something goes wrong', async () => {
33+
it('throws a GrampsError if something goes wrong', async () => {
4634
expect.assertions(1);
4735

4836
model.connector.get.mockImplementationOnce(() =>
49-
Promise.reject(mockError),
37+
Promise.reject({ no: 'good' }),
5038
);
5139

5240
try {
@@ -58,9 +46,17 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
5846
});
5947
});
6048

61-
describe('handleError()', () => {
62-
it('converts an error from the endpoint into a BluemixGraphQLError', async () => {
63-
expect.assertions(6);
49+
describe('throwError()', () => {
50+
// TODO: Update this error to match the error format returned by your endpoint.
51+
const mockError = {
52+
statusCode: 401,
53+
options: {
54+
uri: 'https://example.org/',
55+
},
56+
};
57+
58+
it('converts an error from the endpoint into a GrampsError', async () => {
59+
expect.assertions(4);
6460

6561
/*
6662
* To simulate a failed call, we tell Jest to return a rejected Promise
@@ -74,14 +70,9 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
7470
// TODO: Update to use one of your model’s methods.
7571
await model.getById(1234);
7672
} catch (error) {
77-
// Check that BluemixGraphQLError properly received the error detail.
73+
// Check that GrampsError properly received the error detail.
7874
expect(error).toHaveProperty('isBoom', true);
7975
expect(error.output).toHaveProperty('statusCode', 401);
80-
expect(error.output.payload).toHaveProperty(
81-
'description',
82-
'test error',
83-
);
84-
expect(error.output.payload).toHaveProperty('errorCode', 'TEST_ERROR');
8576
expect(error.output.payload).toHaveProperty(
8677
'targetEndpoint',
8778
'https://example.org/',
@@ -92,5 +83,21 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
9283
);
9384
}
9485
});
86+
87+
it('creates a default GrampsError if no custom error data is supplied', async () => {
88+
try {
89+
await model.throwError({});
90+
} catch (error) {
91+
expect(error.output.statusCode).toBe(500);
92+
expect(error.output.payload.errorCode).toBe(
93+
`${DATA_SOURCE_NAME}Model_Error`,
94+
);
95+
expect(error.output.payload.description).toBe('Something went wrong.');
96+
expect(error.output.payload.graphqlModel).toBe(
97+
`${DATA_SOURCE_NAME}Model`,
98+
);
99+
expect(error.output.payload.targetEndpoint).toBeNull();
100+
}
101+
});
95102
});
96103
});

0 commit comments

Comments
 (0)