GithubHelp home page GithubHelp logo

graphql-client's Introduction

English

Graphql Client

The project is graphql client for java,support custom query and mutation.

The current version only supports post requests

You need java1.8 and maven.

Welcome to my home page:Mount Cloud

Update plan V2.0,Update time:Recent months

1:Support proxy setting.

2:Support subquery.

3:Support multiple query.

4:More flexible expansion.

Update 1.2 Note

Requested parameters support custom complex types and Enum types.

Use You Project

maven:

<dependency>
	    <groupId>org.mountcloud</groupId>
	    <artifactId>graphql-client</artifactId>
	    <version>1.2</version>
</dependency>

Insall Project

mvn install

Demo

do query

//crate client
GraphqlClient client = GraphqlClient.buildGraphqlClient("http://localhost:8081/graphql");
//create http headers
Map<String,String> headers = new HashMap<String,String>();
headers.put("token","123");
client.setHttpHeaders(headers);
//create query
GraphqlQuery query = new DefaultGraphqlQuery("findUsers");
//add query or mutation param
query.addParameter("sex","man").addParameter("age",11);
//add query response basics attribute
query.addResultAttributes("id","name","age","sex");
//add query complex attributes
ResultAttributtes classAttributte = new ResultAttributtes("class");
classAttributte.addResultAttributes("name","code");
//attributes can be more complex
ResultAttributtes schoolAttributte = new ResultAttributtes("school");
schoolAttributte.addResultAttributes("name");
//class add school attribute
classAttributte.addResultAttributes(schoolAttributte);
//do query
try {
    GraphqlResponse response = client.doQuery(query);

    //this map is graphql result
    Map data = response.getData();

} catch (IOException e) {
    e.printStackTrace();
}

query is

query{
  findUsers(sex:"man",age:11){
    id
    name
    age
    sex
    class{
    	name
	code
	school{
	  name
	}
    }
  }
}

do mutation

//crate client
GraphqlClient client = GraphqlClient.buildGraphqlClient("http://localhost:8081/graphql");
//create http headers
Map<String,String> headers = new HashMap<String,String>();
headers.put("token","123");
client.setHttpHeaders(headers);
//create mutaion
GraphqlMutation mutation = new DefaultGraphqlMutation("updateUser");
//create param
mutation.addParameter("id",1).addParameter("name","123").addParameter("age",18);
//add more complex attribute to see do query demo

//result
mutation.addResultAttributes("code");
try {
    GraphqlResponse response = client.doMutation(mutation);
    //this map is graphql result
    Map data = response.getData();
} catch (IOException e) {
    e.printStackTrace();
}

mutation is

mutation{
  updateUser(id:1,name:"123",age:18){
    code
  }
}

Complex structure request demo

Mutation demo,The query is consistent with the mutation.

    @Test
    public void testObjectParameter() throws IOException {


        
        String serverUrl = "http://localhost:8080/graphql";
        
        GraphqlClient graphqlClient = GraphqlClient.buildGraphqlClient(serverUrl);

        
        Map<String,String> httpHeaders = new HashMap<>();
        httpHeaders.put("token","graphqltesttoken");
        
        graphqlClient.setHttpHeaders(httpHeaders);

        GraphqlMutation mutation = new DefaultGraphqlMutation("addUser");

        List<User> users = new ArrayList<>();
        users.add(new User("tim",SexEnum.M));
        users.add(new User("sdf",SexEnum.F));
        mutation.getRequestParameter().addParameter("classId","123").addObjectParameter("users",users);

        mutation.addResultAttributes("code");

        System.out.println(mutation.toString());

    }

    /**
     * test user
     */
    class User{
        public User(String name, SexEnum sexEnum) {
            this.name = name;
            this.sexEnum = sexEnum;
        }

        private String name;
        private SexEnum sexEnum;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public SexEnum getSexEnum() {
            return sexEnum;
        }

        public void setSexEnum(SexEnum sexEnum) {
            this.sexEnum = sexEnum;
        }
    }

    /**
     * test user sex
     */
    enum SexEnum{
        M,
        F
    }

mutation is

mutation{
  addUser(classId:"123",users:[{name:"tim",sexEnum:M},{name:"sdf",sexEnum:F}]){
    code
  }
}

中文

Graphql Client

该项目是java的graphql客户端,支持自定义query和mutation.

当前版本仅支持post请求

你需要java1.8和maven.

欢迎观临我的主页:Mount Cloud

更新预览V2.0,预计更新时间:最近几个月不忙的时候

1:支持代理服务器.

2:支持子查询.

3:支持多个查询组合.

4:更加灵活的扩展方式.

更新 1.2 日志

请求的参数支持自定义复杂类型和Enum类型。

使用方式

maven:

<dependency>
	    <groupId>org.mountcloud</groupId>
	    <artifactId>graphql-client</artifactId>
	    <version>1.2</version>
</dependency>

Insall Project

mvn install

Demo

do query

//crate client
GraphqlClient client = GraphqlClient.buildGraphqlClient("http://localhost:8081/graphql");
//create http headers
Map<String,String> headers = new HashMap<String,String>();
headers.put("token","123");
client.setHttpHeaders(headers);
//create query
GraphqlQuery query = new DefaultGraphqlQuery("findUsers");
//add query or mutation param
query.addParameter("sex","man").addParameter("age",11);
//add query response basics attribute
query.addResultAttributes("id","name","age","sex");
//add query complex attributes
ResultAttributtes classAttributte = new ResultAttributtes("class");
classAttributte.addResultAttributes("name","code");
//attributes can be more complex
ResultAttributtes schoolAttributte = new ResultAttributtes("school");
schoolAttributte.addResultAttributes("name");
//class add school attribute
classAttributte.addResultAttributes(schoolAttributte);
//do query
try {
    GraphqlResponse response = client.doQuery(query);

    //this map is graphql result
    Map data = response.getData();

} catch (IOException e) {
    e.printStackTrace();
}

query is

query{
  findUsers(sex:"man",age:11){
    id
    name
    age
    sex
    class{
    	name
	code
	school{
	  name
	}
    }
  }
}

do mutation

//crate client
GraphqlClient client = GraphqlClient.buildGraphqlClient("http://localhost:8081/graphql");
//create http headers
Map<String,String> headers = new HashMap<String,String>();
headers.put("token","123");
client.setHttpHeaders(headers);
//create mutaion
GraphqlMutation mutation = new DefaultGraphqlMutation("updateUser");
//create param
mutation.addParameter("id",1).addParameter("name","123").addParameter("age",18);
//add more complex attribute to see do query demo

//result
mutation.addResultAttributes("code");
try {
    GraphqlResponse response = client.doMutation(mutation);
    //this map is graphql result
    Map data = response.getData();
} catch (IOException e) {
    e.printStackTrace();
}

mutation is

mutation{
  updateUser(id:1,name:"123",age:18){
    code
  }
}

如何实现一个复杂请求

用Mutation做演示,query与mutation原理一样.

    @Test
    public void testObjectParameter() throws IOException {


        
        String serverUrl = "http://localhost:8080/graphql";
        
        GraphqlClient graphqlClient = GraphqlClient.buildGraphqlClient(serverUrl);

        
        Map<String,String> httpHeaders = new HashMap<>();
        httpHeaders.put("token","graphqltesttoken");
        
        graphqlClient.setHttpHeaders(httpHeaders);

        GraphqlMutation mutation = new DefaultGraphqlMutation("addUser");

        List<User> users = new ArrayList<>();
        users.add(new User("tim",SexEnum.M));
        users.add(new User("sdf",SexEnum.F));
        mutation.getRequestParameter().addParameter("classId","123").addObjectParameter("users",users);

        mutation.addResultAttributes("code");

        System.out.println(mutation.toString());

    }

    /**
     * test user
     */
    class User{
        public User(String name, SexEnum sexEnum) {
            this.name = name;
            this.sexEnum = sexEnum;
        }

        private String name;
        private SexEnum sexEnum;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public SexEnum getSexEnum() {
            return sexEnum;
        }

        public void setSexEnum(SexEnum sexEnum) {
            this.sexEnum = sexEnum;
        }
    }

    /**
     * test user sex
     */
    enum SexEnum{
        M,
        F
    }

mutation is

mutation{
  addUser(classId:"123",users:[{name:"tim",sexEnum:M},{name:"sdf",sexEnum:F}]){
    code
  }
}

graphql-client's People

Contributors

crazyahai avatar crazyahaienterprise avatar dependabot[bot] avatar mountcloud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

graphql-client's Issues

Demo的例子有问题

漏了这一行代码:query.addResultAttributes(classAttributte);
classAttributte 并没有被添加进 query的ResultAttributes里面

应该加在:ResultAttributtes classAttributte= new ResultAttributtes("class");这一行的下面

How to execute multi query in one request

Hope to execute multi query in one request, but I found that in one DefaultGraphqlQuery and GraphqlClient only one query can be executed, how could I execute multi query in one request?

Such as the following query:

query{
  findUsers(sex:"man",age:11){
    id
    name
    age
    sex
    class{
    	name
	code
	school{
	  name
	}
    }
  }
findUsers1(sex:"man",age:11){
    id
    name
    age
    sex
    class{
    	name
	code
	school{
	  name
	}
    }
  }
}

复杂参数的查询

@MountCloud
你好请部复杂参数如何查询?
{
pair(id: 50, block:{number:100}) {
id
}
}
这个 block:{number:100}这块参数要怎么传递,多谢

当返回类型是String或者Long类型时报错

如果填了query.addResultAttributes("XXX");会报错返回如下信息:
Validation error of type SubSelectionNotAllowed: Sub selection not allowed on leaf type Long!

如果不填query.addResultAttributes("XXX");会报错返回如下信息:
Invalid Syntax : offending token '}' at line 1 column 187

input格式如何支持?

usesByIds(input: { userIds: $userIds, type: $type }) {
        id
        name
        createdAt
    }

这种input格式如何支持?

Filter Support In GraphQL Query

Hi Team,

We are consuming data from Pluralsight, where we have to filter the data on the basis of startDate and endDate.
That support is not available in this library, I would apricate you add that also into this library.

query Complex { courseDailyUsage(filter:{ startDate:"2021-01-06T00:00:00.000Z", endDate:"2021-09-07T00:00:00.000Z" }){ totalCount pageInfo{ hasNextPage endCursor }

关于请求返回on Type{}怎么用?

比如请求,这个工具怎么用?

mutation a($demo: String!) {
  updateCustomer(demo: $demo) {
    ... on MutationSuccess {
      result
    }
    ... on MutationError {
      errorCode
    }
  }
}

how to query like this

query {
repository(name: "") {
comparison(base:"",head:""){
fileDiffs{
nodes{
hunks{
body
}
}
}
}
}
}
如何支持这种查询,目前GraphqlQuery好像无法嵌套?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.