Skip to content

Commit bb460b3

Browse files
committed
Improve comments
Signed-off-by: Alan Cha <[email protected]>
1 parent 2e18ffd commit bb460b3

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

packages/openapi-to-graphql/lib/resolver_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/resolver_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/resolver_builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ export function getResolver({
325325
`Call ${options.method.toUpperCase()} ${
326326
options.url
327327
}?${querystring.stringify(options.qs)}\n` +
328-
`headers:${JSON.stringify(options.headers)}`
328+
`headers: ${JSON.stringify(options.headers)}\n` +
329+
`request body: ${options.body}`
329330
)
330331

331332
return new Promise((resolve, reject) => {

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ afterAll(() => {
4545
})
4646

4747
test('Get descriptions', () => {
48+
// Get all the descriptions of the fields on the GraphQL object type Car
4849
const query = `{
4950
__type(name: "Car") {
5051
name
@@ -85,6 +86,7 @@ test('Get descriptions', () => {
8586
})
8687

8788
test('Get resource (incl. enum)', () => {
89+
// Status is an enum
8890
const query = `{
8991
user (username: "arlene") {
9092
name
@@ -109,6 +111,7 @@ test('Get resource 2', () => {
109111
})
110112
})
111113

114+
// OAS allows you to define response objects with HTTP code with the XX wildcard syntax
112115
test('Get resource with status code: 2XX', () => {
113116
const query = `{
114117
papers {
@@ -132,6 +135,10 @@ test('Get resource with status code: 2XX', () => {
132135
})
133136
})
134137

138+
/**
139+
* Some operations do not have a response body. The option fillEmptyResponses
140+
* allows OtG to handle these cases.
141+
*/
135142
test('Get resource with no response schema and status code: 204 and fillEmptyResponses', () => {
136143
const query = `{
137144
bonuses
@@ -145,6 +152,7 @@ test('Get resource with no response schema and status code: 204 and fillEmptyRes
145152
})
146153
})
147154

155+
// Link objects in the OAS allow OtG to create nested GraphQL objects that resolve on different API calls
148156
test('Get nested resource via link $response.body#/...', () => {
149157
const query = `{
150158
user (username: "arlene") {
@@ -189,6 +197,7 @@ test('Get nested resource via link $request.path#/... and $request.query#/', ()
189197
})
190198
})
191199

200+
// Both an operationId and an operationRef can be used to create a link object
192201
test('Get nested resource via link operationRef', () => {
193202
const query = `{
194203
productWithId (productId: "123" productTag: "blah") {
@@ -382,6 +391,7 @@ test('Get nested lists of resources', () => {
382391
})
383392
})
384393

394+
// Links can be defined with some parameters as constants or variables
385395
test('Link parameters as constants and variables', () => {
386396
const query = `{
387397
scanner(query: "hello") {
@@ -550,8 +560,10 @@ test('Get response with header parameters', () => {
550560
})
551561
})
552562

553-
// Content-type and accept headers should not change because they are linked
554-
// to GraphQL object types with static schemas
563+
/**
564+
* Content-type and accept headers should not change because they are
565+
* linked to GraphQL object types with static schemas
566+
*/
555567
test('Get JSON response even with non-JSON accept header', () => {
556568
const query = `{
557569
office (id: 2) {
@@ -585,6 +597,10 @@ test('Get response with cookies', () => {
585597
})
586598
})
587599

600+
/**
601+
* GraphQL (input) object type also consider the preferred name when generating
602+
* a name
603+
*/
588604
test('Ensure good naming for operations with duplicated schemas', () => {
589605
const query = `query {
590606
cleanDesks
@@ -600,7 +616,11 @@ test('Ensure good naming for operations with duplicated schemas', () => {
600616
})
601617
})
602618

603-
test('Get response containing 64 bit integer (using GraphQLFloat)', () => {
619+
/**
620+
* CASE: 64 bit int - return number instead of integer, leading to use of
621+
* GraphQLFloat, which can support 64 bits:
622+
*/
623+
test('Get response containing 64-bit integer (using GraphQLFloat)', () => {
604624
const query = `{
605625
productsReviews (id: "100") {
606626
timestamp
@@ -751,7 +771,9 @@ test('Post resource and get nested resource back', () => {
751771
})
752772

753773
test('Post resource with non-application/json content-type request and response bodies', () => {
754-
const query = `mutation{postPaper(textPlainInput: "happy")}`
774+
const query = `mutation {
775+
postPaper(textPlainInput: "happy")
776+
}`
755777
return graphql(createdSchema, query).then(result => {
756778
expect(result).toEqual({
757779
data: {
@@ -767,7 +789,7 @@ test(
767789
'received data is correctly sanitized',
768790
() => {
769791
const query = `{
770-
productWithId (productId: "this-path", productTag:"And a tag") {
792+
productWithId (productId: "this-path", productTag: "And a tag") {
771793
productId
772794
productTag
773795
}
@@ -1034,8 +1056,7 @@ test('Resolve circular allOf', () => {
10341056
}
10351057
}
10361058
}
1037-
}
1038-
`
1059+
}`
10391060
return graphql(createdSchema, query, null, {}).then(result => {
10401061
expect(
10411062
result.data['__type']['fields'].find(field => {
@@ -1050,6 +1071,7 @@ test('Resolve circular allOf', () => {
10501071
})
10511072
})
10521073

1074+
// Extensions provide more information about failed API calls
10531075
test('Error contains extension', () => {
10541076
const query = `query {
10551077
user(username: "abcdef") {
@@ -1067,7 +1089,7 @@ test('Error contains extension', () => {
10671089
path: '/users/{username}',
10681090
statusCode: 404,
10691091
responseBody: {
1070-
message: 'Wrong username.'
1092+
message: 'Wrong username'
10711093
}
10721094
})
10731095
})

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ function startServer(PORT) {
377377
res.send(Users[req.params.username])
378378
} else {
379379
res.status(404).send({
380-
message: 'Wrong username.'
380+
message: 'Wrong username'
381381
})
382382
}
383383
})
@@ -388,7 +388,7 @@ function startServer(PORT) {
388388
res.send(Cars[req.params.username])
389389
} else {
390390
res.status(404).send({
391-
message: 'Wrong username.'
391+
message: 'Wrong username'
392392
})
393393
}
394394
})
@@ -406,7 +406,7 @@ function startServer(PORT) {
406406
res.send(friends)
407407
} else {
408408
res.status(404).send({
409-
message: 'Wrong username.'
409+
message: 'Wrong username'
410410
})
411411
}
412412
})
@@ -421,7 +421,7 @@ function startServer(PORT) {
421421
!('hobbies' in user)
422422
) {
423423
res.status(400).send({
424-
message: 'wrong data'
424+
message: 'Wrong data'
425425
})
426426
} else {
427427
Users[user.name] = user
@@ -527,7 +527,7 @@ function startServer(PORT) {
527527
req.query['product-tag'] === 'undefined'
528528
) {
529529
res.status(400).send({
530-
message: 'wrong data'
530+
message: 'Wrong data'
531531
})
532532
} else {
533533
res
@@ -551,7 +551,7 @@ function startServer(PORT) {
551551
if (!contentType.includes('text/plain')) {
552552
res.status(400).send({
553553
message:
554-
"wrong content-type, expected 'text/plain' but received " +
554+
"Wrong content-type, expected 'text/plain' but received " +
555555
contentType
556556
})
557557
} else {
@@ -592,7 +592,7 @@ function startServer(PORT) {
592592
let project = req.body
593593
if (!('project-id' in project) || !('lead-id' in project)) {
594594
res.status(400).send({
595-
message: 'wrong data'
595+
message: 'Wrong data'
596596
})
597597
} else {
598598
res.status(201).send(project)
@@ -608,7 +608,7 @@ function startServer(PORT) {
608608
!('product-tag' in product)
609609
) {
610610
res.status(400).send({
611-
message: 'wrong data'
611+
message: 'Wrong data'
612612
})
613613
} else {
614614
res.status(201).send(product)
@@ -646,7 +646,7 @@ function startServer(PORT) {
646646
typeof req.get('exampleHeader') === 'undefined'
647647
) {
648648
res.status(400).send({
649-
message: 'wrong request'
649+
message: 'Wrong request'
650650
})
651651
} else {
652652
res
@@ -662,7 +662,7 @@ function startServer(PORT) {
662662
res.status(201).send('success')
663663
} else {
664664
res.status(400).send({
665-
message: "wrong data, try 'hello': 'world'"
665+
message: "Wrong data, try 'hello': 'world'"
666666
})
667667
}
668668
})
@@ -671,7 +671,7 @@ function startServer(PORT) {
671671
console.log(req.method, req.path, req.query, req.headers)
672672
if (req.get('authorization') !== 'Bearer abcdef') {
673673
res.status(401).send({
674-
message: 'missing authorization header'
674+
message: 'Missing authorization header'
675675
})
676676
} else {
677677
res.set('Content-Type', 'text/plain').send('A secure message.')
@@ -689,7 +689,7 @@ function startServer(PORT) {
689689
res.send(TrashCans[req.params.username])
690690
} else {
691691
res.status(404).send({
692-
message: 'Wrong username.'
692+
message: 'Wrong username'
693693
})
694694
}
695695
})
@@ -704,7 +704,7 @@ function startServer(PORT) {
704704
res.send(TrashCans[req.params.username])
705705
} else {
706706
res.status(404).send({
707-
message: 'Wrong username.'
707+
message: 'Wrong username'
708708
})
709709
}
710710
})

0 commit comments

Comments
 (0)