【问题标题】:adding apollo tracing to GraphQL Spring将 apollo 跟踪添加到 GraphQL Spring
【发布时间】:2018-06-24 17:18:06
【问题描述】:

我正在尝试使用 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。 并且该 bean 也被 GraphQLWebAutoConfiguration 拾取。

我尝试调试并在 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!
}

UserLink 的实体和存储库

解析器和查询解析器:

@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'
}

谢谢。

【问题讨论】:

    标签: java spring spring-boot graphql graphql-java


    【解决方案1】:

    我今天也面临同样的问题。我打开了源代码以了解问题所在。
    此问题与 graphql-java-servlet 的版本有关。 GraphQLServlet.java for 4.6.0的代码sn-p:

    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,您会发现扩展已被考虑在内。不知道这个版本有没有发布到maven Central。

    在我的项目中,我使用了 graphql-spring-boot-starter 3.10.0,我只是在我的 gradle 配置中添加了正确的依赖项:

        compile "com.graphql-java:graphql-java-servlet:4.7.0"
    

    现在它可以工作了:我可以在 GraphiQL 中跟踪所有信息,或者使用 GraphQL Plauyground 1.4.2 更好地跟踪信息

    希望对你有帮助

    【讨论】:

    • 哦,我明白了。我一直在跟踪它,直到 TracingInstrument 被添加到 GraphqlServlet,然后停在那里。没有意识到结果在那之后被忽略了。真的很有帮助,谢谢
    猜你喜欢
    • 2018-06-28
    • 2019-12-08
    • 1970-01-01
    • 2021-04-27
    • 2021-01-04
    • 1970-01-01
    • 2014-09-12
    • 2021-06-17
    • 2017-10-09
    相关资源
    最近更新 更多