Skip to content

Instantly share code, notes, and snippets.

@smilingleo
Last active June 12, 2017 02:27
Show Gist options
  • Save smilingleo/89a07aa64e3aeb88fca72fea59d09804 to your computer and use it in GitHub Desktop.
Save smilingleo/89a07aa64e3aeb88fca72fea59d09804 to your computer and use it in GitHub Desktop.
GraphQL java client example, use batch data fetcher for performance optimization.
package leo.me.graphql;
/**
* Created by leo on 4/26/17.
*/
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLList.list;
import static graphql.schema.GraphQLObjectType.newObject;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import graphql.GraphQL;
import graphql.execution.batched.Batched;
import graphql.execution.batched.BatchedExecutionStrategy;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
public class GraphQLBatchFetcher {
public static void main(String[] args) {
// optimized for performance
// this get() method is invoked only once
DataFetcher batchFetcher = new DataFetcher() {
@Batched
public Object get(DataFetchingEnvironment environment) {
List<Team> teams = environment.getSource();
Set<String> teamIds = teams.stream().map(team -> team.getId()).collect(Collectors.toSet());
// filter contacts which teamId is in `accountIds`
Map<String, List<Member>> groupByTeamId = testMembers.stream()
.filter(member -> teamIds.contains(member.getTeamId()))
.collect(Collectors.groupingBy(Member::getTeamId, Collectors.toList()));
return Arrays.asList(groupByTeamId.values().toArray());
}
};
// this is a fan-out fetcher, meaning for each account, the get() method will be called once.
DataFetcher fetchByTeam = new DataFetcher() {
public Object get(DataFetchingEnvironment environment) {
Team team = environment.getSource();
return testMembers.stream()
.filter(member -> member.getTeamId().equals(team.getId()))
.collect(Collectors.toList());
}
};
// schema part
GraphQLObjectType memberType = newObject()
.name("MemberType")
.field(newFieldDefinition()
.type(GraphQLString)
.name("name")
)
.field(newFieldDefinition()
.type(GraphQLString)
.name("email")
)
.build();
GraphQLObjectType teamType = newObject()
.name("TeamType")
.field(newFieldDefinition()
.type(GraphQLString)
.name("id")
)
.field(newFieldDefinition()
.type(GraphQLString)
.name("name")
)
.field(newFieldDefinition()
.type(list(memberType))
.name("members")
.dataFetcher(batchFetcher)
)
.build();
GraphQLObjectType queryType = newObject()
.name("QueryType")
.field(newFieldDefinition()
.type(list(teamType))
.name("team")
.dataFetcher(new StaticDataFetcher(testTeams))
)
.build();
GraphQLSchema schema = GraphQLSchema.newSchema()
.query(queryType)
.build();
GraphQL graphQL = GraphQL.newGraphQL(schema)
.queryExecutionStrategy(new BatchedExecutionStrategy())
.build();
Map<String, Object> result = graphQL.execute("{team { id, name, members{ name } }}").getData();
System.out.println(result);
}
// test data model
public static class Team {
private String id;
private String name;
public Team(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class Member {
private String id;
private String name;
private String teamId;
public Member(String id, String name, String teamId) {
this.id = id;
this.name = name;
this.teamId = teamId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeamId() {
return teamId;
}
public void setTeamId(String teamId) {
this.teamId = teamId;
}
}
// test data
static List<Team> testTeams = new LinkedList<>();
static {
testTeams.add(new Team("1", "Billing"));
testTeams.add(new Team("2", "Payment"));
}
static List<Member> testMembers = new LinkedList<>();
static {
testMembers.add(new Member("1", "Kevin Qu", "1"));
testMembers.add(new Member("2", "Zhen Li", "1"));
testMembers.add(new Member("3", "Yuqi Li", "2"));
testMembers.add(new Member("4", "Harry Dong", "2"));
testMembers.add(new Member("5", "Yang Yu", "2"));
testMembers.add(new Member("6", "Leo Liu", "3"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment