Skip to content

Commit bbe4362

Browse files
committed
feat: initial commit
1 parent 0903d25 commit bbe4362

10 files changed

+142
-47
lines changed

install.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const questions = [
5454
module.exports = async ({ project, context }) => {
5555
const opts = await context.prompt(questions);
5656

57-
replaceDefaultStringsInFiles({ ...opts, templateName: pkg.name, project });
57+
replaceDefaultStringsInFiles(Object.assign({ opts }, { templateName: pkg.name, project }));
5858

5959
// eslint-disable-next-line no-console
6060
console.log(`

package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "@gramps/data-source-base",
3-
"description": "GrAMPS GraphQL Data Source Base",
2+
"name": "@gramps/data-source-numbers",
3+
"description": "GrAMPS GraphQL Data Source for Numbers API",
44
"contributors": [
5-
"Jason Lengstorf <jason@lengstorf.com>"
5+
"Kim Brandwijk <kim.brandwijk@gmail.com>"
66
],
77
"repository": {
88
"type": "git",
9-
"url": "https://github.com/gramps-graphql/data-source-base.git"
9+
"url": "https://github.com/gramps-graphql/data-source-numbers.git"
1010
},
1111
"main": "dist/index.js",
1212
"directories": {
@@ -32,6 +32,8 @@
3232
],
3333
"license": "MIT",
3434
"dependencies": {
35+
"@gramps/errors": "^1.0.1",
36+
"@gramps/rest-helpers": "^1.0.0",
3537
"casual": "^1.5.19"
3638
},
3739
"peerDependencies": {
@@ -41,6 +43,8 @@
4143
"devDependencies": {
4244
"@gramps/cli": "^1.1.3",
4345
"@gramps/gramps": "^1.1.0",
46+
"@gramps/errors": "^1.0.1",
47+
"@gramps/rest-helpers": "^1.0.0",
4448
"babel-cli": "^6.24.1",
4549
"babel-eslint": "^8.0.3",
4650
"babel-jest": "^22.0.4",

src/connector.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { GraphQLConnector } from '@gramps/rest-helpers';
2+
3+
export default class NumbersConnector extends GraphQLConnector {
4+
constructor() {
5+
super();
6+
7+
this.headers['Content-Type'] = 'application/json';
8+
this.apiBaseUri = `http://numbersapi.com`;
9+
}
10+
}

src/context.js

-8
This file was deleted.

src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typeDefs from './schema.graphql';
2-
import context from './context';
2+
import Connector from './connector';
3+
import Model from './model';
34
import resolvers from './resolvers';
45
import mocks from './mocks';
56

@@ -8,9 +9,8 @@ import mocks from './mocks';
89
* https://gramps.js.org/data-source/data-source-overview/
910
*/
1011
export default {
11-
// TODO: Rename the context to describe the data source.
12-
namespace: 'DataSourceBase',
13-
context,
12+
namespace: 'Numbers',
13+
context: new Model({ connector: new Connector() }),
1414
typeDefs,
1515
resolvers,
1616
mocks,

src/mocks.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import casual from 'casual';
33

44
export default {
55
// TODO: Update to mock all schema types and fields
6-
PFX_DataSourceBase: () => ({
7-
id: casual.uuid,
8-
name: casual.name,
9-
lucky_numbers: () => new MockList([0, 3]),
6+
Numbers_Trivia: () => ({
7+
text: casual.sentence,
8+
found: casual.boolean,
9+
number: casual.number,
10+
type: casual.random_element(['trivia', 'date', 'math', 'year']),
11+
date: casual.date('mmm D')
1012
}),
1113
};

src/model.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { GrampsError } from '@gramps/errors';
2+
import { GraphQLModel } from '@gramps/rest-helpers';
3+
4+
export default class NumbersModel extends GraphQLModel {
5+
6+
async get(number, type) {
7+
console.log(await this.connector.get(`/${number}/${type}`))
8+
return this.connector.get(`/${number}/${type}`).catch(res =>
9+
this.throwError(res.error, {
10+
description: 'Could not get the info',
11+
}),
12+
);
13+
}
14+
15+
/**
16+
* Throws a custom GrAMPS error.
17+
* @param {Object} error the API error
18+
* @param {Object?} customErrorData additional error data to display
19+
* @return {void}
20+
*/
21+
throwError(
22+
{
23+
statusCode = 500,
24+
message = 'Something went wrong.',
25+
targetEndpoint = null,
26+
} = {},
27+
customErrorData = {},
28+
) {
29+
const defaults = {
30+
statusCode,
31+
targetEndpoint,
32+
errorCode: `${this.constructor.name}_Error`,
33+
description: message,
34+
graphqlModel: this.constructor.name,
35+
docsLink: 'https://ibm.biz/gramps-data-source-tutorial',
36+
};
37+
38+
throw GrampsError(Object.assign({defaults}, {customErrorData}));
39+
}
40+
}

src/resolvers.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ export default {
22
Query: {
33
// TODO: Update query resolver name and args to match the schema
44
// TODO: Update the context method to load the correct data
5-
getById: (_, { id }, context) => context.getById(id),
6-
},
7-
// TODO: Update to map data to your schema type(s) and field(s)
8-
PFX_DataSourceBase: {
9-
name: data => data.name || null,
10-
},
5+
trivia: (_, { number }, context) => context.get(number, 'trivia'),
6+
date: (_, { date }, context) => context.get(date, 'date'),
7+
math: (_, { number }, context) => context.get(number, 'math'),
8+
year: (_, { number }, context) => context.get(number, 'year'),
9+
}
1110
};

src/schema.graphql

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
type Query {
2-
"TODO: rename and add a description of this query"
3-
getById(
4-
"A unique identifier"
5-
id: ID!
6-
): PFX_DataSourceBase
2+
trivia(number: Int): Numbers_Trivia
3+
date(date: String): Numbers_Trivia
4+
math(number: Int): Numbers_Trivia
5+
year(number: Int): Numbers_Trivia
76
}
87

9-
"TODO: Choose a unique prefix and rename the type descriptively."
10-
type PFX_DataSourceBase {
11-
"A unique identifier"
12-
id: ID!
13-
"Describe each field to help people use the data more effectively."
14-
name: String
15-
"""
16-
An array of very super lucky numbers.
17-
18-
DISCLAIMER: The luckiness of these numbers is _not scientifically proven_.
19-
"""
20-
lucky_numbers: [Int]
8+
type Numbers_Trivia {
9+
text: String
10+
found: Boolean
11+
number: Int
12+
type: String
13+
date: String
2114
}

yarn.lock

+58-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@
8585
reify "^0.13.5"
8686
yargs "^10.0.3"
8787

88+
"@gramps/errors@^1.0.1":
89+
version "1.0.1"
90+
resolved "https://registry.yarnpkg.com/@gramps/errors/-/errors-1.0.1.tgz#b0852abbcc6207a18cd7bf1669c3947f9f015398"
91+
dependencies:
92+
cross-env "^5.1.1"
93+
graphql-apollo-errors "^2.0.2"
94+
uuid "^3.1.0"
95+
8896
"@gramps/gramps@^1.0.0", "@gramps/gramps@^1.1.0":
8997
version "1.1.0"
9098
resolved "https://registry.yarnpkg.com/@gramps/gramps/-/gramps-1.1.0.tgz#902a38a922d65b7c7b5eb27fcc2db9b2da125e39"
@@ -93,6 +101,14 @@
93101
graphql-tools "^2.14.1"
94102
lodash.merge "^4.6.0"
95103

104+
"@gramps/rest-helpers@^1.0.0":
105+
version "1.0.0"
106+
resolved "https://registry.yarnpkg.com/@gramps/rest-helpers/-/rest-helpers-1.0.0.tgz#8cda83e47f35db5c19e25c10d2d80053070c0ece"
107+
dependencies:
108+
dataloader "^1.3.0"
109+
request "^2.83.0"
110+
request-promise "^4.2.2"
111+
96112
"@semantic-release/commit-analyzer@^5.0.0":
97113
version "5.0.0"
98114
resolved "https://registry.yarnpkg.com/@semantic-release/commit-analyzer/-/commit-analyzer-5.0.0.tgz#767a2055b5cd0a67421b1d504f3ca7db97055c42"
@@ -1023,7 +1039,7 @@ block-stream@*:
10231039
dependencies:
10241040
inherits "~2.0.0"
10251041

1026-
bluebird@^3.5.1:
1042+
bluebird@^3.5.0, bluebird@^3.5.1:
10271043
version "3.5.1"
10281044
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"
10291045

@@ -1048,7 +1064,7 @@ [email protected]:
10481064
dependencies:
10491065
hoek "2.x.x"
10501066

1051-
1067+
[email protected], boom@^4.2.0:
10521068
version "4.3.1"
10531069
resolved "https://registry.yarnpkg.com/boom/-/boom-4.3.1.tgz#4f8a3005cb4a7e3889f749030fd25b96e01d2e31"
10541070
dependencies:
@@ -1584,6 +1600,10 @@ dashdash@^1.12.0:
15841600
dependencies:
15851601
assert-plus "^1.0.0"
15861602

1603+
dataloader@^1.3.0:
1604+
version "1.3.0"
1605+
resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-1.3.0.tgz#6fec5be4b30a712e4afd30b86b4334566b97673b"
1606+
15871607
dateformat@^1.0.11:
15881608
version "1.0.12"
15891609
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-1.0.12.tgz#9f124b67594c937ff706932e4a642cca8dbbfee9"
@@ -2528,6 +2548,14 @@ graphcool-yml@^0.0.6:
25282548
scuid "^1.0.2"
25292549
yaml-ast-parser "^0.0.40"
25302550

2551+
graphql-apollo-errors@^2.0.2:
2552+
version "2.0.3"
2553+
resolved "https://registry.yarnpkg.com/graphql-apollo-errors/-/graphql-apollo-errors-2.0.3.tgz#c4e0c605e0581a9514b3d3e03218328f2c4700bb"
2554+
dependencies:
2555+
lodash "^4.17.4"
2556+
minilog "^3.1.0"
2557+
seven-boom "^1.0.3"
2558+
25312559
graphql-config-extension-graphcool@^0.0.4:
25322560
version "0.0.4"
25332561
resolved "https://registry.yarnpkg.com/graphql-config-extension-graphcool/-/graphql-config-extension-graphcool-0.0.4.tgz#ee6febf942fb7baafb1f03af65b0f160ac47127c"
@@ -3830,6 +3858,10 @@ methods@~1.1.2:
38303858
version "1.1.2"
38313859
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
38323860

3861+
3862+
version "0.0.6"
3863+
resolved "https://registry.yarnpkg.com/microee/-/microee-0.0.6.tgz#a12bdb0103681e8b126a9b071eba4c467c78fffe"
3864+
38333865
micromatch@^2.1.5, micromatch@^2.3.11:
38343866
version "2.3.11"
38353867
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565"
@@ -3874,6 +3906,12 @@ mimic-response@^1.0.0:
38743906
version "1.0.0"
38753907
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.0.tgz#df3d3652a73fded6b9b0b24146e6fd052353458e"
38763908

3909+
minilog@^3.1.0:
3910+
version "3.1.0"
3911+
resolved "https://registry.yarnpkg.com/minilog/-/minilog-3.1.0.tgz#d2d0f1887ca363d1acf0ea86d5c4df293b3fb675"
3912+
dependencies:
3913+
microee "0.0.6"
3914+
38773915
"minimatch@2 || 3", minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
38783916
version "3.0.4"
38793917
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
@@ -4685,6 +4723,15 @@ request-promise-native@^1.0.3:
46854723
stealthy-require "^1.1.0"
46864724
tough-cookie ">=2.3.3"
46874725

4726+
request-promise@^4.2.2:
4727+
version "4.2.2"
4728+
resolved "https://registry.yarnpkg.com/request-promise/-/request-promise-4.2.2.tgz#d1ea46d654a6ee4f8ee6a4fea1018c22911904b4"
4729+
dependencies:
4730+
bluebird "^3.5.0"
4731+
request-promise-core "1.1.1"
4732+
stealthy-require "^1.1.0"
4733+
tough-cookie ">=2.3.3"
4734+
46884735
[email protected], request@^2.74.0:
46894736
version "2.81.0"
46904737
resolved "https://registry.yarnpkg.com/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0"
@@ -4931,6 +4978,14 @@ [email protected]:
49314978
version "1.1.0"
49324979
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.0.tgz#d0bd85536887b6fe7c0d818cb962d9d91c54e656"
49334980

4981+
seven-boom@^1.0.3:
4982+
version "1.0.3"
4983+
resolved "https://registry.yarnpkg.com/seven-boom/-/seven-boom-1.0.3.tgz#433038cd5f9e491607c374d3a23934be02e44473"
4984+
dependencies:
4985+
boom "^4.2.0"
4986+
lodash "^4.17.4"
4987+
uuid "^3.0.1"
4988+
49344989
shebang-command@^1.2.0:
49354990
version "1.2.0"
49364991
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
@@ -5436,7 +5491,7 @@ [email protected]:
54365491
version "1.0.1"
54375492
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
54385493

5439-
uuid@^3.0.0, uuid@^3.1.0:
5494+
uuid@^3.0.0, uuid@^3.0.1, uuid@^3.1.0:
54405495
version "3.1.0"
54415496
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.1.0.tgz#3dd3d3e790abc24d7b0d3a034ffababe28ebbc04"
54425497

0 commit comments

Comments
 (0)