将片段从GraphQL文件导入到另一个GraphQL文件不起作用 [英] Import Fragment from GraphQL File to Another GraphQL File Does Not Work

查看:42
本文介绍了将片段从GraphQL文件导入到另一个GraphQL文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make test on Karate by using .graphql file and passing variables. On my graphql schema, I am trying to reuse fragment from another .graphql file. I tried following the explanation on https://www.apollographql.com/docs/react/advanced/fragments#webpack-importing-fragments but when I run the Karate test with #import statement on .graphql file, the test failed saying the fragment is unknown.

FindProfile.feature

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

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

Media.graphql

    fragment Media on Media {
    id
    title
    description
  }

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

I expect that I can reuse the fragment from another .graphql file, but the actual I cannot do that. Any help is greatly appreciated. Thank you.

推荐答案

对于空手道GraphQL只是作为纯文本或原始字符串处理.不支持GraphQL导入.

To Karate GraphQL is just processed as plain text or raw strings. There is no support for GraphQL imports.

团队通常要做的是在客户端向服务器发出HTTP POST请求时获得有效的GraphQL样本.它甚至可以包含片段,就像您在这些示例中看到的一样.

What teams typically do is get a sample of valid GraphQL at the time when the client makes the HTTP POST request to the server. This can even contain fragments like you see in these examples.

然后,您所有的测试需要做的就是重播该内容.空手道为您提供了许多字符串处理功能,例如 replace (如果您需要数据-驱动测试.

Then all your test needs to do is re-play that. What Karate gives you is plenty of string manipulation capabilities such as replace if you need data-driven testing.

总而言之,请使用完整的GraphQL字符串.如果需要修改它们,请使用replace或使用JS或Java实用程序的自定义字符串操作.在大多数情况下,GraphQL 变量足以进行数据驱动的测试.

To summarize, work with fully formed GraphQL strings. If you need to modify them, use replace or custom string manipulation using JS or a Java utility. In most cases GraphQL variables are sufficient for data-driven tests.

这篇关于将片段从GraphQL文件导入到另一个GraphQL文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆