在GraphQL(二):GraphQL服务搭建中咱们在pom文件中增长了以下依赖:java
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
复制代码
接下来咱们来分析其中的部分原理。spring
添加了上述依赖后,会引入这么几个jar包:api
开发者工具的两个包暂不讨论。一切都是从graphql-spring-boot-autoconfigure开始的,经过graphql-spring-boot-autoconfigure完成了GraphQLServlet的自动配置。跨域
@Configuration
@ConfigurationProperties(prefix = "graphql.servlet")
public class GraphQLServletProperties {
private String mapping;
public String getMapping() {
return mapping != null ? mapping : "/graphql";
}
//省略
}
复制代码
在GraphQLServletProperties配置类上启动了ConfigurationProperties,前缀是"graphql.servlet",所以咱们能够在application.properties中以"graphql.servlet"开头进行配置,好比将endpoint从默认的“/graphql”改成“/school”:bash
graphql.servlet.mapping=/school
复制代码
一样的,在GraphQLWebAutoConfiguration配置类中能够找到关因而否启用GraphQLServlet和跨域访问的配置。app
经过graphql-spring-boot-autoconfigure,SpringBoot会自动扫描到GraphQLServlet的相关配置信息,在GraphQLServlet的构造函数中初始化了getHandler和postHandler分别用于处理get和post请求 函数
和Spring的DispatcherServlet不同,GraphQLServlet重写了doGet和doPost方法,同时GraphQLServlet并不包含拦截器(DispatcherServlet请求执行过程),GraphQL提供了一个GraphQLServletListener接口,容许咱们针对请求执行结果作处理:spring-boot
private void doRequest(HttpServletRequest request, HttpServletResponse response, RequestHandler handler) {
List<GraphQLServletListener.RequestCallback> requestCallbacks = runListeners(l -> l.onRequest(request, response));
try {
handler.handle(request, response);
runCallbacks(requestCallbacks, c -> c.onSuccess(request, response));
} catch (Throwable t) {
response.setStatus(500);
log.error("Error executing GraphQL request!", t);
runCallbacks(requestCallbacks, c -> c.onError(request, response, t));
} finally {
runCallbacks(requestCallbacks, c -> c.onFinally(request, response));
}
}
复制代码
那么,若是要在GraphQL中实现拦截器的功能要怎么作呢?工具
GraphQL提供了一个Instrumentation接口: post
在GraphQL(二):GraphQL服务搭建中咱们提到,实现Resolver须要知足以下约定:
1. <field>
2. is<field> – only if the field is of type Boolean
3. get<field>
4. getField<field>(最新版增长的契约)
复制代码
关于这部分契约的定义在官方文档中并无找到,那就从源代码去找是如何定义契约。
在graphql-java-tools(4.0.0版本)中,能够找到一个FieldResolverScanner类,负责了FieldResolver的扫描,找到方法findResolverMethod:
private fun findResolverMethod(field: FieldDefinition, search: Search): java.lang.reflect.Method? {
val methods = getAllMethods(search.type)
val argumentCount = field.inputValueDefinitions.size + if(search.requiredFirstParameterType != null) 1 else 0
val name = field.name
val isBoolean = isBoolean(field.type)
// Check for the following one by one:
// 1. Method with exact field name
// 2. Method that returns a boolean with "is" style getter
// 3. Method with "get" style getter
return methods.find {
it.name == name && verifyMethodArguments(it, argumentCount, search)
} ?: methods.find {
(isBoolean && it.name == "is${name.capitalize()}") && verifyMethodArguments(it, argumentCount, search)
} ?: methods.find {
it.name == "get${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search)
}
}
复制代码
这就是定义以上契约的地方。