{"id":246280,"date":"2016-10-13T06:57:16","date_gmt":"2016-10-13T13:57:16","guid":{"rendered":"http:\/\/css-tricks.com\/?p=246280"},"modified":"2017-04-06T16:55:37","modified_gmt":"2017-04-06T23:55:37","slug":"declarative-data-fetching-graphql","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/declarative-data-fetching-graphql\/","title":{"rendered":"Declarative Data Fetching with GraphQL"},"content":{"rendered":"
The following is a guest post by Nilan Marktanner from Graph.cool<\/a>. I don’t know about y’all but I’ve spent plenty of time in my career dealing with REST API’s. It’s a matter of always trying to figure out what URL to hit, what data to expect back, and how you can control that data. A quick glance at GraphQL makes it seem like it simplifies things both for the creators and consumers of the API. Let’s let Nilan explain.<\/em><\/p>\n <\/p>\n When it comes to standards for designing the API architecture for web services of all kinds, REST has been the state of the art for many years already. With the widespread popularity of REST, GraphQL’s claim to replace it as the API bridge between front end and back end has received a lot of scepticism. But GraphQL is already very well established, as evidenced by Facebook’s announcement of GraphQL being production ready<\/a> and GitHub’s reveal of their GitHub GraphQL API<\/a>.<\/p>\n Like with REST, several components are needed before we can use a GraphQL API.<\/p>\n In this article we will explore a possible GraphQL API for an Instagram clone highlighting some of the benefits that GraphQL brings to the table.<\/p>\n The endpoints of a REST API are often reminiscent of the actual schema of the database. Endpoints like The GraphQL Schema however needs to be built as a type system in a two step process. <\/p>\n Object types are composed using primitive types like String and Boolean. In our case, we will build the We have a Additionally to the fields mentioned above, both object types have the These types are then used to define actual queries, like the The type system encoded in the GraphQL schema paves the way for powerful tools like GraphiQL<\/a> for example, which is maintained by Facebook. It allows the playful exploration of a GraphQL API.<\/p>\nThe Infrastructure Required for GraphQL<\/h3>\n
\n
allUsers<\/code> to fetch all users might be mapped to a SQL query like
SELECT * FROM USERS<\/code>.<\/li>\n<\/ul>\n
The GraphQL Schema<\/h3>\n
\/users<\/code> or
\/posts<\/code> that enable access to the database tables
User<\/code> and
Post<\/code> are common.<\/p>\n
Step 1<\/h4>\n
User<\/code> and the
Post<\/code> object types described in IDL syntax:<\/p>\n
type User {\r\n id: String!\r\n name: String\r\n posts: [Post]\r\n}\r\n\r\ntype Post {\r\n id: String!\r\n imageUrl: String\r\n description: String\r\n author: User\r\n}<\/code><\/pre>\n
User<\/code> object type that consists of the fields
name<\/code> of type String and
posts<\/code> of list type
Post<\/code> (denoted by [Post]) and the
Post<\/code> object type that consists of a
imageUrl<\/code> and a
description<\/code> field of type String and an
author<\/code> field of type
User<\/code>.<\/p>\n
id<\/code> field which is a required String (denoted by
String!<\/code>).<\/p>\n
Step 2<\/h4>\n
allUsers<\/code> query that can be used to fetch all existing users in the database. You can basically expose whatever you come up with, but most popular are queries to fetch all or one item of a specific type and so called mutations to create, update and delete items of a specific type.<\/p>\n
Tooling<\/h3>\n