You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2023/03/27 19:22:51 UTC

[camel] 01/02: [CAMEL-19188] Make GraphQL component lenient (#9655)

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit c6809b2345765b1aa2cd4372b99c7ef69fff19c3
Author: Felix Hoßfeld <80...@users.noreply.github.com>
AuthorDate: Mon Mar 27 20:46:43 2023 +0200

    [CAMEL-19188] Make GraphQL component lenient (#9655)
    
    * Make GraphQL component lenient to support additional query parameters passed to the HTTP endpoints (CAMEL-19188)
    
    * Fixed order of the imports.
---
 .../graphql/GraphqlEndpointUriFactory.java         |  2 +-
 .../apache/camel/component/graphql/graphql.json    |  2 +-
 .../camel/component/graphql/GraphqlComponent.java  | 12 +++++++++++
 .../camel/component/graphql/GraphqlEndpoint.java   |  7 ++++++-
 .../component/graphql/GraphqlComponentTest.java    | 23 ++++++++++++++++++++++
 5 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/components/camel-graphql/src/generated/java/org/apache/camel/component/graphql/GraphqlEndpointUriFactory.java b/components/camel-graphql/src/generated/java/org/apache/camel/component/graphql/GraphqlEndpointUriFactory.java
index 5bf38618f74..68926589131 100644
--- a/components/camel-graphql/src/generated/java/org/apache/camel/component/graphql/GraphqlEndpointUriFactory.java
+++ b/components/camel-graphql/src/generated/java/org/apache/camel/component/graphql/GraphqlEndpointUriFactory.java
@@ -78,7 +78,7 @@ public class GraphqlEndpointUriFactory extends org.apache.camel.support.componen
 
     @Override
     public boolean isLenientProperties() {
-        return false;
+        return true;
     }
 }
 
diff --git a/components/camel-graphql/src/generated/resources/org/apache/camel/component/graphql/graphql.json b/components/camel-graphql/src/generated/resources/org/apache/camel/component/graphql/graphql.json
index 5efbcb8c13d..55a2f26cbf0 100644
--- a/components/camel-graphql/src/generated/resources/org/apache/camel/component/graphql/graphql.json
+++ b/components/camel-graphql/src/generated/resources/org/apache/camel/component/graphql/graphql.json
@@ -19,7 +19,7 @@
     "api": false,
     "consumerOnly": false,
     "producerOnly": true,
-    "lenientProperties": false
+    "lenientProperties": true
   },
   "componentProperties": {
     "lazyStartProducer": { "kind": "property", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during star [...]
diff --git a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlComponent.java b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlComponent.java
index 4d346cda6bc..605a08b0408 100644
--- a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlComponent.java
+++ b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlComponent.java
@@ -22,6 +22,7 @@ import java.util.Map;
 import org.apache.camel.Endpoint;
 import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.util.URISupport;
 
 @Component("graphql")
 public class GraphqlComponent extends DefaultComponent {
@@ -34,4 +35,15 @@ public class GraphqlComponent extends DefaultComponent {
         return endpoint;
     }
 
+    @Override
+    protected void afterConfiguration(String uri, String remaining, Endpoint endpoint, Map<String, Object> parameters)
+            throws Exception {
+
+        GraphqlEndpoint graphqlEndpoint = (GraphqlEndpoint) endpoint;
+        if (!parameters.isEmpty()) {
+            URI httpUri = URISupport.createRemainingURI(graphqlEndpoint.getHttpUri(), parameters);
+            graphqlEndpoint.setHttpUri(httpUri);
+        }
+    }
+
 }
diff --git a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
index 80869870fc8..d28dd044f73 100644
--- a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
+++ b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
@@ -49,7 +49,7 @@ import org.apache.hc.core5.http.message.BasicHeader;
  * Send GraphQL queries and mutations to external systems.
  */
 @UriEndpoint(firstVersion = "3.0.0", scheme = "graphql", title = "GraphQL", syntax = "graphql:httpUri",
-             category = { Category.API }, producerOnly = true)
+             category = { Category.API }, producerOnly = true, lenientProperties = true)
 public class GraphqlEndpoint extends DefaultEndpoint {
 
     @UriPath
@@ -280,4 +280,9 @@ public class GraphqlEndpoint extends DefaultEndpoint {
     public void setHttpClient(CloseableHttpClient httpClient) {
         this.httpClient = httpClient;
     }
+
+    @Override
+    public boolean isLenientProperties() {
+        return true;
+    }
 }
diff --git a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/GraphqlComponentTest.java b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/GraphqlComponentTest.java
index 11b6ed4db27..24cff92c41a 100644
--- a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/GraphqlComponentTest.java
+++ b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/GraphqlComponentTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.graphql;
 
 import java.io.IOException;
+import java.net.URI;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.camel.BindToRegistry;
@@ -32,6 +33,8 @@ import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 public class GraphqlComponentTest extends CamelTestSupport {
 
     private static String booksQueryResult;
@@ -112,6 +115,9 @@ public class GraphqlComponentTest extends CamelTestSupport {
                         .to("graphql://http://localhost:" + server.getPort()
                             + "/graphql?queryHeader=myQuery")
                         .to("mock:result");
+                from("direct:start8")
+                        .to("graphql://http://localhost:" + server.getPort() + "/graphql?apikey=123456&query={books{id name}}")
+                        .to("mock:result");
             }
         };
     }
@@ -199,4 +205,21 @@ public class GraphqlComponentTest extends CamelTestSupport {
 
         result.assertIsSatisfied();
     }
+
+    @Test
+    public void checkApiKey() throws Exception {
+
+        GraphqlEndpoint graphqlEndpoint = (GraphqlEndpoint) template.getCamelContext().getEndpoint(
+                "graphql://http://localhost:" + server.getPort() + "/graphql?apikey=123456&query={books{id name}}");
+        URI httpUri = graphqlEndpoint.getHttpUri();
+        assertEquals("apikey=123456", httpUri.getQuery());
+
+        result.expectedMessageCount(1);
+        result.expectedBodiesReceived(booksQueryResult);
+
+        template.sendBody("direct:start8", "");
+
+        result.assertIsSatisfied();
+
+    }
 }