Spring认证_什么是SpringGraphQL?第一课:https://segmentfault.com/a/11...第二课:https://segmentfault.com/a/11...Launcher这个项目建立在Boot2.x上,不过应该是相关的到最新的Boot2.4.x5。项目设置要创建一个项目,请转到start.spring.io并为要使用的GraphQL传输选择一个启动器:StartertransportExecutespring-boot-starter-webHTTPSpringMVCspring-boot-starter-websocketWebresultforServletapplicationProgramWebSocketspring-boot-starter-webfluxHTTP、WebSocketSpringWebFlux在生成的项目中手动添加graphql-spring-boot-starter:依赖{//SpringGraphQL启动实现'org.springframework.experimental:graphql-spring-boot-starter:1.0.0-SNAPSHOT'//...}repository{MavenCentral()maven{url'https://repo.spring.io/milestone'}//spring里程碑maven{url'https://repo.spring.io/snapshot'}//Springsnapshot}schema默认情况下,GraphQLschema文件将位于src/main/resources/graphql中,扩展名为“.graphqls”、“.graphql”、“.gql”或“.gqls”。您可以自定义要检查的目录位置,如下所示:spring.graphql.schema.locations=classpath:graphql/架构架构可以通过HTTP上的“/GraphQL/schema”的graphQL访问。默认情况下不允许这样做:spring.graphql.schema.printer.enabled=falseDataFetcher注册您可以使用GraphQL引擎bean在Spring配置中声明RuntimeWiringConfigurer,并将这些注册用于数据获取器、类型解析器等:@componentpublicclassPersonDataWiring实现RuntimeWiringConfigurer{privatefinalPersonService服务;publicPersonDataWiring(PersonServiceservice){this.service=service;}@overridepublicvoidConfigure(RuntimeWiring.Builderbuilder){builder.type("query",wiring->Wiring.dataFetcher("people",env->this.service.findAll()));查询dsl存储库扩展QuerydslPredicateExecutor或扩展或ReactiveQuerydslPredicateExecutor存储库@GraphQlRepository由SpringData检测并决定DataFetcher自动注册以匹配查询的环境候选者。WebSearch默认情况下,GraphQLHTTP访问地址为HTTPPOST"/graphql"。路径可以自定义:spring.graphql.path=/graphqlGraphQLWebSocket默认支持位于“/graphql”的WebSocket。适用于WebSocket处理的属性如下所示:spring.graphql.websocket.path=/Thetimegraphqlmustreceivea"CONNECTION_INIT"messagefromtheclientspring.graphql.websocket.connection-init-timeout=60sGraphQLWebSocketisclosedby默认。要启用它:对于Servlet应用程序,添加WebSocketstarterspring-boot-starter-websocket。对于WebFlux应用程序,设置spring.graphql.websocket.path应用程序属性。声明一个WebInterceptorbean以通过HTTP和WebSocket请求注册GraphQL的Web拦截。声明一个ThreadLocalAccessorbean以帮助在SpringMVCThreadLocal中传播鼠标的值。GraphiQLSpringBoot启动器默认包含一个在“/graphiql”中公开的GraphiQL页面。您可以按如下方式配置它:spring.graphql.graphiql.enabled=truespring.graphql.graphiql.path=/graphiql-metrics当启动程序spring-boot-starter-actuator出现在类路径上时,将收集GraphQL请求的指标.您可以按如下方式查找指标收集:management.metrics.graphql.autotime.enabled=false以下部分可能在您的应用程序配置中,可以通过ActuatorWeb指标公开。如下:management.endpoints.web.exposure.include=health,metrics,inforequest事件请求访问位置/actuator/metrics/graphql.request。标签描述示例值结果请求结果“成功”、“错误”DataFetcher发生DataFetcher指标计时器可在/actuator/metrics/graphql.datafetcher获得。LabelDescriptionSampleValuePath数据获取路径"query.item"Result数据获取结果"Success","Error"ErrorVotingGraphQLError点击选择位置/actuator/metrics/graphql.error。标签描述示例值错误类型错误类型“数据获取异常”错误路径错误JSON路径“$.project”测试SpringGraphQL测试支持将以下内容添加到您的类路径中,这是一个可用于注入测试的WebGraphQlTester团队:依赖{testImplementation'org.springframework.boot:spring-boot-starter-test'testImplementation'org.springframework.graphql:spring-graphql-test:1.0.0-SNAPSHOT'//也添加这个,除非spring-boot-starter-webfluxalso存在testImplementation'org.springframework:spring-webflux'//...}repository{MavenCentral()maven{url'https://repo.spring.io/milestone'}//springmilestonemaven{url'https://repo.spring.io/snapshot'}//Springsnapshot}对于SpringMVC基于HTTP的GraphQL,用作MockMvc服务器:@SpringBootTest@AutoConfigureMockMvc@AutoConfigureGraphQlTeste对于使用SpringWebFlux的HTTP上的GraphQL,使用模拟服务器:@SpringBootTest@AutoConfigureWebTestClient@AutoConfigureGraphQlTeste对于运行服务器的HTTP上的GraphQL:@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)@AutoConfigureGraphQlTeste公共类MockMvcGraphQlTests{@autowireprivateWebGraphQlTestergraphQlTester;订阅可以在没有WebSocket的情况下进行测试,如下所示:@SpringBootTest@AutoConfigureGraphQlTeste@testvoidSubscription(){Flux
