我使用graphq框架开发了一个带有spqr的应用程序,下面给出了依赖关系。我的春季启动应用程序有一个上下文路径说, server.servlet路径= / 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其中&#34; asdfg&#34;是我的应用程序上下文路径但是在graphIQL UI中,我无法像通常显示的那样在文档下看到模式。我找到的原因是虽然graphIQL UI使用/ graphql进行POST调用但它没有在路径上添加上下文。这是POST http://localhost:8080/graphql而不是POST http://localhost:8080/asdfg/graphql。
如果我从spring应用程序中删除上下文路径,则应用程序运行良好。有没有办法在POST http://localhost:8080/graphql调用中包含上下文路径?
答案 0 :(得分:0)
也许您应该更改*.graphqls
文件?
mapping: /graphql --> mapping: /graphql
发件人:
graphql:
servlet:
mapping: /graphql
收件人:
graphql:
servlet:
mapping: /asdfg/graphql