简体   繁体   中英

How to write a GraphQL mutation query for xunit integration test

I want to perform an integration test on my GraphQL mutation. I can run the query in my GraphiQL fine but don't really know how to translate this into the query string with the input for my xunit integration test.

在此处输入图片说明

Above is my GraphiQL that return me data fine. I have followed StarWars git hub project for the integration tests. Querying GraphQL working fine by passing the graphql query string but because of the lack of knowledge and documentation, I am unable to convert above GraphiQL mutation into the query string.

Following is my integration test code so far where the query variables part is missing and don't know how to bring them in

        //Arrange
        const string query = @"{ 
            ""query"": "" mutation CreateMutation($input: InputType!) {
                          addNewItem(myInput: $input) {
                            col1
                            col2
                            col3
                            col4
                            col5
                    }
                } ""
        }";
        var content = new StringContent(query, Encoding.UTF8, "application/json");

        // Act
        var response = await client.PostAsync("/graphql", content);

        //Assert
        response.EnsureSuccessStatusCode();

I have figured out the query string syntax and it will be as follow

const string query = @"{ 
            ""query"": "" mutation CreateMutation($input: InputType!) { addNewItem(myInput: $input) { col1 col2 col3 col4 col5  }} "",
            ""variables"": { input :{ col1: 100, col2: 'starwars' }}
        }";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM