Class GraphQLTestTemplate

java.lang.Object
com.graphql.spring.boot.test.GraphQLTestTemplate

public class GraphQLTestTemplate extends Object
Helper class to test GraphQL queries and mutations.
  • Constructor Details

    • GraphQLTestTemplate

      public GraphQLTestTemplate(org.springframework.core.io.ResourceLoader resourceLoader, org.springframework.boot.test.web.client.TestRestTemplate restTemplate, @Value("${graphql.servlet.mapping:/graphql}") String graphqlMapping, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
  • Method Details

    • withAdditionalHeader

      public GraphQLTestTemplate withAdditionalHeader(String name, String... value)
      Add an HTTP header that will be sent with each request this sends.
      Parameters:
      name - Name (key) of HTTP header to add.
      value - Value(s) of HTTP header to add.
      Returns:
      self
    • withAdditionalHeaders

      public GraphQLTestTemplate withAdditionalHeaders(org.springframework.util.MultiValueMap<String,String> additionalHeaders)
      Add multiple HTTP header that will be sent with each request this sends.
      Parameters:
      additionalHeaders - additional headers to add
      Returns:
      self
    • withBearerAuth

      public GraphQLTestTemplate withBearerAuth(@NonNull @NonNull String token)
      Adds a bearer token to the authorization header.
      Parameters:
      token - the bearer token
      Returns:
      self
    • withBasicAuth

      public GraphQLTestTemplate withBasicAuth(@NonNull @NonNull String username, @NonNull @NonNull String password, @Nullable Charset charset)
      Adds basic authentication to the authorization header.
      Parameters:
      username - the username
      password - the password
      charset - the charset used by the credentials
      Returns:
      self
    • withBasicAuth

      public GraphQLTestTemplate withBasicAuth(@NonNull @NonNull String username, @NonNull @NonNull String password)
      Adds basic authentication to the authorization header.
      Parameters:
      username - the username
      password - the password
      Returns:
      self
    • withBasicAuth

      public GraphQLTestTemplate withBasicAuth(@NonNull @NonNull String encodedCredentials)
      Adds basic authentication to the authorization header.
      Parameters:
      encodedCredentials - the encoded credentials
      Returns:
      self
    • withHeaders

      public GraphQLTestTemplate withHeaders(org.springframework.http.HttpHeaders newHeaders)
      Replace any associated HTTP headers with the provided headers.
      Parameters:
      newHeaders - Headers to use.
      Returns:
      self
    • withClearHeaders

      public GraphQLTestTemplate withClearHeaders()
      Clear all associated HTTP headers.
      Returns:
      self
    • perform

      public GraphQLResponse perform(String graphqlResource, com.fasterxml.jackson.databind.node.ObjectNode variables) throws IOException
      Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.
      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      variables - the input variables for the GraphQL query
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • perform

      public GraphQLResponse perform(String graphqlResource, String operationName) throws IOException
      Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.
      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      operationName - the name of the GraphQL operation to be executed
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • perform

      public GraphQLResponse perform(String graphqlResource, String operation, com.fasterxml.jackson.databind.node.ObjectNode variables) throws IOException
      Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.
      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      operation - the name of the GraphQL operation to be executed
      variables - the input variables for the GraphQL query
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • perform

      public GraphQLResponse perform(String graphqlResource, com.fasterxml.jackson.databind.node.ObjectNode variables, List<String> fragmentResources) throws IOException
      Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.
      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      variables - the input variables for the GraphQL query
      fragmentResources - an ordered list of classpath resources containing GraphQL fragment definitions.
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • perform

      public GraphQLResponse perform(String graphqlResource, String operationName, com.fasterxml.jackson.databind.node.ObjectNode variables, List<String> fragmentResources) throws IOException
      Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.
      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      variables - the input variables for the GraphQL query
      fragmentResources - an ordered list of classpath resources containing GraphQL fragment definitions.
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • postForResource

      public GraphQLResponse postForResource(String graphqlResource) throws IOException
      Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.
      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • postForResource

      public GraphQLResponse postForResource(String graphqlResource, List<String> fragmentResources) throws IOException
      Loads a GraphQL query or mutation from the given classpath resource, appending any graphql fragment resources provided and sends it to the GraphQL server.
      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      fragmentResources - an ordered list of classpath resources containing GraphQL fragment definitions.
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • postMultipart

      public GraphQLResponse postMultipart(String query, String variables)
    • postFiles

      public GraphQLResponse postFiles(String graphqlResource, com.fasterxml.jackson.databind.node.ObjectNode variables, List<org.springframework.core.io.ClassPathResource> files) throws IOException
      Handle the multipart files upload request to GraphQL servlet

      In contrast with usual the GraphQL request with body as json payload (consist of query, operationName and variables), multipart file upload request will use multipart/form-data body with the following structure:

      • operations the payload that we used to use for the normal GraphQL request
      • map a map for referencing between one part of multi-part request and the corresponding Upload element inside variables
      • a consequence of upload files embedded into the multi-part request, keyed as numeric number starting from 1, valued as File payload of usual multipart file upload

      Example uploading two files:

      * Please note that we can't embed binary data into json. Clients library supporting graphql file upload will set variable.files to null for every element inside the array, but each file will be a part of multipart request. GraphQL Servlet will use map part to walk through variables.files and validate the request in combination with other binary file parts

      ----------------------------dummyid

      Content-Disposition: form-data; name="operations"

      { "query": "mutation($files:[Upload]!) {uploadFiles(files:$files)}", "operationName": "uploadFiles", "variables": { "files": [null, null] } }

      ----------------------------dummyid

      Content-Disposition: form-data; name="map"

      map: { "1":["variables.files.0"], "2":["variables.files.1"] }

      ----------------------------dummyid

      Content-Disposition: form-data; name="1"; filename="file1.pdf"

      Content-Type: application/octet-stream

      --file 1 binary code--

      ----------------------------dummyid

      Content-Disposition: form-data; name="2"; filename="file2.pdf"

      Content-Type: application/octet-stream

      2: --file 2 binary code--

      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      variables - the input variables for the GraphQL query
      files - ClassPathResource instance for each file that will be uploaded to GraphQL server. When Spring RestTemplate processes the request, it will automatically produce a valid part representing given file inside multipart request (including size, submittedFileName, etc.)
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • postFiles

      public GraphQLResponse postFiles(String graphqlResource, com.fasterxml.jackson.databind.node.ObjectNode variables, List<org.springframework.core.io.ClassPathResource> files, IntFunction<String> pathFunc) throws IOException
      Handle the multipart files upload request to GraphQL servlet
      Parameters:
      graphqlResource - path to the classpath resource containing the GraphQL query
      variables - the input variables for the GraphQL query
      files - ClassPathResource instance for each file that will be uploaded to GraphQL server. When Spring RestTemplate processes the request, it will automatically produce a valid part representing given file inside multipart request (including size, submittedFileName, etc.)
      pathFunc - function to generate the path to file inside variables. For example:
      • index -> String.format("variables.files.%d", index) for multiple files
      • index -> "variables.file" for single file
      Returns:
      GraphQLResponse containing the result of query execution
      Throws:
      IOException - if the resource cannot be loaded from the classpath
    • postForString

      public GraphQLResponse postForString(String graphql) throws IOException
      Performs a GraphQL request using the provided GraphQL query string. Operation name will be derived from the provided GraphQL query string.
      Parameters:
      graphql - the GraphQL query
      Returns:
      GraphQLResponse containing the result of the query execution
      Throws:
      IOException - if the request json cannot be created because of issues with one of the provided arguments
    • postForString

      public GraphQLResponse postForString(String graphql, String operation) throws IOException
      Performs a GraphQL request using the provided GraphQL query string and operation name.
      Parameters:
      graphql - the GraphQL query
      operation - the name of the GraphQL operation to be executed
      Returns:
      GraphQLResponse containing the result of the query execution
      Throws:
      IOException - if the request json cannot be created because of issues with one of the provided arguments
    • postForString

      public GraphQLResponse postForString(String graphql, Map<String,?> variables) throws IOException
      Performs a GraphQL request using the provided GraphQL query string and variables. Operation name will be derived from the provided GraphQL query string.
      Parameters:
      graphql - the GraphQL query
      variables - the input variables for the GraphQL query
      Returns:
      GraphQLResponse containing the result of the query execution
      Throws:
      IOException - if the request json cannot be created because of issues with one of the provided arguments
    • postForString

      public GraphQLResponse postForString(String graphql, String operation, Map<String,?> variables) throws IOException
      Performs a GraphQL request using the provided GraphQL query string, operation name, and variables.
      Parameters:
      graphql - the GraphQL query
      operation - the name of the GraphQL operation to be executed
      variables - the input variables for the GraphQL query
      Returns:
      GraphQLResponse containing the result of the query execution
      Throws:
      IOException - if the request json cannot be created because of issues with one of the provided arguments
    • postForString

      public GraphQLResponse postForString(String graphql, com.fasterxml.jackson.databind.node.ObjectNode variables) throws IOException
      Performs a GraphQL request using the provided GraphQL query string and variables. Operation name will be derived from the provided GraphQL query string.
      Parameters:
      graphql - the GraphQL query
      variables - the input variables for the GraphQL query
      Returns:
      GraphQLResponse containing the result of the query execution
      Throws:
      IOException - if the request json cannot be created because of issues with one of the provided arguments
    • postForString

      public GraphQLResponse postForString(String graphql, String operation, com.fasterxml.jackson.databind.node.ObjectNode variables) throws IOException
      Performs a GraphQL request using the provided GraphQL query string, operation name, and variables.
      Parameters:
      graphql - the GraphQL query
      operation - the name of the GraphQL operation to be executed
      variables - the input variables for the GraphQL query
      Returns:
      GraphQLResponse containing the result of the query execution
      Throws:
      IOException - if the request json cannot be created because of issues with one of the provided arguments
    • post

      public GraphQLResponse post(String payload)
      Performs a GraphQL request with the provided payload.
      Parameters:
      payload - the GraphQL payload
      Returns:
      GraphQLResponse containing the result of query execution