From 7ea5328ea43b0db6be945438bc506b8a4dfb2e91 Mon Sep 17 00:00:00 2001 From: Book Moons <35854232+bookmoons@users.noreply.github.com> Date: Sun, 28 Jul 2019 16:39:11 -0400 Subject: [PATCH 1/6] Remove unused ignores --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index ca1e0c84..6852dbe4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -bin/libs -bin/*.json npm-debug.log package-lock.json .bundle From f017603d795ef44b697e2bc016d5124e497ce352 Mon Sep 17 00:00:00 2001 From: Book Moons <35854232+bookmoons@users.noreply.github.com> Date: Sun, 28 Jul 2019 17:43:37 -0400 Subject: [PATCH 2/6] Factor graphql body test --- test/int/graphql.js | 42 +++++++++++++++++++++++++++++ test/material/2.1/graphql-body.json | 33 +++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 test/int/graphql.js create mode 100644 test/material/2.1/graphql-body.json diff --git a/test/int/graphql.js b/test/int/graphql.js new file mode 100644 index 00000000..879960f5 --- /dev/null +++ b/test/int/graphql.js @@ -0,0 +1,42 @@ +import test from 'ava' +import convertFile from 'convert/file' + +function render (body) { + const content = JSON.stringify(body) + const escaped = content.replace(/\\/g, '\\\\') + return `'${escaped}'` +} + +test('body', async t => { + const [ main ] = await convertFile('test/material/2.1/graphql-body.json') + const query = '' + +`{ + user { + name + } +}` + const body = { + query, + variables: '' + } + t.is(main, `// Auto-generated by the Load Impact converter + +import "./libs/shim/core.js"; + +export let options = { maxRedirects: 4 }; + +const Request = Symbol.for("request"); +postman[Symbol.for("initial")]({ + options +}); + +export default function() { + postman[Request]({ + name: "Test Request", + method: "POST", + address: "http://graphql.example.com/", + data: ${render(body)} + }); +} +`) +}) diff --git a/test/material/2.1/graphql-body.json b/test/material/2.1/graphql-body.json new file mode 100644 index 00000000..d95426e6 --- /dev/null +++ b/test/material/2.1/graphql-body.json @@ -0,0 +1,33 @@ +{ + "info": { + "_postman_id": "a3033a78-1a2e-4036-9621-11bc4fdc3ad6", + "name": "GraphQL", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "Test Request", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "{\n user {\n name\n }\n}", + "variables": "" + } + }, + "url": { + "raw": "http://graphql.example.com", + "protocol": "http", + "host": [ + "graphql", + "example", + "com" + ] + } + }, + "response": [] + } + ] +} From 69a5a7b140996040214c37bc27f1aae65441c004 Mon Sep 17 00:00:00 2001 From: Book Moons <35854232+bookmoons@users.noreply.github.com> Date: Sun, 28 Jul 2019 17:51:00 -0400 Subject: [PATCH 3/6] Remove basic -> graphql test Moved to devoted graphql section. --- test/int/basic.js | 62 ----------------- test/material/2.1/graphql.json | 122 --------------------------------- 2 files changed, 184 deletions(-) delete mode 100644 test/material/2.1/graphql.json diff --git a/test/int/basic.js b/test/int/basic.js index d0de513a..b31bd5d4 100644 --- a/test/int/basic.js +++ b/test/int/basic.js @@ -153,65 +153,3 @@ export default function() { } `) }) - -test('graphql', async t => { - const [ main ] = await convertFile('test/material/2.1/graphql.json') - - t.is(main, `// Auto-generated by the Load Impact converter - -import "./libs/shim/core.js"; -import "./libs/shim/expect.js"; -import { group } from "k6"; - -export let options = { maxRedirects: 4 }; - -const Request = Symbol.for("request"); -postman[Symbol.for("initial")]({ - options -}); - -export default function() { - group("graphql", function() { - postman[Request]({ - name: "GET /graphql", - method: "GET", - address: - "http://test.com/graphql?query=exampleQuery&operationName=ExampleGraphQLQuery&variables={}", - headers: { - "Content-Type": "application/json" - }, - post(response) { - pm.test("Status code is 200", function() { - pm.response.to.have.status(200); - }); - - pm.test("Response time is less than 900ms", function() { - pm.expect(pm.response.responseTime).to.be.below(900); - }); - } - }); - - postman[Request]({ - name: "POST /graphql", - method: "POST", - address: "http://test.com/graphql/", - data: - '{"query":"testQuery","variables":"{\\\\n \\\\"name\\\\": \\\\"example\\\\",\\\\n \\\\"example\\\\": \\\\"test\\\\"\\\\n}"}', - headers: { - "Content-Type": "application/json" - }, - post(response) { - pm.test("Status code is 200", function() { - pm.response.to.have.status(200); - }); - - pm.test("Response time is less than 900ms", function() { - pm.expect(pm.response.responseTime).to.be.below(900); - }); - } - }); - }); -} -`) -}) - diff --git a/test/material/2.1/graphql.json b/test/material/2.1/graphql.json deleted file mode 100644 index 2c576997..00000000 --- a/test/material/2.1/graphql.json +++ /dev/null @@ -1,122 +0,0 @@ -{ - "info": { - "_postman_id": "00059caa-aed5-49f3-bed9-241d1ab3618e", - "name": "GraphQL", - "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" - }, - "item": [ - { - "name": "graphql", - "item": [ - { - "name": "GET /graphql", - "event": [ - { - "listen": "test", - "script": { - "id": "57cafcd3-bf6e-4e4a-82c4-0214b0752715", - "exec": [ - "pm.test(\"Status code is 200\", function () {", - " pm.response.to.have.status(200);", - "});", - "", - "pm.test(\"Response time is less than 900ms\", function () {", - " pm.expect(pm.response.responseTime).to.be.below(900);", - "});" - ], - "type": "text/javascript" - } - } - ], - "request": { - "method": "GET", - "header": [ - { - "key": "Content-Type", - "name": "Content-Type", - "value": "application/json" - } - ], - "url": { - "raw": "http://test.com/graphql?query=exampleQuery&operationName=ExampleGraphQLQuery&variables={}", - "protocol": "http", - "host": [ - "test", - "com" - ], - "path": [ - "graphql" - ], - "query": [ - { - "key": "query", - "value": "exampleQuery" - }, - { - "key": "operationName", - "value": "ExampleGraphQLQuery" - }, - { - "key": "variables", - "value": "{}" - } - ] - } - }, - "response": [] - }, - { - "name": "POST /graphql", - "event": [ - { - "listen": "test", - "script": { - "id": "155c6cce-a42c-4aaf-ac89-e4962d87d86e", - "exec": [ - "pm.test(\"Status code is 200\", function () {", - " pm.response.to.have.status(200);", - "});", - "", - "pm.test(\"Response time is less than 900ms\", function () {", - " pm.expect(pm.response.responseTime).to.be.below(900);", - "});" - ], - "type": "text/javascript" - } - } - ], - "request": { - "method": "POST", - "header": [ - { - "key": "Content-Type", - "name": "Content-Type", - "value": "application/json" - } - ], - "body": { - "mode": "graphql", - "graphql": { - "query": "testQuery", - "variables": "{\n \"name\": \"example\",\n \"example\": \"test\"\n}" - } - }, - "url": { - "raw": "http://test.com/graphql/", - "protocol": "http", - "host": [ - "test", - "com" - ], - "path": [ - "graphql", - "" - ] - } - }, - "response": [] - } - ] - } - ] -} \ No newline at end of file From 3e400fee17a9d2883c8914bc8d65c957b15f5e44 Mon Sep 17 00:00:00 2001 From: Book Moons <35854232+bookmoons@users.noreply.github.com> Date: Sun, 28 Jul 2019 18:16:33 -0400 Subject: [PATCH 4/6] graphql: Elide optional fields when possible --- lib/generate/Request/analyze.js | 8 ++++++-- test/int/graphql.js | 5 +---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/generate/Request/analyze.js b/lib/generate/Request/analyze.js index 8471f6d6..e1de17c5 100644 --- a/lib/generate/Request/analyze.js +++ b/lib/generate/Request/analyze.js @@ -87,9 +87,13 @@ function dataUrl (body, feature) { feature.data = data } function dataGraphQL (body, feature) { - if (body.graphql) { - feature.data = JSON.stringify(body.graphql) + const data = { + query: body.graphql.query } + if (body.graphql.variables) { + data.variables = body.variable + } + feature.data = JSON.stringify(data) } function headers (headers, feature) { diff --git a/test/int/graphql.js b/test/int/graphql.js index 879960f5..b0de57df 100644 --- a/test/int/graphql.js +++ b/test/int/graphql.js @@ -15,10 +15,7 @@ test('body', async t => { name } }` - const body = { - query, - variables: '' - } + const body = { query } t.is(main, `// Auto-generated by the Load Impact converter import "./libs/shim/core.js"; From f33b3b2a9163600681c1e3e9f76dad9bbd0ead60 Mon Sep 17 00:00:00 2001 From: Book Moons <35854232+bookmoons@users.noreply.github.com> Date: Sun, 28 Jul 2019 18:33:40 -0400 Subject: [PATCH 5/6] Support GraphQL variables --- lib/generate/Request/analyze.js | 2 +- test/int/graphql.js | 47 ++++++++++++++++++- ...hql-body.json => graphql-body-simple.json} | 0 test/material/2.1/graphql-body-var.json | 41 ++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) rename test/material/2.1/{graphql-body.json => graphql-body-simple.json} (100%) create mode 100644 test/material/2.1/graphql-body-var.json diff --git a/lib/generate/Request/analyze.js b/lib/generate/Request/analyze.js index e1de17c5..2b4de6a4 100644 --- a/lib/generate/Request/analyze.js +++ b/lib/generate/Request/analyze.js @@ -91,7 +91,7 @@ function dataGraphQL (body, feature) { query: body.graphql.query } if (body.graphql.variables) { - data.variables = body.variable + data.variables = body.graphql.variables } feature.data = JSON.stringify(data) } diff --git a/test/int/graphql.js b/test/int/graphql.js index b0de57df..32f7f280 100644 --- a/test/int/graphql.js +++ b/test/int/graphql.js @@ -7,8 +7,10 @@ function render (body) { return `'${escaped}'` } -test('body', async t => { - const [ main ] = await convertFile('test/material/2.1/graphql-body.json') +test('body simple', async t => { + const [ main ] = await convertFile( + 'test/material/2.1/graphql-body-simple.json' + ) const query = '' + `{ user { @@ -37,3 +39,44 @@ export default function() { } `) }) + +test('body var', async t => { + const [ main ] = await convertFile( + 'test/material/2.1/graphql-body-var.json' + ) + const query = '' + +`query UserName($id: UserId) { + user(id: $id) { + name + } +}` + const variables = '' + +`{ +\t"id": "{{userId}}" +}` + const body = { query, variables } + t.is(main, `// Auto-generated by the Load Impact converter + +import "./libs/shim/core.js"; + +export let options = { maxRedirects: 4 }; + +const Request = Symbol.for("request"); +postman[Symbol.for("initial")]({ + options, + collection: { + userId: "5" + } +}); + +export default function() { + postman[Request]({ + name: "Test Request", + method: "POST", + address: "http://graphql.example.com/", + data: + ${render(body)} + }); +} +`) +}) diff --git a/test/material/2.1/graphql-body.json b/test/material/2.1/graphql-body-simple.json similarity index 100% rename from test/material/2.1/graphql-body.json rename to test/material/2.1/graphql-body-simple.json diff --git a/test/material/2.1/graphql-body-var.json b/test/material/2.1/graphql-body-var.json new file mode 100644 index 00000000..a87624c9 --- /dev/null +++ b/test/material/2.1/graphql-body-var.json @@ -0,0 +1,41 @@ +{ + "info": { + "_postman_id": "a3033a78-1a2e-4036-9621-11bc4fdc3ad6", + "name": "GraphQL", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "Test Request", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "query UserName($id: UserId) {\n user(id: $id) {\n name\n }\n}", + "variables": "{\n\t\"id\": \"{{userId}}\"\n}" + } + }, + "url": { + "raw": "http://graphql.example.com", + "protocol": "http", + "host": [ + "graphql", + "example", + "com" + ] + } + }, + "response": [] + } + ], + "variable": [ + { + "id": "c910e57e-8c50-4ce0-aa13-6ab64f448421", + "key": "userId", + "value": "5", + "type": "string" + } + ] +} From 021eb001a9d2daf142fc0a7de9dc700dd9f8a93a Mon Sep 17 00:00:00 2001 From: Book Moons <35854232+bookmoons@users.noreply.github.com> Date: Sun, 28 Jul 2019 18:58:37 -0400 Subject: [PATCH 6/6] Add live GraphQL example --- example/v2/github-graphql.json | 72 ++++++++++++++++++++ example/v2/{github.json => github-rest.json} | 0 2 files changed, 72 insertions(+) create mode 100644 example/v2/github-graphql.json rename example/v2/{github.json => github-rest.json} (100%) diff --git a/example/v2/github-graphql.json b/example/v2/github-graphql.json new file mode 100644 index 00000000..b46e0831 --- /dev/null +++ b/example/v2/github-graphql.json @@ -0,0 +1,72 @@ +{ + "info": { + "_postman_id": "068355d1-ef5d-446c-9cf3-97823da6a196", + "name": "GitHub GraphQL", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "Query", + "event": [ + { + "listen": "test", + "script": { + "id": "186b3734-3fe3-4495-b4c9-44b0245dbe3c", + "exec": [ + "console.log(pm.response.text())" + ], + "type": "text/javascript" + } + } + ], + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{GITHUB_TOKEN}}", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "{\n viewer {\n name\n repositories(last: {{numberOfRepos}}) {\n nodes {\n name\n }\n }\n }\n}\n", + "variables": "" + } + }, + "url": { + "raw": "https://api.github.com/graphql", + "protocol": "https", + "host": [ + "api", + "github", + "com" + ], + "path": [ + "graphql" + ] + } + }, + "response": [] + } + ], + "variable": [ + { + "id": "b414bfbd-b39b-460a-bbe4-3e768b3784a0", + "key": "numberOfRepos", + "value": "3", + "type": "string" + }, + { + "id": "9dff4a0c-28c0-44ff-aebf-1439fd9298a5", + "key": "GITHUB_TOKEN", + "value": "", + "type": "string" + } + ] +} diff --git a/example/v2/github.json b/example/v2/github-rest.json similarity index 100% rename from example/v2/github.json rename to example/v2/github-rest.json