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

Lines changed: 9 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 3 deletions
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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 8 additions & 24 deletions
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 = `{
Lines changed: 105 additions & 0 deletions
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+
})

0 commit comments

Comments
 (0)