3

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

@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
  }

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.

Raymond
  • 604
  • 1
  • 9
  • 27

1 Answers1

2

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

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.

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.

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.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks for the reply. In my case there will be fragments called in another fragments.. Was thinking maybe there is another way in Karate to get around this (maybe to let the fragment call another fragment from inside the gql files).. I guess from your explanation we have to copy all the fragment strings to the query? – Raymond May 27 '19 at 13:34