Skip to content

Instantly share code, notes, and snippets.

@hpstuff
Last active April 12, 2018 08:54
Show Gist options
  • Save hpstuff/9771ca3d443f1be9a7c907ee099030da to your computer and use it in GitHub Desktop.
Save hpstuff/9771ca3d443f1be9a7c907ee099030da to your computer and use it in GitHub Desktop.
graphql-java
type Link {
url: String!
description: String!
}
type Query {
allLinks: [Link]
}
schema {
query: Query
}
public class LinkRepository {
private final List<Link> links;
public LinkRepository() {
links = new ArrayList<>();
//add some links to start off with
links.add(new Link("http://howtographql.com", "Your favorite GraphQL page"));
links.add(new Link("http://graphql.org/learn/", "The official docks"));
}
public List<Link> getAllLinks() {
return links;
}
public void saveLink(Link link) {
links.add(link);
}
}
public class Query implements GraphQLRootResolver {
private final LinkRepository linkRepository;
public Query(LinkRepository linkRepository) {
this.linkRepository = linkRepository;
}
public List<Link> allLinks() {
return linkRepository.getAllLinks();
}
}
@hpstuff
Copy link
Author

hpstuff commented Apr 12, 2018

Benefits of GraphQL

Schema/type system.

GraphQL requires a user-defined schema built using a strict type system. This schema acts as a blueprint for the data handled by the API, allowing developers to know exactly what kind of data is available and in what format. Developer tools can take advantage of the schema through the use of introspection, enabling documentation, query auto-complete and mocking tools.

Request only data needed.

Since GraphQL queries can define what data exactly is needed at query time, all the data required to render an application view can be requested in a single request, reducing the number of roundtrip network requests and with that, the application latency.

Wrapping existing data services.

GraphQL can work with any database or data layer, allowing for a single GraphQL service to fetch data from multiple sources in a single request served by the composed API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment