Last active
April 12, 2018 08:54
-
-
Save hpstuff/9771ca3d443f1be9a7c907ee099030da to your computer and use it in GitHub Desktop.
graphql-java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Link { | |
url: String! | |
description: String! | |
} | |
type Query { | |
allLinks: [Link] | |
} | |
schema { | |
query: Query | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Link { | |
private final String url; | |
private final String description; | |
public Link(String url, String description) { | |
this.url = url; | |
this.description = description; | |
} | |
public String getUrl() { | |
return url; | |
} | |
public String getDescription() { | |
return description; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Query implements GraphQLRootResolver { | |
private final LinkRepository linkRepository; | |
public Query(LinkRepository linkRepository) { | |
this.linkRepository = linkRepository; | |
} | |
public List<Link> allLinks() { | |
return linkRepository.getAllLinks(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.