首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建使用NetFlix DGS客户端的控制台应用程序

如何创建使用NetFlix DGS客户端的控制台应用程序
EN

Stack Overflow用户
提问于 2021-08-10 17:31:00
回答 1查看 359关注 0票数 2

我想使用来自NetFlix的漂亮的NetFlix来对GraphQL服务进行客户端调用。

我发现的所有示例都假设我正在构建一个服务,而Java GraphQL客户机是作为一种事后考虑而引入的。

我想从其中调用服务的应用程序只是一个控制台应用程序。它不需要web服务器,也不需要dgsQueryExecutor或模式。然而,当我运行我的应用程序时,它会发现大量的运行时错误是必需的。

我是Java/Maven/SpringBoot世界中的一个相对新手,我仍然在挣扎于在没有我要求的情况下启动事物的概念,以及消除依赖关系的概念。

这是我的pom.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>io.m.t</groupId>
    <artifactId>t-l-g-m</artifactId>
    <version>${revision}</version>

    <properties>
        <revision>1.0.0-SNAPSHOT</revision>
        <java.version>11</java.version>
        <maven.compiler.release>${java.version}</maven.compiler.release>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.4</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.graphql.dgs</groupId>
            <artifactId>graphql-dgs-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.netflix.graphql.dgs</groupId>
                <artifactId>graphql-dgs-platform-dependencies</artifactId>
                <!-- The DGS BOM/platform dependency. This is the only place you set version of DGS -->
                <version>4.5.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这是我的主要类(注意:它实际上还没有做任何事情,我只是希望它没有错误地运行)。

代码语言:javascript
复制
package io.m.t.l.g;

import io.m.t.l.g.config.DatabaseConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

/**
 * 1. Act as main class for spring boot application
 * 2. Also implements CommandLineRunner, so that code within run method
 * is executed before application startup but after all beans are effectively created
 */
@SpringBootApplication
@Slf4j
public class MyConsoleApplication implements CommandLineRunner {

    public static void main(String[] args) {
        log.info("STARTING THE APPLICATION");
        new SpringApplicationBuilder(MyConsoleApplication.class)
                .web(WebApplicationType.NONE)
                .run(args);
        // NOTE: different from the usual to prevent Tomcat from starting (not needed in console app)
        //SpringApplication.run(MyConsoleApplication.class, args);
        log.info("APPLICATION FINISHED");
    }

    /**
     * This method will be executed after the application context is loaded and
     * right before the Spring Application main method is completed.
     */
    @Override
    public void run(String... args) {

        log.info("For now, I just want to get to this line without runtime errors.");
    }        
}
EN

回答 1

Stack Overflow用户

发布于 2022-04-03 07:23:04

Spring控制台应用程序,Netflix客户端:

代码语言:javascript
复制
package org.example;

import com.netflix.graphql.dgs.client.GraphQLClient;
import com.netflix.graphql.dgs.client.GraphQLResponse;
import com.netflix.graphql.dgs.client.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;


@Configuration
@SpringBootApplication
public class Application implements CommandLineRunner {
    private static final Logger LOG = LoggerFactory.getLogger(Application.class);
    private static final String ENDPOINT = "https://graphql-weather-api.herokuapp.com/";

    @Bean
    GraphQLClient graphQLClient() {
        RestTemplate restTemplate = new RestTemplate(); // underlying http client for blocking calls
        // for reactive calls use MonoGraphQLClient

        return GraphQLClient.createCustom(ENDPOINT,
                (url, headers, body) -> {
                    HttpHeaders httpHeaders = new HttpHeaders();
                    headers.forEach(httpHeaders::addAll);
                    ResponseEntity<String> exchange =
                            restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(body, httpHeaders), String.class);
                    return new HttpResponse(exchange.getStatusCodeValue(), exchange.getBody());
                });
    }

    @Autowired
    private GraphQLClient client;

    public static void main(String[] args) {
        LOG.info("started");
        SpringApplication.run(Application.class, args);
        LOG.info("finished");
    }

    @Override
    public void run(String... args) {
        GraphQLResponse response = client.executeQuery("query { getCityById(id: \"2729907\") { name } }");
        String name = response.extractValueAsObject("getCityById[0].name", String.class);
        LOG.info("got city name: " + name);
    }
}

application.yml:

代码语言:javascript
复制
spring.main.web-application-type: NONE

pom.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>gqlclient</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
    </parent>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <!-- required for RestTemplate -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-reactor-netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.netflix.graphql.dgs</groupId>
            <artifactId>graphql-dgs-client</artifactId>
            <version>4.9.24</version>
        </dependency>
    </dependencies>
</project>

希望能帮上忙。

我还建议接下来获取远程模式并将其用于类型生成(类型安全查询和dtos)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68731196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档