Skip to content

Commit

Permalink
chore(graphql): Use element template generator for GraphQL outbound c… (
Browse files Browse the repository at this point in the history
#1903)

* chore(graphql): Use element template generator for GraphQL outbound connector

* Format code

* Add e2e tests. Fix variables encoding for GET requests.

* Fix variables type mapping

* Fix test regressions
  • Loading branch information
sbuettner authored Feb 7, 2024
1 parent bd90e78 commit 7ea4eb5
Show file tree
Hide file tree
Showing 20 changed files with 1,250 additions and 660 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ public File writeTo(File output) {
throw new RuntimeException(e);
}
}

public String toString() {
return documentContext.jsonString();
}
}
6 changes: 6 additions & 0 deletions connectors-e2e-test/connectors-e2e-test-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.camunda.connector</groupId>
<artifactId>connector-graphql</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.camunda.connector</groupId>
<artifactId>connector-webhook</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package io.camunda.connector.e2e;

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
Expand Down Expand Up @@ -353,4 +356,106 @@ void successfulWebhookModelRun() throws Exception {

assertThat(bpmnTest.getProcessInstanceEvent()).hasVariableWithValue("webhookExecuted", true);
}

@Test
void graphQL() {
// Prepare an HTTP mock server
wm.stubFor(
post(urlPathMatching("/mock"))
.withHeader("testHeader", matching("testHeaderValue"))
.withBasicAuth("username", "password")
.withRequestBody(matchingJsonPath("$..query", equalTo("{hero { name } }")))
.withRequestBody(matchingJsonPath("$..variables.hello", equalTo("world")))
.willReturn(
ResponseDefinitionBuilder.okForJson(
Map.of("order", Map.of("status", "processing")))));

var mockUrl = "http://localhost:" + wm.getPort() + "/mock";

var model =
Bpmn.createProcess().executable().startEvent().serviceTask("graphqlTask").endEvent().done();

var elementTemplatePath =
"../../connectors/http/graphql/element-templates/graphql-outbound-connector.json";
var elementTemplate =
ElementTemplate.from(elementTemplatePath)
.property("graphql.url", mockUrl)
.property("graphql.method", "post")
.property("graphql.headers", "={testHeader: \"testHeaderValue\"}")
.property("graphql.query", "{hero { name } }")
.property("graphql.variables", "={hello:\"world\"}")
.property("authentication.type", BasicAuthentication.TYPE)
.property("authentication.username", "username")
.property("authentication.password", "password")
.property("resultExpression", "={orderStatus: response.body.order.status}")
.writeTo(new File(tempDir, "template.json"));

var updatedElementTemplateFile = new File(tempDir, "result.bpmn");
var updatedModel =
new BpmnFile(model)
.writeToFile(new File(tempDir, "test.bpmn"))
.apply(elementTemplate, "graphqlTask", updatedElementTemplateFile);

var bpmnTest =
ZeebeTest.with(zeebeClient)
.deploy(updatedModel)
.createInstance()
.waitForProcessCompletion();

assertThat(bpmnTest.getProcessInstanceEvent())
.hasVariableWithValue("orderStatus", "processing");
}

@Test
void graphQLViaGet() {
// Prepare an HTTP mock server
wm.stubFor(
get(urlPathMatching("/mock"))
.withHeader("testHeader", matching("testHeaderValue"))
.withBasicAuth("username", "password")
.withQueryParams(
Map.of(
"query",
equalTo("{hero { name } }"),
"variables",
equalTo("{\"hello\":\"world\"}")))
.willReturn(
ResponseDefinitionBuilder.okForJson(
Map.of("order", Map.of("status", "processing")))));

var mockUrl = "http://localhost:" + wm.getPort() + "/mock";

var model =
Bpmn.createProcess().executable().startEvent().serviceTask("graphqlTask").endEvent().done();

var elementTemplatePath =
"../../connectors/http/graphql/element-templates/graphql-outbound-connector.json";
var elementTemplate =
ElementTemplate.from(elementTemplatePath)
.property("graphql.url", mockUrl)
.property("graphql.method", "get")
.property("graphql.headers", "={testHeader: \"testHeaderValue\"}")
.property("graphql.query", "{hero { name } }")
.property("graphql.variables", "={hello:\"world\"}")
.property("authentication.type", BasicAuthentication.TYPE)
.property("authentication.username", "username")
.property("authentication.password", "password")
.property("resultExpression", "={orderStatus: response.body.order.status}")
.writeTo(new File(tempDir, "template.json"));

var updatedElementTemplateFile = new File(tempDir, "result.bpmn");
var updatedModel =
new BpmnFile(model)
.writeToFile(new File(tempDir, "test.bpmn"))
.apply(elementTemplate, "graphqlTask", updatedElementTemplateFile);

var bpmnTest =
ZeebeTest.with(zeebeClient)
.deploy(updatedModel)
.createInstance()
.waitForProcessCompletion();

assertThat(bpmnTest.getProcessInstanceEvent())
.hasVariableWithValue("orderStatus", "processing");
}
}
2 changes: 1 addition & 1 deletion connectors/http/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@ Set the specific property or environment variable to enable proxy mode as descri
## Element Template

The element templates can be found in
the [element-templates/graphql-connector.json](element-templates/graphql-connector.json) file.
the [element-templates/graphql-outbound-connector.json](element-templates/graphql-outbound-connector.json) file.
Loading

0 comments on commit 7ea4eb5

Please sign in to comment.