Multiple top-level items can be queried from the GraphQL server at once in a single http call, if the same resource is requested more than once the resource will also need to be labelled a unique name within the query, for example I’m using “ironMan” and “hulk” Using the above modified schema, I could now apply pagination to the hero’s friends and only fetch their first friend. Now lets define a slightly more complicated query as GraphQL queries can traverse related objects and dive deeper down the tree defined within the schema which allows the developer to fetch multiple related resources within one GraphQL query.įor example, fetching the hero’s friends and home world:Īs you can see in the above returned result the data is returned formatted into a tree of data, and only contains the fields we requested within the query, this means not only is this query efficient for slower connections, its also easier for a developer to parse the returned result.Īrguments can be passed into queries at any level not just the root level as defined in the above schema.įor example, we could modify our original schema to add arguments when a user queries the “friends” field under the Character data type. In GraphQL the developer can query against the schema by writing a query, this query must specify each field that is required to be returned as the GraphQL server will only return the fields listed in the query, leading to an optimised predictable result.įor example, to query all hero names we could use:Īs you can see, we only received the data that was requested. To read more about the schema syntax see (I talk about some of these tools later in the article) The GraphQL Schema is designed to be in an easily readable format for both human and computers, this helps with onboarding new developers as they can easily see what the API represents and how to query the data, and by being easily parsed by a computer it allows a whole range of tools to extract information from the schema and present it to the user in different ways. The top-level items a developer can query within GraphQL are specified under the “Type Query” block, within the above schema we can query and find a single hero by passing a character name or query against all heroes without any arguments. The GraphQL Server will only accept queries which use these fields, and won’t allow the server to return data which isn’t listed within this “contract”. In GraphQL you first describe what your data will look like through the GraphQL Schema format, this is a strongly typed “contract” where the API is described in terms of types, fields and the relationship between them, rather than endpoints in a REST API. I’ll walk through the basic make up of GraphQL GraphQL aims to solve both the above problems by allowing the developer in one HTTP query to query against multiple resources and choose what data they require, rather than in the REST format having to make a HTTP query to each resource that is required, and receiving all the data the server decides to return for that resource. One core problem Facebook was trying to solve with this new API format was the idea that different platforms e.g Mobile and Desktop may need similar data provided from an API but each platform might need more or less of the data provided from an API and in the case of Mobile devices where users have high latency connections every bit of data transferred should be questioned.Īnother problem being the amount of boiler plate code required to fetch data from multiple rest endpoints to prepare the data, parse it and then join it all together, this code then is typically rewritten for each platform as required. GraphQL was created by Facebook as they needed an API format which was powerful enough to describe the entirety of the Facebook ecosystem while also being easy to learn and to provide predictable results. One such option that I’ll be describing in this article is GraphQL. During my time with Lynkz I’ve had the opportunity to work with GraphQL, in this guide I’ll cover the basic principals of getting started with GraphQL.Įvery developer at some point in their career used REST before to enable the frontend of their application to communicate to a backend system, while REST does the job there are other options on the market which preform similar or better.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |