44.6. 例子
44.6.1. 查询 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
简单查询可以在 URI 中直接定义:
from("direct:start")
.to("graphql://http://example.com/graphql?query={books{id name}}")
from("direct:start")
.to("graphql://http://example.com/graphql?query={books{id name}}")
正文也可以用于查询:
from("direct:start")
.setBody(constant("{books{id name}}"))
.to("graphql://http://example.com/graphql")
from("direct:start")
.setBody(constant("{books{id name}}"))
.to("graphql://http://example.com/graphql")
查询可以来自标头:
from("direct:start")
.setHeader("myQuery", constant("{books{id name}}"))
.to("graphql://http://example.com/graphql?queryHeader=myQuery")
from("direct:start")
.setHeader("myQuery", constant("{books{id name}}"))
.to("graphql://http://example.com/graphql?queryHeader=myQuery")
更复杂的查询可以存储在文件中,并按如下所示在 URI 中引用:
bookQuery.graphql 文件:
from("direct:start")
.to("graphql://http://example.com/graphql?queryFile=booksQuery.graphql")
from("direct:start")
.to("graphql://http://example.com/graphql?queryFile=booksQuery.graphql")
当查询文件定义多个操作时,需要指定应执行哪些操作:
from("direct:start")
.to("graphql://http://example.com/graphql?queryFile=multipleQueries.graphql&operationName=Books")
from("direct:start")
.to("graphql://http://example.com/graphql?queryFile=multipleQueries.graphql&operationName=Books")
使用变量的查询需要引用 registry 中的 JsonObject 实例:
bookByIdQuery.graphql 文件:
通过 variablesHeader 参数访问变量的查询:
44.6.2. mutations 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
变异类似于带有变量的查询。它们指定查询和对变量 bean 的引用,如下所示:
addBookMutation.graphql 文件: