= GraphQL Component
:doctitle: GraphQL
:shortname: graphql
:artifactid: camel-graphql
:description: Send GraphQL queries and mutations to external systems.
:since: 3.0
:supportlevel: Stable
:component-header: Only producer is supported
//Manually maintained attributes
:camel-spring-boot-name: graphql

*Since Camel {since}*

*{component-header}*

The GraphQL component is a GraphQL client that communicates over HTTP and supports queries and mutations, but not subscriptions. It uses the https://hc.apache.org/httpcomponents-client-4.5.x/index.html[Apache HttpClient] library.

Maven users will need to add the following dependency to their `pom.xml`
for this component:

[source,xml]
------------------------------------------------------------
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-graphql</artifactId>
    <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>
------------------------------------------------------------


// component-configure options: START

// component-configure options: END

// component options: START
include::partial$component-configure-options.adoc[]
include::partial$component-endpoint-options.adoc[]
// component options: END

// endpoint options: START

// endpoint options: END


== Message Body

If the `variables` and `variablesHeader` parameters are not set and the IN body is a JsonObject instance, Camel will use it for the operation's variables. If the `query` and `queryFile` parameters are not set and the IN body is a String, Camel will use it as the query. Camel will store the GraphQL response from the external server on the OUT message body. All headers from the IN message will be copied to the OUT message, so headers are preserved during routing. Additionally Camel will add the HTTP response headers as well to the OUT message headers.

== Examples

=== Queries

Simple queries can be defined directly in the URI:

[source,java]
----
from("direct:start")
    .to("graphql://http://example.com/graphql?query={books{id name}}")
----

The body can also be used for the query:

[source,java]
----
from("direct:start")
    .setBody(constant("{books{id name}}"))
    .to("graphql://http://example.com/graphql")
----

The query can come from a header also:

[source,java]
----
from("direct:start")
    .setHeader("myQuery", constant("{books{id name}}"))
    .to("graphql://http://example.com/graphql?queryHeader=myQuery")
----

More complex queries can be stored in a file and referenced in the URI:

booksQuery.graphql file:
----
query Books {
  books {
    id
    name
  }
}
----

[source,java]
----
from("direct:start")
    .to("graphql://http://example.com/graphql?queryFile=booksQuery.graphql")
----

When the query file defines multiple operations, it's required to specify which one should be executed:

[source,java]
----
from("direct:start")
    .to("graphql://http://example.com/graphql?queryFile=multipleQueries.graphql&operationName=Books")
----

Queries with variables need to reference a JsonObject instance from the registry:

[source,java]
----
@BindToRegistry("bookByIdQueryVariables")
public JsonObject bookByIdQueryVariables() {
    JsonObject variables = new JsonObject();
    variables.put("id", "book-1");
    return variables;
}

from("direct:start")
    .to("graphql://http://example.com/graphql?queryFile=bookByIdQuery.graphql&variables=#bookByIdQueryVariables")
----

A query that accesses variables via the variablesHeader parameter:

[source,java]
----
from("direct:start")
    .setHeader("myVariables", () -> {
        JsonObject variables = new JsonObject();
        variables.put("id", "book-1");
        return variables;
    })
    .to("graphql://http://example.com/graphql?queryFile=bookByIdQuery.graphql&variablesHeader=myVariables")
----

=== Mutations

Mutations are like queries with variables. They specify a query and a reference to a variables bean:

addBookMutation.graphql file:

----
mutation AddBook($bookInput: BookInput) {
  addBook(bookInput: $bookInput) {
    id
    name
    author {
      name
    }
  }
}
----

[source,java
----
@BindToRegistry("addBookMutationVariables")
public JsonObject addBookMutationVariables() {
    JsonObject bookInput = new JsonObject();
    bookInput.put("name", "Typee");
    bookInput.put("authorId", "author-2");
    JsonObject variables = new JsonObject();
    variables.put("bookInput", bookInput);
    return variables;
}

from("direct:start")
    .to("graphql://http://example.com/graphql?graphql?queryFile=addBookMutation.graphql&variables=#addBookMutationVariables")
----


include::spring-boot:partial$starter.adoc[]
