如何通过com.graphql-java从.graphql文件生成客户端GraphQL查询

时间:2018-09-20 10:55:16

标签: graphql graphql-java

我正在使用:-

用Java研究GraphQL
// https://mvnrepository.com/artifact/com.graphql-java/graphql-java-tools
compile group: 'com.graphql-java', name: 'graphql-java-tools', version: '5.2.4'

我有多个.graphql文件定义了我的不同类型,而一个.graphql文件定义了我的查询。

我已经开发了以下代码来合并所有.graphql文件并实例化可执行模式

public static void main(final String[] args) throws IOException, URISyntaxException {

    final List<Path> paths = new ArrayList<>();

    Files.list(Paths.get("schemas")).filter(path -> path.toString().endsWith(".graphql")).forEach(paths::add);

    wire(paths);

}

private static void wire(final List<Path> paths) {

    final SchemaParser schemaParser = new SchemaParser();
    final SchemaGenerator schemaGenerator = new SchemaGenerator();

    final TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
    for (final Path path : paths) {
        typeRegistry.merge(schemaParser.parse(path.toFile()));
    }

    final RuntimeWiring wiring = buildRuntimeWiring();
    final GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, wiring);

    final GraphQLObjectType queries = graphQLSchema.getQueryType();
    final ObjectTypeDefinition objectTypeDefinition = queries.getDefinition();

}

private static RuntimeWiring buildRuntimeWiring() {
    return RuntimeWiring.newRuntimeWiring().wiringFactory(new EchoingWiringFactory()).build();
}

希望实现的是生成实际的GraphQL查询文本字符串,例如:-

{
   "operationName":"testQuery",
   "variables":{
      "filters":"",
      "unread":false,
      "offset":20,
      "limit":20
   },
   "query":"query testQuery($filters: String, $unread: Boolean, $source: String, $sourceId: ID, $offset: Int, $limit: Int) {\n  testQuery(keywordFilter: $filters, unreadFilter: $unread, sourceFilter: $source, sourceIdFilter: $sourceId, offset: $offset, limit: $limit) {\n    info {\n      totalCount\n      __typename\n    }\n    contents {\n      uuid\n      bookmarked\n      removed\n      title\n      authors {\n        first_name\n        last_name\n        __typename\n      }\n      uuid\n      abstract\n      doi\n      year\n      issue\n      volume\n      pages\n      journal_name\n      publisher\n      new\n      __typename\n    }\n    __typename\n  }\n}\n"
}

可以使用graphql-java-tools吗?

使用此代码,我可以看到我的查询包含在可执行模式对象中

    final GraphQLObjectType queries = graphQLSchema.getQueryType();
    final ObjectTypeDefinition objectTypeDefinition = queries.getDefinition();

    System.out.println("queries.getDefinition() = " + objectTypeDefinition);
    System.out.println("queries.getFieldDefinitions() = " + queries.getFieldDefinitions());

我想念什么?

0 个答案:

没有答案