GraphQL Fundamentals: Core Cocepts of GraphQL
Ashwin Ashok
Actively seeking full-time Data Engineer job | Cloud Data Engineering (ETL Data Pipeline Development) - Python, Advanced SQL, PySpark, Databricks, Snowflake, Airflow, dbt, Tableau, AWS | MS CSE @ SUNY - Buffalo
1.??? Benefits of a Schema and Type System
GraphQL uses a strongly type system (like Python) to define the various capabilities of an API. The types that will be exposed in a API are drafted in a schema using the GraphQL Schema Definition Language. The schema serves as the contract on how the client and server defines how a client can access the data leaving the frontend and backend team work independently.
2.??? Underlying constructs of GraphQL – Syntax of defining types, sending queries and mutations
GraphQL uses its own type system to define the syntax of an API – Schema Definition Language (SDL). Every entity is defined as type using SDL. For example: type ‘Person’
type Person {
? name: String!
? age: Int! }
This type has 2 fields, name and age of datatype String and Int, The ‘!’ signifies the field is mandatory.
It is possible to express relationships between types. type ‘Person’ associated with type ‘Post’ :
type Post {
? title: String!
??author: Person! }
type Person {
? name: String!
? age: Int!
? posts: [Post!]! }??????????????????? #One-to-many relationship
2a. Fetching Data with Queries
With REST APIs, data requirements are encoded within the URL it connects to. Whereas in GraphQL, instead of having multiple endpoints returning fixed structure of data, exposes a single endpoint, flexible and allowing the client to request for the required fields through a query.
Sample request query (simple JSON object) :
{ allPersons {???????????? # allPersons – root field of the query
??? name?????????????????????? # query payload
??? posts {???????????????????? # query only the required nested information
????? title }}}
The response will return only the ‘name’ field from ‘Person’ type and only the ‘title’ field from ‘Post’ type of all the persons in the database.
领英推荐
2b. Queries with Arguments
Query only a specific no. of persons using parameters.
{ allPersons (last: 2) {?????????? # last parameter specifies the no. of persons to return
??? ?name }
2c. Writing Data with Mutations
Using GraphQL, manipulations to data on server are made using 3 kinds of mutations: 1) creating new data, 2) updating and 3) deleting existing data. Mutations are similar to read queries but always start with ‘mutation’ keyword.
mutation {
? createPerson(name: “xxx”, age: 1) {????????? # createPerson – root field; name & age – args
??? id }}???????????? # payload – specify information to return from server on executing mutation
3.??? Realtime Updates with Subscriptions
A powerful feature of GraphQL is subscriptions for clients to hold a active connection to the server to listen to any important events. The server pushes the corresponding stream of data (instead of typical request-response-cycle in case of queries and mutations) on an event.
Ex: To subscribe on events happening on the Person type
{ subscription {
??? newPerson {
????? name,
????? age }}
Whenever a new record of type ‘Person’ is created, it is sent over to the subscribed client.
4.??? Defining a Schema
Schema (blueprint how the server and client communicate) of states the capabilities of a GraphQL API defining how clients can request data.
type Query {…}????????? ??????????? # For reading data from server
type Mutation {…}???? ??????????? #For writing data on the server
type Subscriptions {…}???????? #For listening to data on the server
type <object> {…}????????????????? #Defining an entity/object ?
All put together, will form a full schema
Entrepreneur, Technocrat , Learner
10 个月Fab, Ashwin, stay blessed