【问题标题】:How to modify a GraphQL variable in a contract during the Provider Verification? [Pact-JS]如何在提供者验证期间修改合约中的 GraphQL 变量? [契约-JS]
【发布时间】:2020-10-21 01:28:13
【问题描述】:

我有一个 Pact 文件,如下所示:

{
  "consumer": {
    "name": "UI"
  },
  "provider": {
    "name": "GraphQL API"
  },
  "interactions": [
    {
      "description": "Delete item request",
      "providerState": "Atleast one item exists",
      "request": {
        "method": "POST",
        "path": "/",
        "headers": {
          "content-type": "application/json"
        },
        "body": {
          "operationName": null,
          "query": "mutation ($itemId: ID!) { \n                  removeItem(id: $itemId) {    \n                    item {    \n                      id     \n                      __typename  \n                    }    \n                    __typename \n                  }\n                }",
         "variables": {
            "itemId": "item-c9b1f276-3748-4be3-915d-ec01ccbc2197"
          }
        },
        "matchingRules": {
          "$.body.query": {
            "match": "regex",
            "regex": "mutation\\s*\\(\\$itemId:\\s*ID!\\)\\s*\\{\\s*removeItem\\(id:\\s*\\$itemId\\)\\s*\\{\\s*plan\\s*\\{\\s*id\\s*__typename\\s*\\}\\s*__typename\\s*\\}\\s*\\}"
          },
          "$.body.variables.itemId": {
            "match": "type"
          }
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": {
          "data": {
            "removeItem": {
              "item": {
                "id": "item-c9b1f276-3748-4be3-915d-ec01ccbc2197",
                "__typename": "Item"
              },
              "__typename": "ItemPayload"
            }
          }
        },
        "matchingRules": {
          "$.body.data.removeItem.item.id": {
            "match": "type"
          }
        }
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "2.0.0"
    }
  }
}

在协议验证之前,我想提供一个有效的 itemId 值,它是一个 GraphQL 变量。

在上面的 JSON 中,它位于 interactions > request > body > variables > itemId

我尝试的一个选项是使用 requestFilters 选项 (Pact-JS-modify-requests-prior-to-verification-request-filters), 但是,我无法理解如何修改 GraphQL 变量?

或者如果有任何其他验证选项可以使用?

【问题讨论】:

    标签: javascript node.js graphql graphql-js pact


    【解决方案1】:

    requestFilter 功能使用原始 Express JS 中间件,并且不对传入的请求负载内容类型做出任何假设。因此,您将需要解析正文,但对您的请求有意义。例如,要提取 JSON 有效负载,您可以执行以下操作(在 GraphQL 合同上针对 Pact JS 存储库中的 e2e 提供程序进行了测试):

      requestFilter: (req, _res, next) => {
        if (req.path.match("/graphql")) {
          // Body is NOT automatically converted to JSON or any specific type
          // you need to extract the contents and parse
          let chunks = []
          req.on("data", function(chunk) {
            chunks.push(chunk)
          })
          req.on("end", function() {
            // Request upload complete, convert to a string and parse into JSON
            req.body = JSON.parse(Buffer.concat(chunks).toString())
    
            // req.body is now JSON, manipulate as needed
            console.log("Body as JSON:", req.body)
            next()
          })
        } else {
          next()
        }
      }
    

    【讨论】:

    • 谢谢马修!你有完整的工作代码吗?测试只是超时,因为它在收集“数据”块时尝试提取 req 对象的内容
    • 抱歉,我没有给你一个例子。您能否通过可重现的示例提出问题,我们可以使用该示例来改进/修复(问题模板应该指导您编写可以修改和共享的代码)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多