Skip to content

Commit 894c055

Browse files
committed
Add test for requestOptions and links
Signed-off-by: Alan Cha <[email protected]>
1 parent bfa93ed commit 894c055

13 files changed

+336
-45
lines changed

packages/openapi-to-graphql/lib/oas_3_tools.js

+9-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/oas_3_tools.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/schema_builder.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/schema_builder.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/src/oas_3_tools.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1101,10 +1101,14 @@ export function storeSaneName(
11011101
* sanitized
11021102
*/
11031103
export function sanitizeObjectKeys(obj: object): object {
1104-
return Object.keys(obj).reduce((acc, key) => {
1105-
acc[sanitize(key, CaseStyle.camelCase)] = obj[key]
1106-
return acc
1107-
}, {})
1104+
if (typeof obj === 'object') {
1105+
return Object.keys(obj).reduce((acc, key) => {
1106+
acc[sanitize(key, CaseStyle.camelCase)] = obj[key]
1107+
return acc
1108+
}, {})
1109+
} else {
1110+
return undefined
1111+
}
11081112
}
11091113

11101114
/**

packages/openapi-to-graphql/test/example_api.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { GraphQLOperationType } from '../lib/types/graphql'
1616

1717
const oas = require('./fixtures/example_oas.json')
1818
const PORT = 3002
19-
// update PORT for this test case:
19+
// Update PORT for this test case:
2020
oas.servers[0].variables.port.default = String(PORT)
2121

2222
let createdSchema

packages/openapi-to-graphql/test/example_api2.test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77

88
/* globals beforeAll, test, expect */
99

10-
import { graphql, parse, validate } from 'graphql'
10+
import { graphql } from 'graphql'
1111

1212
import * as openAPIToGraphQL from '../lib/index'
1313
import { startServer, stopServer } from './example_api2_server'
1414

1515
const oas = require('./fixtures/example_oas2.json')
1616
const PORT = 3004
17-
// update PORT for this test case:
17+
// Update PORT for this test case:
1818
oas.servers[0].variables.port.default = String(PORT)
1919

2020
let createdSchema
2121

22+
/**
23+
* This test suite is used to verify the behavior of the operationIdFieldNames
24+
* option.
25+
*
26+
* It is necessary to make a separate OAS because we need all of operations to
27+
* have operationIDs.
28+
*/
29+
2230
/**
2331
* Set up the schema first and run example API server
2432
*/
@@ -41,7 +49,8 @@ afterAll(() => {
4149
})
4250

4351
/**
44-
* There should be two operations
52+
* There should be two operations.
53+
*
4554
* One will be given a field name from the operationId, i.e. user, and the other
4655
* one, because it does not have an operationId defined, will have an
4756
* autogenerated field name based on the path, i.e. getUser

packages/openapi-to-graphql/test/example_api3.test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ const oas = require('./fixtures/example_oas.json')
1818
const oas3 = require('./fixtures/example_oas3.json')
1919
const PORT = 3005
2020
const PORT2 = 3006
21-
// update PORT for this test case:
21+
// Update PORT for this test case:
2222
oas.servers[0].variables.port.default = String(PORT)
2323
oas3.servers[0].variables.port.default = String(PORT2)
2424

25+
/**
26+
* This test suite is used to verify the behavior of interOAS links, i.e.
27+
* links across different OASs
28+
*/
29+
2530
let createdSchema
2631

2732
/**
@@ -30,9 +35,7 @@ let createdSchema
3035
beforeAll(() => {
3136
return Promise.all([
3237
openAPIToGraphQL
33-
.createGraphQLSchema([oas, oas3], {
34-
fillEmptyResponses: true
35-
})
38+
.createGraphQLSchema([oas, oas3])
3639
.then(({ schema, report }) => {
3740
createdSchema = schema
3841
}),

packages/openapi-to-graphql/test/example_api4.test.ts

+8-24
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,23 @@
88
/* globals beforeAll, test, expect */
99

1010
import { graphql } from 'graphql'
11-
1211
import * as openAPIToGraphQL from '../lib/index'
13-
import { Options } from '../lib/types/options'
14-
import { startServer, stopServer } from './example_api_server'
1512

1613
const oas = require('./fixtures/example_oas4.json')
17-
const PORT = 3007
18-
// Update PORT for this test case:
19-
oas.servers[0].variables.port.default = String(PORT)
14+
15+
// This test suite is used to verify the behavior of anyOf and oneOf handling
2016

2117
let createdSchema
2218

2319
/**
24-
* Set up the schema first and run example API server
20+
* Set up the schema
2521
*/
2622
beforeAll(() => {
27-
return Promise.all([
28-
openAPIToGraphQL
29-
.createGraphQLSchema(oas, {
30-
fillEmptyResponses: true
31-
})
32-
.then(({ schema, report }) => {
33-
createdSchema = schema
34-
}),
35-
startServer(PORT)
36-
])
37-
})
38-
39-
/**
40-
* Shut down API server
41-
*/
42-
afterAll(() => {
43-
return stopServer()
23+
return openAPIToGraphQL
24+
.createGraphQLSchema(oas)
25+
.then(({ schema, report }) => {
26+
createdSchema = schema
27+
})
4428
})
4529

4630
const anyOfQuery = `{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
2+
// Node module: openapi-to-graphql
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
'use strict'
7+
8+
/* globals beforeAll, test, expect */
9+
10+
import { graphql, parse, validate } from 'graphql'
11+
12+
import * as openAPIToGraphQL from '../lib/index'
13+
import { Options } from '../lib/types/options'
14+
import { startServer, stopServer } from './example_api6_server'
15+
16+
const oas = require('./fixtures/example_oas6.json')
17+
const PORT = 3009
18+
// Update PORT for this test case:
19+
oas.servers[0].variables.port.default = String(PORT)
20+
21+
let createdSchema
22+
23+
/**
24+
* Set up the schema first and run example API server
25+
*/
26+
beforeAll(() => {
27+
return Promise.all([
28+
openAPIToGraphQL.createGraphQLSchema(oas).then(({ schema, report }) => {
29+
createdSchema = schema
30+
}),
31+
startServer(PORT)
32+
])
33+
})
34+
35+
/**
36+
* Shut down API server
37+
*/
38+
afterAll(() => {
39+
return stopServer()
40+
})
41+
42+
test('Option requestOptions should work with links', () => {
43+
// Verifying the behavior of the link by itself
44+
const query = `{
45+
object {
46+
object2Link {
47+
data
48+
}
49+
withParameter: object2Link (specialheader: "extra data"){
50+
data
51+
}
52+
}
53+
}`
54+
55+
const promise = graphql(createdSchema, query).then(result => {
56+
expect(result.data).toEqual({
57+
object: {
58+
object2Link: {
59+
data: 'object2'
60+
},
61+
withParameter: {
62+
data: "object2 with special header: 'extra data'"
63+
}
64+
}
65+
})
66+
})
67+
68+
const options: Options = {
69+
requestOptions: {
70+
url: undefined,
71+
headers: {
72+
specialheader: 'requestOptions'
73+
}
74+
}
75+
}
76+
77+
const query2 = `{
78+
object {
79+
object2Link {
80+
data
81+
}
82+
}
83+
}`
84+
85+
const promise2 = openAPIToGraphQL
86+
.createGraphQLSchema(oas, options)
87+
.then(({ schema }) => {
88+
const ast = parse(query2)
89+
const errors = validate(schema, ast)
90+
expect(errors).toEqual([])
91+
return graphql(schema, query2).then(result => {
92+
expect(result).toEqual({
93+
data: {
94+
object: {
95+
object2Link: {
96+
data: "object2 with special header: 'requestOptions'" // Data from requestOptions in a link
97+
}
98+
}
99+
}
100+
})
101+
})
102+
})
103+
104+
return Promise.all([promise, promise2])
105+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
2+
// Node module: openapi-to-graphql
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
'use strict'
7+
8+
let server // holds server object for shutdown
9+
10+
/**
11+
* Starts the server at the given port
12+
*/
13+
function startServer(PORT) {
14+
const express = require('express')
15+
const app = express()
16+
17+
const bodyParser = require('body-parser')
18+
app.use(bodyParser.json())
19+
20+
app.get('/api/object', (req, res) => {
21+
console.log(req.method, req.path)
22+
res.send({
23+
data: 'object'
24+
})
25+
})
26+
27+
app.get('/api/object2', (req, res) => {
28+
console.log(req.method, req.path)
29+
if (typeof req.headers.specialheader === 'string') {
30+
res.send({
31+
data: `object2 with special header: '${req.headers.specialheader}'`
32+
})
33+
} else {
34+
res.send({
35+
data: 'object2'
36+
})
37+
}
38+
})
39+
40+
return new Promise(resolve => {
41+
server = app.listen(PORT, () => {
42+
console.log(`Example API accessible on port ${PORT}`)
43+
resolve()
44+
})
45+
})
46+
}
47+
48+
/**
49+
* Stops server.
50+
*/
51+
function stopServer() {
52+
return new Promise(resolve => {
53+
server.close(() => {
54+
console.log(`Stopped API server`)
55+
resolve()
56+
})
57+
})
58+
}
59+
60+
// if run from command line, start server:
61+
if (require.main === module) {
62+
startServer(3006)
63+
}
64+
65+
module.exports = {
66+
startServer,
67+
stopServer
68+
}

packages/openapi-to-graphql/test/example_gql_server.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const openAPIToGraphQL = require('../lib/index')
1414
// const oas = require('./fixtures/example_oas.json')
1515
// const oas2 = require('./fixtures/example_oas2.json')
1616
// const oas3 = require('./fixtures/example_oas3.json')
17-
const oas4 = require('./fixtures/example_oas4.json')
17+
// const oas4 = require('./fixtures/example_oas4.json')
18+
const oas6 = require('./fixtures/example_oas6.json')
1819

1920
// const oas = require('./fixtures/github.json')
2021
// const oas = require('./fixtures/instagram.json')
@@ -29,7 +30,14 @@ const oas4 = require('./fixtures/example_oas4.json')
2930
// const oas = yamljs.parse(fs.readFileSync('../tmp/APIs/box.com/content/2.0/swagger.yaml', 'utf8'))
3031

3132
openAPIToGraphQL
32-
.createGraphQLSchema(oas4)
33+
.createGraphQLSchema(oas6, {
34+
requestOptions: {
35+
url: undefined,
36+
headers: {
37+
specialheader: 'requestOptions' // Option requestOptions
38+
}
39+
}
40+
})
3341
.then(({ schema, report }) => {
3442
console.log(JSON.stringify(report, null, 2))
3543

0 commit comments

Comments
 (0)