{
  "version": 13,
  "name": "17_graphql",
  "description": "Example: GraphQL requests (bodyMode \"graphql\"). Covers a plain query, variables (with $vars substituted inside them), a document with several operations picked by operationName, extracting a value and chaining it into the next query, a mutation, introspection, and a negative test on GraphQL errors.\nAPIs: countries.trevorblades.com (read-only) and graphqlzero.almansi.me (fake mutations).",
  "globals": [
    {
      "name": "$countriesApi",
      "value": "https://countries.trevorblades.com/"
    },
    {
      "name": "$zeroApi",
      "value": "https://graphqlzero.almansi.me/api"
    },
    {
      "name": "$countryCode",
      "value": "RO"
    },
    {
      "name": "$continentCode",
      "value": ""
    },
    {
      "name": "$postTitle",
      "value": "Written by CLEM"
    }
  ],
  "globalFunctions": "// Assertion helpers, built on check(label, pass, details).\n// Each records a pass / fail line in the log and the report.\n// A failed one fails its step when the step is done;\n// the code keeps running. Change them or add your own.\n// r defaults to the last response.\n\nfunction assertTrue(value, label = \"is true\") {\n  return check(label, Boolean(value), { actual: value, expected: true });\n}\n\nfunction assertEqual(actual, expected, label = \"equals \" + JSON.stringify(expected)) {\n  return check(label, isEqual(actual, expected), { actual, expected });\n}\n\nfunction assertNotEqual(actual, unexpected, label = \"does not equal \" + JSON.stringify(unexpected)) {\n  return check(label, !isEqual(actual, unexpected), { actual, expected: \"not \" + JSON.stringify(unexpected) });\n}\n\n// text contains a part, or a list contains an item\nfunction assertContains(haystack, needle, label = \"contains \" + JSON.stringify(needle)) {\n  const pass = Array.isArray(haystack) ? haystack.some((x) => isEqual(x, needle)) : String(haystack).includes(needle);\n  return check(label, pass, { actual: haystack, expected: needle });\n}\n\nfunction assertMatch(text, regex, label = \"matches \" + regex) {\n  return check(label, new RegExp(regex).test(String(text)), { actual: text, expected: String(regex) });\n}\n\nfunction assertLessThan(actual, limit, label = \"< \" + limit) {\n  return check(label, Number(actual) < Number(limit), { actual, expected: \"< \" + limit });\n}\n\nfunction assertGreaterThan(actual, limit, label = \"> \" + limit) {\n  return check(label, Number(actual) > Number(limit), { actual, expected: \"> \" + limit });\n}\n\n// expected: 200, \"2xx\", or a list such as [200, 201]\nfunction assertStatus(expected, r = response) {\n  const list = [].concat(expected).map(String);\n  const status = String(r && r.status);\n  const pass = list.some((e) => e === status || (/^[1-5]xx$/i.test(e) && e[0] === status[0]));\n  return check(\"status is \" + list.join(\" or \"), pass, { actual: r && r.status, expected });\n}\n\nfunction assertTimeBelow(ms, r = response) {\n  return check(\"time < \" + ms + \" ms\", Boolean(r) && r.timeMs < ms, { actual: r && r.timeMs, expected: \"< \" + ms });\n}\n\n// without expected, the header only has to exist\nfunction assertHeader(name, expected, r = response) {\n  const value = r ? r.headers[String(name).toLowerCase()] : undefined;\n  if (expected === undefined) return check(\"header \" + name + \" exists\", value !== undefined, { actual: value });\n  return check(\"header \" + name + \" equals \" + expected, String(value) === String(expected), { actual: value, expected });\n}\n\n// e.g. assertJsonPath(\"$.items[0].id\", 42); without expected, the value only has to exist\nfunction assertJsonPath(path, expected, r = response) {\n  const value = jsonPath(path, r);\n  if (expected === undefined) return check(path + \" exists\", value !== undefined, { actual: value });\n  return check(path + \" equals \" + JSON.stringify(expected), isEqual(value, expected), { actual: value, expected });\n}\n\n// schema: a JSON Schema object\nfunction assertSchema(schema, r = response) {\n  const result = validateSchema(r, schema);\n  return check(\"matches the JSON Schema\", result.valid, { error: result.errors || undefined });\n}\n",
  "libraries": [],
  "activeEnvironment": "",
  "auth": {
    "type": "none"
  },
  "settings": {},
  "data": {
    "file": ""
  },
  "cookies": [],
  "tags": [
    "example",
    "graphql"
  ],
  "steps": [
    {
      "bodyMode": "graphql",
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "notExists",
          "path": "$.errors",
          "target": "json",
          "value": ""
        },
        {
          "op": "equals",
          "path": "$.data.country.name",
          "target": "json",
          "value": "Romania"
        },
        {
          "op": "equals",
          "path": "$.data.country.capital",
          "target": "json",
          "value": "Bucharest"
        }
      ],
      "graphql": {
        "operationName": "",
        "query": "{\n  country(code: \"RO\") {\n    name\n    capital\n    currency\n  }\n}",
        "variables": ""
      },
      "id": "simple-query",
      "method": "POST",
      "name": "Simple query",
      "type": "request",
      "url": "$countriesApi"
    },
    {
      "bodyMode": "graphql",
      "checks": [
        {
          "op": "equals",
          "path": "$.data.country.continent.name",
          "target": "json",
          "value": "Europe"
        },
        {
          "op": "equals",
          "path": "$.data.country.languages[0].code",
          "target": "json",
          "value": "ro"
        },
        {
          "op": "type",
          "path": "$.data.country.languages",
          "target": "json",
          "value": "array"
        }
      ],
      "extract": [
        {
          "after": "\"",
          "before": "\"continent\":{\"code\":\"",
          "from": "Body",
          "name": "$continentCode",
          "ordinal": 1
        }
      ],
      "graphql": {
        "operationName": "CountryByCode",
        "query": "query CountryByCode($code: ID!) {\n  country(code: $code) {\n    name\n    continent { code name }\n    languages { code name }\n  }\n}",
        "variables": "{ \"code\": \"$countryCode\" }"
      },
      "id": "with-variables",
      "method": "POST",
      "name": "Query with variables ($countryCode)",
      "type": "request",
      "url": "$countriesApi"
    },
    {
      "bodyMode": "graphql",
      "checks": [
        {
          "op": "equals",
          "path": "$.data.continent.name",
          "target": "json",
          "value": "Europe"
        },
        {
          "op": "type",
          "path": "$.data.continent.countries",
          "target": "json",
          "value": "array"
        },
        {
          "op": "exists",
          "path": "$.data.continent.countries[40].code",
          "target": "json",
          "value": ""
        },
        {
          "op": "contains",
          "target": "body",
          "value": "\"code\":\"RO\""
        }
      ],
      "graphql": {
        "operationName": "",
        "query": "query Continent($code: ID!) {\n  continent(code: $code) {\n    name\n    countries { code name }\n  }\n}",
        "variables": "{ \"code\": \"$continentCode\" }"
      },
      "id": "chained",
      "method": "POST",
      "name": "Chain: countries of the extracted continent",
      "type": "request",
      "url": "$countriesApi"
    },
    {
      "bodyMode": "graphql",
      "checks": [
        {
          "op": "equals",
          "path": "$.data.country.currency",
          "target": "json",
          "value": "RON"
        },
        {
          "op": "notExists",
          "path": "$.data.languages",
          "target": "json",
          "value": ""
        }
      ],
      "graphql": {
        "operationName": "Currency",
        "query": "query Languages {\n  languages { code }\n}\n\nquery Currency($code: ID!) {\n  country(code: $code) { currency }\n}",
        "variables": "{ \"code\": \"$countryCode\" }"
      },
      "id": "operation-name",
      "method": "POST",
      "name": "Pick one of several operations (operationName)",
      "type": "request",
      "url": "$countriesApi"
    },
    {
      "bodyMode": "graphql",
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "equals",
          "path": "$.data.createPost.title",
          "target": "json",
          "value": "Written by CLEM"
        },
        {
          "op": "exists",
          "path": "$.data.createPost.id",
          "target": "json",
          "value": ""
        }
      ],
      "extract": [
        {
          "after": "\"",
          "before": "\"id\":\"",
          "from": "Body",
          "name": "$postId",
          "ordinal": 1
        }
      ],
      "graphql": {
        "operationName": "CreatePost",
        "query": "mutation CreatePost($input: CreatePostInput!) {\n  createPost(input: $input) {\n    id\n    title\n    body\n  }\n}",
        "variables": "{ \"input\": { \"title\": \"$postTitle\", \"body\": \"GraphQL mutation from the Example project\" } }"
      },
      "id": "mutation",
      "method": "POST",
      "name": "Mutation: create a post",
      "type": "request",
      "url": "$zeroApi"
    },
    {
      "bodyMode": "graphql",
      "checks": [
        {
          "op": "equals",
          "path": "$.data.__type.kind",
          "target": "json",
          "value": "OBJECT"
        },
        {
          "op": "contains",
          "target": "body",
          "value": "\"name\":\"capital\""
        }
      ],
      "graphql": {
        "operationName": "",
        "query": "{\n  __type(name: \"Country\") {\n    name\n    kind\n    fields { name }\n  }\n}",
        "variables": ""
      },
      "id": "introspection",
      "method": "POST",
      "name": "Introspection: the schema has a Country type",
      "type": "request",
      "url": "$countriesApi"
    },
    {
      "bodyMode": "graphql",
      "checks": [
        {
          "op": "exists",
          "path": "$.errors",
          "target": "json",
          "value": ""
        },
        {
          "op": "contains",
          "path": "$.errors[0].message",
          "target": "json",
          "value": "population"
        },
        {
          "op": "notExists",
          "path": "$.data",
          "target": "json",
          "value": ""
        }
      ],
      "graphql": {
        "operationName": "",
        "query": "{\n  country(code: \"RO\") { name population }\n}",
        "variables": ""
      },
      "id": "graphql-error",
      "method": "POST",
      "name": "Negative: unknown field returns a GraphQL error",
      "type": "request",
      "url": "$countriesApi"
    }
  ]
}
