我正在尝试使用 graphql-spring-boot-starter 构建 GraphQL 服务器,并对 Apollo 跟踪 ( https://github.com/apollographql/apollo-tracing ) 感兴趣,它在扩展中的跟踪键下添加额外的数据。
添加跟踪仪器后,我的响应没有任何变化。 我在这里错过了什么吗?
请求:
query getalllink{
allLinks(filter:{description_contains:"ab"}){
url,
postedBy {
id
,name
}
}
}
回复:
{
"data": {
"allLinks": [
{
"url": "1a",
"postedBy": {
"id": "1",
"name": "1"
}
}
]
}
我已经提供了 TracingInstrumentation Bean。 并且 GraphQLWebAutoConfiguration 也拾取了该 bean。
我尝试调试并在 GraphQLWebAutoConfiguration 处命中断点,该检测已添加到 GraphQLServlet bean 中。
以下是有关我的示例项目的一些详细信息:
@SpringBootApplication
public class DemographqlApplication {
public static void main(String[] args) {
SpringApplication.run(DemographqlApplication.class, args);
}
@Bean
Instrumentation instrumentation() {
return new TracingInstrumentation();
}
}
resources/graphql/schema.graphqls
schema {
query: Query
}
type Query {
allLinks: [Link]
}
type Link {
id: ID!
url: String!
description: String
postedBy: User
}
type User {
id: ID!
name: String!
}
用户
和链接
的实体和存储库
解析器和查询解析器:
@Component
public class LinkResolver implements GraphQLResolver<Link> {
@Autowired
private final UserRepository userRepository;
LinkResolver(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User postedBy(Link link) {
if (link.getUserId() == null) {
return null;
}
return userRepository.findOne(link.getUserId());
}
}
@Component
public class Query implements GraphQLQueryResolver {
private final LinkRepository linkRepository;
@Autowired
public Query(LinkRepository linkRepository) {
this.linkRepository = linkRepository;
}
public List<Link> allLinks() {
return linkRepository.findAll());
}
}
build.gradle
:
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile 'com.graphql-java:graphql-spring-boot-starter:3.10.0'
compile 'com.graphql-java:graphql-java-tools:4.3.0'
compile 'javax.xml.bind:jaxb-api:2.3.0'
compile 'com.h2database:h2'
// to embed GraphiQL tool
compile 'com.graphql-java:graphiql-spring-boot-starter:3.10.0'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
// Use MySQL Connector-J
compile 'mysql:mysql-connector-java'
}
谢谢。
最佳答案
我今天也遇到了同样的问题。我打开源代码来了解问题所在。
该问题与graphql-java-servlet的版本有关。 4.6.0 的 GraphQLServlet.java 代码片段:
final ExecutionResult executionResult = newGraphQL(schema).execute(new ExecutionInput(query, operationName, context, rootObject, transformVariables(schema, query, variables)));
final List<GraphQLError> errors = executionResult.getErrors();
final Object data = executionResult.getData();
final String response = getMapper().writeValueAsString(createResultFromDataAndErrors(data, errors));
executionResult 包含扩展 (executionResult#getExtensions),因此 TracingInstrumentation 完成了这项工作。但正如您所看到的,响应仅考虑数据和错误(但不考虑扩展)。这就是跟踪检测值不会添加到响应中的原因。
如果您查看 graphql-java-servlet ( https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLServlet.java ) 的分支 master,您将看到扩展已被考虑在内。不知道这个版本是否已经发布到mavencentral了。
在我的项目中,我使用了 graphql-spring-boot-starter 3.10.0,并且我刚刚在我的 gradle 配置中添加了正确的依赖项:
compile "com.graphql-java:graphql-java-servlet:4.7.0"
现在可以了:我可以使用 GraphiQL 或更好的 GraphQL Plauyground 1.4.2 来跟踪所有信息
希望对你有帮助
关于java - 将 apollo 跟踪添加到 GraphQL Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48273780/