Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #40 from bookmoons/bookmoons/graphql-var
Browse files Browse the repository at this point in the history
Support GraphQL variables
  • Loading branch information
robingustafsson committed Aug 23, 2019
2 parents 00569e1 + 021eb00 commit 7be3670
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 188 deletions.
2 changes: 0 additions & 2 deletions .gitignore
@@ -1,5 +1,3 @@
bin/libs
bin/*.json
npm-debug.log
package-lock.json
.bundle
Expand Down
72 changes: 72 additions & 0 deletions 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"
}
]
}
File renamed without changes.
8 changes: 6 additions & 2 deletions lib/generate/Request/analyze.js
Expand Up @@ -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.graphql.variables
}
feature.data = JSON.stringify(data)
}

function headers (headers, feature) {
Expand Down
62 changes: 0 additions & 62 deletions test/int/basic.js
Expand Up @@ -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);
});
}
});
});
}
`)
})

82 changes: 82 additions & 0 deletions test/int/graphql.js
@@ -0,0 +1,82 @@
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 simple', async t => {
const [ main ] = await convertFile(
'test/material/2.1/graphql-body-simple.json'
)
const query = '' +
`{
user {
name
}
}`
const body = { query }
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)}
});
}
`)
})

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)}
});
}
`)
})
33 changes: 33 additions & 0 deletions test/material/2.1/graphql-body-simple.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": []
}
]
}
41 changes: 41 additions & 0 deletions 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"
}
]
}

0 comments on commit 7be3670

Please sign in to comment.