【问题标题】:Import Fragment from GraphQL File to Another GraphQL File Does Not Work将 GraphQL 文件中的片段导入另一个 GraphQL 文件不起作用
【发布时间】:2019-10-09 22:09:32
【问题描述】:

我正在尝试使用 .graphql 文件并传递变量来测试空手道。在我的 graphql 模式中,我试图重用另一个 .graphql 文件中的片段。我尝试按照https://www.apollographql.com/docs/react/advanced/fragments#webpack-importing-fragments 上的说明进行操作,但是当我在 .graphql 文件上使用 #import 语句运行空手道测试时,测试失败,提示片段未知。

FindProfile.feature

@smoke
Feature: Test GraphQL FindTrendingProfiles

    Background:
        * url 'https://192.168.0.0.1'

    Scenario: FindTrendingProfiles Request
        Given def query = read('FindProfile.graphql')
        And def variables = { cursor: "1", cursorType: PAGE, size: 2 }
        And request { query: '#(query)', variables: '#(variables)' }
        When method post
        Then status 200

FindProfile.graphql

#import "./Media.graphql"

query FindProfile($cursor: String, $cursorType: CursorType!, $size: Int!) {
  FindProfile(cursor: $cursor, cursorType: $cursorType, size: $size) {
   edges{
        id
       profilePictureMedia{
               ...Media
             }
  }
}
}

Media.graphql

    fragment Media on Media {
    id
    title
    description
  }

我希望我可以重用另一个 .graphql 文件中的片段,但实际上我不能这样做。任何帮助是极大的赞赏。谢谢。

【问题讨论】:

    标签: graphql karate


    【解决方案1】:

    To Karate GraphQL 只是作为纯文本或原始字符串处理。不支持 GraphQL 导入。

    团队通常会在客户端向服务器发出 HTTP POST 请求时获取有效 GraphQL 的样本。这甚至可以包含您在these examples 中看到的片段。

    那么您需要做的所有测试就是重新播放。如果您需要数据驱动的测试,Karate 为您提供了大量的字符串操作功能,例如 replace

    总而言之,使用完全形成的 GraphQL 字符串。如果您需要修改它们,请使用 replace 或使用 JS 或 Java 实用程序的自定义字符串操作。在大多数情况下,GraphQL variables 足以用于数据驱动的测试。

    【讨论】:

    • 感谢您的回复。在我的情况下,将在另一个片段中调用片段..在想也许空手道有另一种方法来解决这个问题(也许让片段从 gql 文件中调用另一个片段)..我猜从你的解释我们有将所有片段字符串复制到查询中?
    • @Raymond 查看文档中的“读取文件”:github.com/intuit/karate#reading-files - 如果您阅读文档,则有多种使用实用程序函数(js / java)的方法。并请将此答案标记为“已接受”
    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 2019-03-26
    • 1970-01-01
    • 2018-09-29
    • 2023-01-19
    • 1970-01-01
    • 2020-04-22
    • 2021-01-11
    相关资源
    最近更新 更多