Class GraphQLTestTemplate
-
Constructor Summary
ConstructorsConstructorDescriptionGraphQLTestTemplate
(org.springframework.core.io.ResourceLoader resourceLoader, org.springframework.boot.test.web.client.TestRestTemplate restTemplate, String graphqlMapping, com.fasterxml.jackson.databind.ObjectMapper objectMapper) -
Method Summary
Modifier and TypeMethodDescriptionLoads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.perform
(String graphqlResource, com.fasterxml.jackson.databind.node.ObjectNode variables, List<String> fragmentResources) Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.perform
(String graphqlResource, String operation, com.fasterxml.jackson.databind.node.ObjectNode variables) Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.perform
(String graphqlResource, String operationName, com.fasterxml.jackson.databind.node.ObjectNode variables, List<String> fragmentResources) Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.Performs a GraphQL request with the provided payload.postFiles
(String graphqlResource, com.fasterxml.jackson.databind.node.ObjectNode variables, List<org.springframework.core.io.ClassPathResource> files) Handle the multipart files upload request to GraphQL servletpostFiles
(String graphqlResource, com.fasterxml.jackson.databind.node.ObjectNode variables, List<org.springframework.core.io.ClassPathResource> files, IntFunction<String> pathFunc) Handle the multipart files upload request to GraphQL servletpostForResource
(String graphqlResource) Loads a GraphQL query or mutation from the given classpath resource and sends it to the GraphQL server.postForResource
(String graphqlResource, List<String> fragmentResources) Loads a GraphQL query or mutation from the given classpath resource, appending any graphql fragment resources provided and sends it to the GraphQL server.postForString
(String graphql) Performs a GraphQL request using the provided GraphQL query string.postForString
(String graphql, com.fasterxml.jackson.databind.node.ObjectNode variables) Performs a GraphQL request using the provided GraphQL query string and variables.postForString
(String graphql, String operation) Performs a GraphQL request using the provided GraphQL query string and operation name.postForString
(String graphql, String operation, com.fasterxml.jackson.databind.node.ObjectNode variables) Performs a GraphQL request using the provided GraphQL query string, operation name, and variables.postForString
(String graphql, String operation, Map<String, ?> variables) Performs a GraphQL request using the provided GraphQL query string, operation name, and variables.postForString
(String graphql, Map<String, ?> variables) Performs a GraphQL request using the provided GraphQL query string and variables.postMultipart
(String query, String variables) withAdditionalHeader
(String name, String... value) Add an HTTP header that will be sent with each request this sends.withAdditionalHeaders
(org.springframework.util.MultiValueMap<String, String> additionalHeaders) Add multiple HTTP header that will be sent with each request this sends.withBasicAuth
(@NonNull String encodedCredentials) Adds basic authentication to the authorization header.withBasicAuth
(@NonNull String username, @NonNull String password) Adds basic authentication to the authorization header.withBasicAuth
(@NonNull String username, @NonNull String password, Charset charset) Adds basic authentication to the authorization header.withBearerAuth
(@NonNull String token) Adds a bearer token to the authorization header.Clear all associated HTTP headers.withHeaders
(org.springframework.http.HttpHeaders newHeaders) Replace any associated HTTP headers with the provided headers.
-
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
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
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 usernamepassword
- the passwordcharset
- 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 usernamepassword
- the password- Returns:
- self
-
withBasicAuth
Adds basic authentication to the authorization header.- Parameters:
encodedCredentials
- the encoded credentials- Returns:
- self
-
withHeaders
Replace any associated HTTP headers with the provided headers.- Parameters:
newHeaders
- Headers to use.- Returns:
- self
-
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 queryvariables
- 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
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 queryoperationName
- 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 queryoperation
- the name of the GraphQL operation to be executedvariables
- 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 queryvariables
- the input variables for the GraphQL queryfragmentResources
- 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 queryvariables
- the input variables for the GraphQL queryfragmentResources
- 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
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 queryfragmentResources
- 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
-
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 servletIn 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 queryvariables
- the input variables for the GraphQL queryfiles
- 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 queryvariables
- the input variables for the GraphQL queryfiles
- 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
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
Performs a GraphQL request using the provided GraphQL query string and operation name.- Parameters:
graphql
- the GraphQL queryoperation
- 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
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 queryvariables
- 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 IOExceptionPerforms a GraphQL request using the provided GraphQL query string, operation name, and variables.- Parameters:
graphql
- the GraphQL queryoperation
- the name of the GraphQL operation to be executedvariables
- 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 queryvariables
- 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 queryoperation
- the name of the GraphQL operation to be executedvariables
- 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
Performs a GraphQL request with the provided payload.- Parameters:
payload
- the GraphQL payload- Returns:
GraphQLResponse
containing the result of query execution
-