Skip to content

Commit 33fbb1e

Browse files
committed
add registry type
1 parent eaa69d1 commit 33fbb1e

File tree

10 files changed

+3418
-0
lines changed

10 files changed

+3418
-0
lines changed

.babelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": [
3+
"latest",
4+
"stage-3"
5+
]
6+
}

.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "airbnb",
3+
"rules": {
4+
"jsx-a11y/img-has-alt": "off",
5+
"comma-dangle": "off",
6+
"import/no-extraneous-dependencies": "off"
7+
},
8+
"env": {
9+
"jest": true
10+
}
11+
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
# oc-registry-graphql-express-middleware
2+
23
oc-registry-graphql-express-middleware
4+
5+
```bash
6+
yarn
7+
yarn test
8+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`expect res setHeader and end to match snapshot 1`] = `
4+
Array [
5+
Array [
6+
"Content-Type",
7+
"application/json; charset=utf-8",
8+
],
9+
]
10+
`;
11+
12+
exports[`expect res setHeader and end to match snapshot 2`] = `
13+
Array [
14+
Array [
15+
"{\\"data\\":{\\"registry\\":{\\"href\\":\\"http://mock:3000/\\",\\"ocVersion\\":\\"8.1.5\\",\\"type\\":\\"mock-registry\\"}}}",
16+
],
17+
]
18+
`;

__tests__/index.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const fetch = require('node-fetch');
2+
const factory = require('../src/index');
3+
4+
const options = {
5+
baseUrl: 'http://mock:3000/',
6+
graphiql: true
7+
};
8+
9+
const middleware = factory(options);
10+
11+
test('expect type of middleware to be function', () => {
12+
expect(typeof middleware).toBe('function');
13+
});
14+
15+
test('expect res setHeader and end to match snapshot', async () => {
16+
fetch.mockResponse(JSON.stringify({
17+
href: options.baseUrl,
18+
ocVersion: '8.1.5',
19+
type: 'mock-registry'
20+
}));
21+
22+
const req = {
23+
method: 'GET',
24+
headers: {},
25+
url: `?query=
26+
{
27+
registry {
28+
href
29+
ocVersion
30+
type
31+
}
32+
}
33+
`
34+
};
35+
36+
const res = { setHeader: jest.fn(), end: jest.fn() };
37+
38+
await middleware(req, res);
39+
40+
expect(res.setHeader.mock.calls).toMatchSnapshot();
41+
expect(res.end.mock.calls).toMatchSnapshot();
42+
});

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./src');

package.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "oc-registry-graphql-express-middleware",
3+
"version": "1.0.0-alpha.1",
4+
"description": "oc-registry-graphql-express-middleware",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest --coverage"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/opencomponents/oc-registry-graphql-express-middleware.git"
12+
},
13+
"keywords": [
14+
"opencomponents",
15+
"registry",
16+
"graphql",
17+
"express",
18+
"middleware"
19+
],
20+
"author": {
21+
"name": "Mattia Richetto",
22+
"email": "[email protected]"
23+
},
24+
"license": "ISC",
25+
"bugs": {
26+
"url": "https://github.com/opencomponents/oc-registry-graphql-express-middleware/issues"
27+
},
28+
"homepage": "https://github.com/opencomponents/oc-registry-graphql-express-middleware#readme",
29+
"devDependencies": {
30+
"babel-core": "^6.24.1",
31+
"babel-eslint": "^7.2.3",
32+
"babel-polyfill": "^6.23.0",
33+
"babel-preset-latest": "^6.24.1",
34+
"babel-preset-stage-3": "^6.24.1",
35+
"eslint": "^3.19.0",
36+
"eslint-config-airbnb": "^14.1.0",
37+
"eslint-plugin-import": "^2.2.0",
38+
"eslint-plugin-jsx-a11y": "^5.0.1",
39+
"isomorphic-fetch": "^2.2.1",
40+
"jest": "^20.0.0",
41+
"jest-fetch-mock": "^1.1.1"
42+
},
43+
"dependencies": {
44+
"express-graphql": "^0.6.4",
45+
"graphql": "^0.9.6",
46+
"node-fetch": "^1.6.3"
47+
},
48+
"jest": {
49+
"automock": false,
50+
"setupFiles": [
51+
"./setupJest.js"
52+
],
53+
"testEnvironment": "node"
54+
}
55+
}

setupJest.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('isomorphic-fetch');
2+
const fetch = require('jest-fetch-mock');
3+
4+
jest.setMock('node-fetch', fetch);

src/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable arrow-body-style */
2+
3+
const graphqlHTTP = require('express-graphql');
4+
const { buildSchema } = require('graphql');
5+
const fetch = require('node-fetch');
6+
7+
const schema = buildSchema(`
8+
type Registry {
9+
href: String
10+
ocVersion: String
11+
type: String
12+
}
13+
14+
type Query {
15+
registry: Registry
16+
}
17+
`);
18+
19+
const root = (options) => {
20+
return {
21+
registry: () => {
22+
return fetch(options.baseUrl)
23+
.then(response => response.json())
24+
.then((data) => {
25+
return {
26+
href: data.href,
27+
ocVersion: data.ocVersion,
28+
type: data.type
29+
};
30+
});
31+
}
32+
};
33+
};
34+
35+
const factory = (options) => {
36+
return graphqlHTTP({
37+
schema,
38+
rootValue: root(options),
39+
graphiql: options.graphiql,
40+
});
41+
};
42+
43+
module.exports = factory;

0 commit comments

Comments
 (0)