Как генерировать клиентские запросы GraphQL из файлов .graphql через com.graphql-java - PullRequest
0 голосов
/ 20 сентября 2018

Я исследую GraphQL с помощью Java, используя: -

// 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 файл определяет мои запросы.

Iразработал следующий код для объединения всех моих файлов .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());

Чего мне не хватает?

...