在GraphQL(二):GraphQL服務搭建中我們在pom文件中增加了如下依賴:
<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>
接下來我們來分析其中的部分原理拉馋。
包
添加了上述依賴后跑揉,會引入這么幾個jar包:
- graphiql-spring-boot-autoconfigure: 開發(fā)者工具graphiql的自動配置jar包
- graphiql-spring-boot-starter: 開發(fā)者工具graphiql的實現(xiàn)
- graphql-java: graphql的java實現(xiàn)
- graphql-java-servlet: 封裝graphql服務為servlet,處理graphql的request和response
- graphql-java-tools: 自動加載*.graphqls文件行疏,并屏蔽graphql-java的底層實現(xiàn)細節(jié)
- graphql-spring-boot-autoconfigure: graphql-spring-boot的自動配置jar包
- graphql-spring-boot-starter: starter
開發(fā)者工具的兩個包暫不討論匆光。一切都是從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”:
graphql.servlet.mapping=/school
同樣的周崭,在GraphQLWebAutoConfiguration配置類中可以找到關于是否啟用GraphQLServlet和跨域訪問的配置。
GraphQLServlet
通過graphql-spring-boot-autoconfigure喳张,SpringBoot會自動掃描到GraphQLServlet的相關配置信息续镇,在GraphQLServlet的構造函數(shù)中初始化了getHandler和postHandler分別用于處理get和post請求
和Spring的DispatcherServlet不一樣,GraphQLServlet重寫了doGet和doPost方法销部,同時GraphQLServlet并不包含攔截器(DispatcherServlet請求執(zhí)行過程)摸航,GraphQL提供了一個GraphQLServletListener接口,允許我們針對請求執(zhí)行結果做處理:
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中實現(xiàn)攔截器的功能要怎么做呢酱虎?
GraphQL提供了一個Instrumentation接口:
允許我們在執(zhí)行前、解析前擂涛、驗證前读串、數(shù)據獲取前、字段數(shù)據獲取前(最后兩個是一樣的作用)插入自己的邏輯撒妈,但是它跟Spring的攔截器不一樣恢暖,它沒有提供跳過執(zhí)行的功能,要攔截掉執(zhí)行只能拋出異常狰右。
FiledResolverScanner
在GraphQL(二):GraphQL服務搭建中我們提到杰捂,實現(xiàn)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)
}
}
這就是定義以上契約的地方。