java调用Graphql接口

java调用Graphql接口

最近项目调用第三方的接口,但是第三方接口是GraphQL,刚开始看见这个一脸懵逼,竟然是自己没有用过的语言,这时候就开始去网上找资料学习一下。

1.什么是GraphQL?

GraphQL 是一种针对 Graph(图状数据)进行查询特别有优势的 Query Language(查询语言),所以叫做 GraphQL。它跟 SQL 的关系是共用 QL 后缀,就好像「汉语」和「英语」共用后缀一样,但他们本质上是不同的语言。

GraphQL 跟用作存储的 NoSQL 没有必然联系,虽然 GraphQL 背后的实际存储可以选择 NoSQL 类型的数据库,但也可以用 SQL 类型的数据库,或者任意其它存储方式(例如文本文件、存内存里等等)。GraphQL 最大的优势是查询图状数据。GraphQL 是 Facebook 发明的。

  1. java什么调用Graphql接口?

 首先需要引入jar到自己项目的pom文件里面

<dependency>

    <groupId>org.mountcloud</groupId>

    <artifactId>graphql-client</artifactId>

    <version>1.2</version>

</dependency>

在用java实现调用Graphql时,我们首先可以尝试用postman工具调一下第三方接口,看看是不是可以调通,调用结果如下:

 

 

注:设置请求的头部

注:上面三个记得要注意,2和3就是请求参数,下面的就是返回结果,说明是可以调成功。

最后我们用java代码实现调用如下:

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;

import org.mountcloud.graphql.GraphqlClient;

import org.mountcloud.graphql.request.mutation.DefaultGraphqlMutation;

import org.mountcloud.graphql.request.mutation.GraphqlMutation;

import org.mountcloud.graphql.request.result.ResultAttributtes;

import org.mountcloud.graphql.response.GraphqlResponse;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

@Slf4j

public class GraphqlTestDemo {

    public static void main(String[] args) {

        String token = "**********";

        String authorizationToken = "eyJhbGciOiJSU";

        String url = "https://api.staging.transit.dev/graphql/transact";

        GraphqlClient graphqlClient = GraphqlClient.buildGraphqlClient(url);

        Map<String,String> httpHeaders = new HashMap<>();

        httpHeaders.put("content-type", "application/json");

        httpHeaders.put("authorization", "Bearer "+authorizationToken);

        graphqlClient.setHttpHeaders(httpHeaders);

        //创建mutaion

        GraphqlMutation mutation = new DefaultGraphqlMutation("transact");

        //创建参数对象

        Map<String,Object> input = new HashMap<String,Object>();

        input.put("ref", "test/REDEEMED");

        input.put("dry", true);

        input.put("locationID", "35872673");

        input.put("hostID", "865650030605402");

        input.put("token", token);

        //添加变量名

        mutation.getRequestParameter().addObjectParameter("input", input);

        //设置获取返回结果参数

        mutation.addResultAttributes("ref");

        ResultAttributtes resultAttributtes = new ResultAttributtes("voucher");

        resultAttributtes.addResultAttributes("status","transactionID", "transactionTimestamp", "value");

        mutation.addResultAttributes(resultAttributtes);

        log.info("---------请求参数-----------mutation:{}", mutation.toString());

        GraphqlResponse response = null;

        try {

            response = graphqlClient.doMutation(mutation);

        } catch (IOException e) {

            e.printStackTrace();

        }

        Map data = response.getData();

        log.info("---------返回结果---------data:{}", JSON.toJSONString(data));

    }

}

注:主要就是postman图片中2的请求参数的组装!!!

graphql-java 是 GraphQLJava 实现。这个库的目标是用于真实的生产环境。graphql-java 解析和执行查询 GraphQL 。它并不真正获取任何数据的:数据来源于执行回调或提供静态数据。graphql-java 的 "hello world":import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import static graphql.Scalars.GraphQLString; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLObjectType.newObject; public class HelloWorld {     public static void main(String[] args) {         GraphQLObjectType queryType = newObject()                         .name("helloWorldQuery")                         .field(newFieldDefinition()                                 .type(GraphQLString)                                 .name("hello")                                 .staticValue("world")                                 .build())                         .build();         GraphQLSchema schema = GraphQLSchema.newSchema()                         .query(queryType)                         .build();         Map result = new GraphQL(schema).execute("{hello}").getData();         System.out.println(result);         // Prints: {hello=world}     } } 标签:graphql
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值