Repository: quarkusio/quarkus
Issue: SmallRye GraphQL returns multiple content-types in `Content-Type` header when requested with multiple content-types in `Accept` header

### Describe the bug

When querying `/graphql` with an `Accept` HTTP-header containing multiple media types, e.g. `application/json, text/plain`, the endpoint mirrors that header's content into the response's `Content-Type` header. `application/json, text/plain` is not a valid `Content-Type` and subsequent parsing fails.

### Expected behavior

Only the chosen media type is used in the response's `Content-Type` header, `application/json` in this case.

### Actual behavior

The response currently is invalid, and trying to process it throws an exception. See also steps to reproduce.

### How to Reproduce?

Have a GraphQL endpoint, e.g.:

```java
@GraphQLApi
public class GraphQLResource {
    @Query
    public String test() {
        return "test";
    }
}
```

Then query it with multiple `Accept` media types, e.g.:

```java
Client client = ClientBuilder.newClient();
Response response = client
        .target("http://localhost:8080/graphql")
        .request(MediaType.APPLICATION_JSON_TYPE)
        .accept(MediaType.TEXT_PLAIN) // this adds a second `Accept` media type
        .post(Entity.entity("{\\"query\\":\\"query {test}\\"}", MediaType.APPLICATION_JSON));
return response.readEntity(String.class); // exception thrown here
```

The result is an error:

```
java.lang.IllegalArgumentException: RESTEASY003340: Failure parsing MediaType string: application/json, text/plain
        at org.jboss.resteasy.plugins.delegates.MediaTypeHeaderDelegate.internalParse(MediaTypeHeaderDelegate.java:118)
        at org.jboss.resteasy.plugins.delegates.MediaTypeHeaderDelegate.parse(MediaTypeHeaderDelegate.java:69)
        at org.jboss.resteasy.plugins.delegates.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate.java:33)
        at org.jboss.resteasy.plugins.delegates.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate.java:19)
        at javax.ws.rs.core.MediaType.valueOf(MediaType.java:172)
        at org.jboss.resteasy.specimpl.AbstractBuiltResponse.getMediaType(AbstractBuiltResponse.java:342)
        at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:90)
        at org.jboss.resteasy.specimpl.AbstractBuiltResponse.readEntity(AbstractBuiltResponse.java:262)
        ...
```

### Output of `java -version`

```
openjdk 17 2021-09-14
OpenJDK Runtime Environment Temurin-17+35 (build 17+35)
OpenJDK 64-Bit Server VM Temurin-17+35 (build 17+35, mixed mode, sharing)
```

### Quarkus version or git rev

2.4.0.Final


Changed files:
extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLOverHttpTest.java
extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLExecutionHandler.java

diff --git a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLOverHttpTest.java b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLOverHttpTest.java
index ccf5ddb9ce8..7465817bed3 100644
--- a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLOverHttpTest.java
+++ b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLOverHttpTest.java
@@ -1,6 +1,7 @@
 package io.quarkus.smallrye.graphql.deployment;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.hamcrest.CoreMatchers;
@@ -77,6 +78,25 @@ public void httpPostWithQueryInQueryParamAndBodyTest() throws Exception {
                 "{\\"data\\":{\\"user\\":{\\"id\\":\\"1\\",\\"name\\":\\"Koos\\",\\"surname\\":\\"van der Merwe\\"}}}"); // query in query parameter win
     }
 
+    @Test
+    public void httpPostWithMultipleAcceptHeaders() throws Exception {
+
+        Map<String, String> queryparams = new HashMap<>();
+        queryparams.put("query", "query ($id: ID!) {  user(id:$id) {    id    name  surname}}");
+
+        String variables = "{\\"id\\": \\"1\\"}";
+
+        String request = "query ($id: ID!) {\\n" +
+                "  user(id:$id) {\\n" +
+                "    name\\n" +
+                "  }\\n" +
+                "}";
+
+        post(request, queryparams, variables,
+                "{\\"data\\":{\\"user\\":{\\"id\\":\\"1\\",\\"name\\":\\"Koos\\",\\"surname\\":\\"van der Merwe\\"}}}",
+                List.of(MEDIATYPE_JSON, MEDIATYPE_TEXT));
+    }
+
     @Test
     public void httpPostWithVariablesQueryParamTest() throws Exception {
 
@@ -121,13 +141,18 @@ private static Map<String, String> configuration() {
     }
 
     private void post(String request, Map<String, ?> queryparams, String variables, String expected) {
+        post(request, queryparams, variables, expected, List.of(MEDIATYPE_JSON));
+    }
+
+    private void post(String request, Map<String, ?> queryparams, String variables, String expected,
+            List<String> acceptHeaders) {
         RequestSpecification requestSpecification = RestAssured.given();
         if (queryparams != null) {
             requestSpecification = requestSpecification.queryParams(queryparams);
         }
 
         requestSpecification.when()
-                .accept(MEDIATYPE_JSON)
+                .accept(String.join(",", acceptHeaders))
                 .contentType(MEDIATYPE_JSON)
                 .body(getPayload(request, variables))
                 .post("/graphql")
diff --git a/extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLExecutionHandler.java b/extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLExecutionHandler.java
index 161305c4565..df03f1d40b8 100644
--- a/extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLExecutionHandler.java
+++ b/extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLExecutionHandler.java
@@ -20,6 +20,8 @@
 import io.vertx.core.http.HttpHeaders;
 import io.vertx.core.http.HttpServerRequest;
 import io.vertx.core.http.HttpServerResponse;
+import io.vertx.ext.web.MIMEHeader;
+import io.vertx.ext.web.ParsedHeaderValues;
 import io.vertx.ext.web.RoutingContext;
 
 /**
@@ -211,9 +213,19 @@ private String getRequestContentType(RoutingContext ctx) {
     }
 
     private String getRequestAccept(RoutingContext ctx) {
-        String accept = ctx.request().getHeader("Accept");
-        if (accept != null && !accept.isEmpty() && !accept.startsWith("*/*")) {
-            return accept;
+        ParsedHeaderValues parsedHeaders = ctx.parsedHeaders();
+        if (parsedHeaders != null && parsedHeaders.accept() != null && !parsedHeaders.accept().isEmpty()) {
+            List<MIMEHeader> acceptList = parsedHeaders.accept();
+            for (MIMEHeader a : acceptList) {
+                if (isValidAcceptRequest(a.rawValue())) {
+                    return a.rawValue();
+                }
+            }
+            // Seems like an unknown accept is passed in
+            String accept = ctx.request().getHeader("Accept");
+            if (accept != null && !accept.isEmpty() && !accept.startsWith("*/*")) {
+                return accept;
+            }
         }
         return DEFAULT_RESPONSE_CONTENT_TYPE;
     }