Skip to content

stepanenko/graphql-info

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

GraphQL

GraphQL OfficialTraining Courses

GraphQL is a query language and runtime for APIs that was developed by Facebook and released as an open-source project in 2015. It provides a more efficient and flexible alternative to traditional RESTful APIs for fetching and manipulating data.

A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type. For example, a GraphQL service that tells you who the logged in user is (me) as well as that user’s name might look like this:

type Query {
  me: User
}
 
type User {
  id: ID
  name: String
}

Along with functions for each field on each type:

function Query_me(request) {
  return request.auth.user;
}
 
function User_name(user) {
  return user.getName();
}

After a GraphQL service is running (typically at a URL on a web service), it can receive GraphQL queries to validate and execute. The service first checks a query to ensure it only refers to the types and fields defined, and then runs the provided functions to produce a result.

For example, the query:

{
  me {
    name
  }
}

Could produce the following JSON result:

{
  "me": {
    "name": "Luke Skywalker"
  }
}

Here's an overview of GraphQL and how it differs from similar tools:

  1. Declarative Data Fetching:

GraphQL allows clients to specify exactly what data they need from the server by defining queries. Clients can request specific fields, nested relationships, and even specify the shape of the response data. This enables more efficient data fetching, as clients only receive the data they explicitly request, reducing over-fetching and under-fetching of data.

  1. Single Endpoint:

In GraphQL, there is typically a single endpoint for all data queries and mutations. This contrasts with RESTful APIs, where multiple endpoints are often required to access different resources and perform different actions. Having a single endpoint simplifies API interactions and reduces the number of HTTP requests needed to fetch data.

  1. Strongly Typed Schema:

GraphQL APIs are defined by a strongly typed schema that explicitly defines the types of data that can be queried and manipulated. This schema serves as a contract between the client and server, providing clear documentation and validation of API operations. Clients can introspect the schema to discover available data and operations, enabling powerful tooling and developer experiences.

  1. Batched Queries:

GraphQL allows clients to send batched queries in a single request, reducing the overhead of multiple HTTP requests. Clients can specify multiple queries or mutations within a single request, and the server processes them in parallel and returns the results in a single response. This can improve network efficiency and reduce latency, especially in mobile or low-bandwidth environments.

  1. Real-Time Data:

GraphQL supports real-time data updates through subscriptions, allowing clients to receive real-time updates when data changes on the server. Subscriptions enable features like live notifications, chat applications, and real-time dashboards, making GraphQL suitable for building reactive and interactive applications.

  1. Client-Driven Development:

GraphQL enables a more client-driven development workflow, where frontend developers can iterate quickly and independently without being constrained by backend changes. Clients can evolve their queries and data requirements over time without requiring changes to the server, promoting faster development cycles and improved developer productivity.

In summary, GraphQL offers several advantages over traditional RESTful APIs, including declarative data fetching, a single endpoint, strongly typed schema, batched queries, real-time data updates, and client-driven development. While similar tools like RESTful APIs, SOAP, and gRPC serve similar purposes, GraphQL's unique features and capabilities make it a compelling choice for building modern, efficient, and flexible APIs.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published