【问题标题】:How to call a GraphQL query/mutation with Java code如何使用 Java 代码调用 GraphQL 查询/突变
【发布时间】:2020-04-18 21:40:29
【问题描述】:

我知道我们可以使用 /graphiql 端点来测试创建或部署的 GraphQL API。

  1. 我需要使用 GraphQL API,该 API 已经使用 Java 代码。我该怎么做。
  2. 如何使用类似 postman 的应用程序测试 GraphQL 端点,或者甚至可以。

【问题讨论】:

    标签: java graphql


    【解决方案1】:

    在后端代码中使用 graphql API,需要多个步骤,例如。

    1. 定义架构
    2. 安装依赖项
    3. 定义 graphql 端点
    import com.coxautodev.graphql.tools.SchemaParser;
    import javax.servlet.annotation.WebServlet;
    import graphql.servlet.SimpleGraphQLServlet;
    
    
    @WebServlet(urlPatterns = "/graphql")
    public class GraphQLEndpoint extends SimpleGraphQLServlet {
    
        public GraphQLEndpoint() {
            super(SchemaParser.newParser()
                    .file("schema.graphqls") //parse the schema file created earlier
                    .build()
                    .makeExecutableSchema());
        }
    }
    

    这里有一个详细的指南:Graphql-java-backend-implementation

    是的,可以使用邮递员发送请求。您可以简单地复制正文中的模式。 Doing GraphQL request using Postman

    【讨论】:

    • 我也很好奇。
    • 因为问题是关于使用 GraphQL,而不是暴露端点?
    【解决方案2】:

    根据您的情况,使用 Spring RestTemplate 可能是最简单的事情。

    public class Person {
        @JsonProperty("query")
        public final String query = "mutation($name:String, $gender:String, $age:Int) { updateDetails (name: $name, gender: $gender, age: $age) }";
        @JsonProperty("variables")
        public final Map<String, String> variables;
    ...
    }
    ...
    (new RestTemplate()).postForEntity("http://example.com/graphql", new Person("David", "male", 12));
    

    通过避免 Spring 并使用 HttpClient,使用 StringBuilder 构建相同的 JSON 文档,可以更简单。

    或者Apollo Andoid 可以为您自动生成客户端代码。

    【讨论】:

    • 请更新您的答案并提供完整解释。即 Person 对象如何获取三个参数,而您只提到 Map&lt;String, String&gt;,以及 Resttemplate 类型。
    【解决方案3】:

    一个例子,为我工作...... 用户名和密码错误:) 但是,URL 没问题...

    这样,我不使用 RestTemplate、任何罐子等。

    private static void test_LivePharma() {
        try {
            URL url = new URL("https://stores-hml.funcionalmais.com/graphql");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "application/json");
    
            String input="{\"query\":\"mutation{createToken(login:\\\"112233\\\",password:\\\"112233\\\"){  token}}\\n\"}";
    
            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes());
            os.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));
            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 1970-01-01
      • 2020-06-17
      • 2022-08-02
      • 2018-11-10
      • 2019-04-04
      • 2019-06-30
      • 2021-10-14
      • 2017-10-18
      相关资源
      最近更新 更多