Skip to content

Commit f7e0b76

Browse files
committed
move schema to separate file
1 parent 3ecefdf commit f7e0b76

File tree

6 files changed

+196
-103
lines changed

6 files changed

+196
-103
lines changed

__tests__/__snapshots__/index.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`expect res setHeader and end to match snapshot 1`] = `
3+
exports[`index expect res setHeader and end to match snapshot 1`] = `
44
Array [
55
Array [
66
"Content-Type",
@@ -13,7 +13,7 @@ Array [
1313
]
1414
`;
1515

16-
exports[`expect res setHeader and end to match snapshot 2`] = `
16+
exports[`index expect res setHeader and end to match snapshot 2`] = `
1717
Object {
1818
"data": Object {
1919
"component": Object {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`schema expect schema to match snapshot 1`] = `
4+
GraphQLSchema {
5+
"_directives": Array [
6+
GraphQLDirective {
7+
"args": Array [
8+
Object {
9+
"defaultValue": undefined,
10+
"description": "Skipped when true.",
11+
"name": "if",
12+
"type": "Boolean!",
13+
},
14+
],
15+
"description": "Directs the executor to skip this field or fragment when the \`if\` argument is true.",
16+
"locations": Array [
17+
"FIELD",
18+
"FRAGMENT_SPREAD",
19+
"INLINE_FRAGMENT",
20+
],
21+
"name": "skip",
22+
},
23+
GraphQLDirective {
24+
"args": Array [
25+
Object {
26+
"defaultValue": undefined,
27+
"description": "Included when true.",
28+
"name": "if",
29+
"type": "Boolean!",
30+
},
31+
],
32+
"description": "Directs the executor to include this field or fragment only when the \`if\` argument is true.",
33+
"locations": Array [
34+
"FIELD",
35+
"FRAGMENT_SPREAD",
36+
"INLINE_FRAGMENT",
37+
],
38+
"name": "include",
39+
},
40+
GraphQLDirective {
41+
"args": Array [
42+
Object {
43+
"defaultValue": "No longer supported",
44+
"description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted in [Markdown](https://daringfireball.net/projects/markdown/).",
45+
"name": "reason",
46+
"type": "String",
47+
},
48+
],
49+
"description": "Marks an element of a GraphQL schema as no longer supported.",
50+
"locations": Array [
51+
"FIELD_DEFINITION",
52+
"ENUM_VALUE",
53+
],
54+
"name": "deprecated",
55+
},
56+
],
57+
"_implementations": Object {},
58+
"_mutationType": null,
59+
"_queryType": "Query",
60+
"_subscriptionType": null,
61+
"_typeMap": Object {
62+
"Boolean": "Boolean",
63+
"Component": "Component",
64+
"Parameter": "Parameter",
65+
"Person": "Person",
66+
"Query": "Query",
67+
"Registry": "Registry",
68+
"Repository": "Repository",
69+
"String": "String",
70+
"__Directive": "__Directive",
71+
"__DirectiveLocation": "__DirectiveLocation",
72+
"__EnumValue": "__EnumValue",
73+
"__Field": "__Field",
74+
"__InputValue": "__InputValue",
75+
"__Schema": "__Schema",
76+
"__Type": "__Type",
77+
"__TypeKind": "__TypeKind",
78+
},
79+
}
80+
`;

__tests__/index.test.js

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -62,70 +62,73 @@ const query = `
6262
}
6363
`;
6464

65-
test('expect type of middleware to be function', () => {
66-
expect(typeof middleware).toBe('function');
67-
});
65+
describe('index', () => {
66+
test('expect type of middleware to be function', () => {
67+
expect(typeof middleware).toBe('function');
68+
});
6869

69-
test('expect res setHeader and end to match snapshot', async () => {
70-
fetch.mockResponses(
71-
[JSON.stringify({
72-
href: options.baseUrl,
73-
ocVersion: '1.2.3',
74-
type: 'mock-registry'
75-
})],
76-
[JSON.stringify({
77-
name: 'oc-apod',
78-
description: 'This component displays picture, title, description and date of the NASA\'s Astronomy Picture of the Day',
79-
version: '1.3.0',
80-
allVersions: ['1.0.0', '1.1.0', '1.1.1', '1.2.0', '1.3.0'],
81-
oc: {
82-
parameters: {
83-
apiKey: {
84-
type: 'string',
85-
mandatory: true,
86-
example: 'DEMO_KEY',
87-
description: 'The NASA Open APIs key'
70+
test('expect res setHeader and end to match snapshot', async () => {
71+
fetch.mockResponses(
72+
[JSON.stringify({
73+
href: options.baseUrl,
74+
ocVersion: '1.2.3',
75+
type: 'mock-registry'
76+
})],
77+
[JSON.stringify({
78+
name: 'oc-apod',
79+
description: 'This component displays picture, title, description and date of the NASA\'s Astronomy Picture of the Day',
80+
version: '1.3.0',
81+
allVersions: ['1.0.0', '1.1.0', '1.1.1', '1.2.0', '1.3.0'],
82+
oc: {
83+
parameters: {
84+
apiKey: {
85+
type: 'string',
86+
mandatory: true,
87+
example: 'DEMO_KEY',
88+
description: 'The NASA Open APIs key'
89+
}
8890
}
8991
}
90-
}
91-
})],
92-
[JSON.stringify({
93-
components: [
94-
`${options.baseUrl}oc-a-component`
95-
]
96-
})],
97-
[JSON.stringify({
98-
name: 'oc-a-component',
99-
description: 'Awesome OpenComponent',
100-
version: '4.5.6',
101-
allVersions: ['4.5.4', '4.5.5', '4.5.6'],
102-
oc: {
103-
parameters: {
104-
id: {
105-
type: 'string',
106-
mandatory: true,
107-
example: '789',
108-
description: 'The Id'
92+
})],
93+
[JSON.stringify({
94+
components: [
95+
`${options.baseUrl}oc-a-component`
96+
]
97+
})],
98+
[JSON.stringify({
99+
name: 'oc-a-component',
100+
description: 'Awesome OpenComponent',
101+
version: '4.5.6',
102+
allVersions: ['4.5.4', '4.5.5', '4.5.6'],
103+
oc: {
104+
parameters: {
105+
id: {
106+
type: 'string',
107+
mandatory: true,
108+
example: '789',
109+
description: 'The Id'
110+
}
109111
}
110112
}
111-
}
112-
})]
113-
);
113+
})]
114+
);
114115

115-
const req = {
116-
method: 'GET',
117-
headers: {
118-
'Content-Type': 'application/json'
119-
},
120-
url: `?query=${query}`
121-
};
116+
const req = {
117+
method: 'GET',
118+
headers: {
119+
'Content-Type': 'application/json'
120+
},
121+
url: `?query=${query}`
122+
};
122123

123-
const res = { setHeader: jest.fn(), end: jest.fn() };
124+
const res = { setHeader: jest.fn(), end: jest.fn() };
124125

125-
await middleware(req, res);
126+
await middleware(req, res);
126127

127-
expect(res.setHeader).toHaveBeenCalled();
128-
expect(res.setHeader.mock.calls).toMatchSnapshot();
129-
expect(res.end).toHaveBeenCalled();
130-
expect(JSON.parse(res.end.mock.calls)).toMatchSnapshot();
128+
expect(res.setHeader).toHaveBeenCalled();
129+
expect(res.setHeader.mock.calls).toMatchSnapshot();
130+
expect(res.end).toHaveBeenCalled();
131+
expect(JSON.parse(res.end.mock.calls)).toMatchSnapshot();
132+
});
131133
});
134+

__tests__/shema.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const schema = require('../src/schema');
2+
3+
describe('schema', () => {
4+
test('expect schema to match snapshot', () => {
5+
expect(schema).toMatchSnapshot();
6+
});
7+
});

src/index.js

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,8 @@
11
/* eslint-disable arrow-body-style */
22

33
const graphqlHTTP = require('express-graphql');
4-
const { buildSchema } = require('graphql');
54
const fetch = require('node-fetch');
6-
7-
const schema = buildSchema(`
8-
type Registry {
9-
href: String
10-
ocVersion: String
11-
type: String
12-
dependencies: [String]
13-
}
14-
15-
type Person {
16-
name: String
17-
email: String
18-
}
19-
20-
type Repository {
21-
type: String
22-
url: String
23-
}
24-
25-
type Parameter {
26-
key: String
27-
type: String
28-
mandatory: Boolean
29-
example: String
30-
description: String
31-
}
32-
33-
type Component {
34-
name: String
35-
description: String
36-
version: String
37-
allVersions: [String]
38-
author: Person
39-
repository: Repository
40-
parameters: [Parameter]
41-
}
42-
43-
type Query {
44-
registry: Registry
45-
component(name: String): Component
46-
components: [Component]
47-
}
48-
`);
5+
const schema = require('./schema');
496

507
const fetchComponent = async (url) => {
518
return fetch(url)

src/schema.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { buildSchema } = require('graphql');
2+
3+
const schema = buildSchema(`
4+
type Registry {
5+
href: String
6+
ocVersion: String
7+
type: String
8+
dependencies: [String]
9+
}
10+
11+
type Person {
12+
name: String
13+
email: String
14+
}
15+
16+
type Repository {
17+
type: String
18+
url: String
19+
}
20+
21+
type Parameter {
22+
key: String
23+
type: String
24+
mandatory: Boolean
25+
example: String
26+
description: String
27+
}
28+
29+
type Component {
30+
name: String
31+
description: String
32+
version: String
33+
allVersions: [String]
34+
author: Person
35+
repository: Repository
36+
parameters: [Parameter]
37+
}
38+
39+
type Query {
40+
registry: Registry
41+
component(name: String): Component
42+
components: [Component]
43+
}
44+
`);
45+
46+
module.exports = schema;

0 commit comments

Comments
 (0)