【问题标题】:grpahiql not using application context path for the /graphqlgrpahiql 不使用 /graphql 的应用程序上下文路径
【发布时间】:2018-11-27 15:38:41
【问题描述】:

我已经使用带有 spqr 的 graphql 框架开发了一个应用程序,下面给出了依赖项。我的 Spring Boot 应用程序有一个上下文路径说, server.servlet-path=/asdfg

    <dependency>
        <groupId>io.leangen.graphql</groupId>
        <artifactId>spqr</artifactId>
        <version>0.9.7</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-spring-boot-starter</artifactId>
        <version>4.2.0<</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java-tools</artifactId>
        <version>5.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphiql-spring-boot-starter</artifactId>
        <version>4.2.0<</version>
    </dependency>

该应用程序是一个 Java / Spring Boot 应用程序,其中我的终点是 PersonQuery - 此处显示

@Component
public class PersonQuery {
    @GraphQLQuery(name = "getPersons")
    public List<Person> getFirstNPersons(@GraphQLArgument(name = "count") int count){
        List<Person> result = new ArrayList<>();
        ...
        return result.stream().limit(count).collect(Collectors.toList());
    }
}

现在在应用程序启动时,我加载 personQuery 然后构建 Schema。 (我使用 spqr 并且没有带有类型定义的 .graphqls 文件,但它们已在上面进行了注释)带有 @RestController 类。

@RestController
public class GraphQLController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GraphQLController.class);
    private final GraphQL graphQL;
    @Autowired
    public GraphQLController(PersonQuery personQuery) {
        // Schema generated from query classes
        GraphQLSchema schema = new GraphQLSchemaGenerator().withResolverBuilders(
                // Resolve by annotations
                new AnnotatedResolverBuilder(),
                // Resolve public methods inside root package
                new PublicResolverBuilder("com.xyz.asd.services"))
                // Query Resolvers
                .withOperationsFromSingleton(personQuery)
                .withValueMapperFactory(new JacksonValueMapperFactory()).generate();
        graphQL = GraphQL.newGraphQL(schema).build();
    }

    @PostMapping(value = "/graphql", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Map<String, Object> indexFromAnnotated(@RequestBody Map<String, String> request, HttpServletRequest raw) {
        ExecutionResult executionResult = graphQL.execute(ExecutionInput.newExecutionInput().query(request.get("query"))
                .operationName(request.get("operationName")).context(raw).build());
        return executionResult.toSpecification();
    }

上面的代码足以调出http://localhost:8080/asdfg/graphiql,其中“asdfg”是我的应用程序上下文路径。但是在 graphIQL UI 中,我看不到通常显示的文档下的架构。我发现的原因是虽然 graphIQL UI 使用 /graphql 进行了 POST 调用,但它没有在路径上添加上下文。那是 POST http://localhost:8080/graphql 而不是 POST http://localhost:8080/asdfg/graphql

如果我从 spring 应用程序中删除上下文路径,应用程序运行良好。有没有办法可以在 POST http://localhost:8080/graphql 调用中包含上下文路径?

【问题讨论】:

  • 您能否更具体地说明您需要哪些帮助?您能否为您的问题提供更多背景信息?
  • 谢谢。我用更多的代码 sn-ps 和细节更新了原始线程。

标签: graphql graphiql


【解决方案1】:

也许您应该更改您的*.graphqls 文件?

mapping: /graphql --> mapping: /graphql

发件人:

graphql:
  servlet:
    mapping: /graphql

收件人:

graphql:
      servlet:
        mapping: /asdfg/graphql

【讨论】:

    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2011-11-14
    • 2016-07-04
    • 2013-07-16
    相关资源
    最近更新 更多