2

我正在尝试使用 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!
}

User和的实体和存储库Link

解析器和查询解析器:

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

谢谢你。

4

1 回答 1

4

我今天面临同样的问题。我打开源代码以了解问题所在。
此问题与 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 的分支 master ( https://github.com/graphql-java/graphql-java-servlet/blob/master/src/main/java/graphql/servlet/GraphQLServlet。 java ),您将看到扩展被考虑在内。我不知道这个版本是否已经发布到 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 更好地跟踪信息

希望能帮助到你

于 2018-01-31T17:18:08.473 回答