Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fail to GET request with variables #58

Open
ttyniwa opened this issue Oct 26, 2019 · 1 comment · May be fixed by #60
Open

fail to GET request with variables #58

ttyniwa opened this issue Oct 26, 2019 · 1 comment · May be fixed by #60

Comments

@ttyniwa
Copy link

ttyniwa commented Oct 26, 2019

reported at #52 (comment)

Currently the transmission of variables in the request via puery param does not work either. When I send a request (as defined in the docu https://graphql.org/learn/serving-over-http/), the following error occurs

Field error in object 'graphQLRequest' on field 'variables': rejected value [{
"$content": "123"
}]; codes [typeMismatch.graphQLRequest.variables,typeMismatch.variables,typeMismatch.java.util.Map,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [graphQLRequest.variables,variables]; arguments []; default message [variables]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.util.Map'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String[]' to required type 'java.util.Map': no matching editors or conversion strategy found]]

	@Test
	public void defaultControllerTest_GET_with_variables() throws Exception {
		mockMvc.perform(
				get("/"+apiContext)
						.param("query","query echo($content: String) {echo(content: $content)}")
						.param("variables", "{\n" +
                                "  \"$content\": \"123\"\n" +
                                "}")
						)
				.andExpect(status().isOk())
				.andExpect(content().string(containsString("Hello world")));
	}

reason: Jackson databiner doesn't know how to convert String to Map.

@ttyniwa
Copy link
Author

ttyniwa commented Oct 26, 2019

Below code is a temporary resolution patch.
This code affects not only graphql-spqr, but also your code.

import example.StringToMapConverter
import org.springframework.context.annotation.Configuration
import org.springframework.format.FormatterRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
class WebMvcConfig : WebMvcConfigurer {

    override fun addFormatters(registry: FormatterRegistry) {
        registry.addConverter(StringToMapConverter())
    }
}


import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.core.convert.converter.Converter
import java.io.IOException

class StringToMapConverter : Converter<String, Map<String, Any>> {

    override fun convert(source: String): Map<String, Any> {
        try {
            return ObjectMapper().readValue(source, object : TypeReference<Map<String, Any>>() {})
        } catch (e: IOException) {
            throw RuntimeException(e.message)
        }
    }
}

Andy2003 added a commit to open-crypto-portfolio/graphql-spqr-spring-boot-starter that referenced this issue Oct 28, 2019
Andy2003 added a commit to open-crypto-portfolio/graphql-spqr-spring-boot-starter that referenced this issue Oct 28, 2019
Andy2003 added a commit to open-crypto-portfolio/graphql-spqr-spring-boot-starter that referenced this issue Jan 25, 2020
Andy2003 added a commit to open-crypto-portfolio/graphql-spqr-spring-boot-starter that referenced this issue Feb 2, 2020
@Andy2003 Andy2003 linked a pull request Aug 15, 2023 that will close this issue
Andy2003 added a commit to open-crypto-portfolio/graphql-spqr-spring-boot-starter that referenced this issue Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant