Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Concept Entry] general: GraphQL #6132

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions content/general/concepts/graphql/graphql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
Title: 'GraphQL'
Description: 'A flexible, efficient API query language that allows clients to request precisely the data they need.'
Subjects:
- 'Computer Science'
- 'Web Development'
Tags:
- 'APIs'
- 'Developer Tools'
- 'Dependency'
- 'Interface'
CatalogContent:
- 'paths/computer-science'
- 'paths/front-end-engineer-career-path'
---

**GraphQL** is a modern query language and runtime for [APIs](https://www.codecademy.com/resources/docs/general/api), developed by Facebook in 2012. It provides a more flexible, powerful alternative to traditional [REST](https://www.codecademy.com/resources/docs/general/rest) APIs by enabling clients to define exactly what data they want to retrieve.

## History

- Developed internally at Facebook in 2012 to address limitations in mobile application data fetching.
- Open-sourced in 2015, gaining rapid adoption in web and mobile development.
- Created to solve inefficiencies in data retrieval and provide more precise data manipulation.

## Working

GraphQL operates through a single endpoint where clients can:

- Define precise data requirements using a strongly typed schema.
- Request multiple resources in a single query.
- Retrieve exactly the data needed, reducing over-fetching and under-fetching.
- Provide a contract between client and server about data capabilities.

## GraphQL vs. REST

| Feature | GraphQL | REST |
| ---------------------------------- | ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| **Data Fetching** | Clients specify the exact data they need | Server defines fixed endpoints returning predefined data |
| **Over-fetching & Under-fetching** | Avoids both by allowing precise queries | Over-fetching (getting more data than needed) and under-fetching (making multiple requests) can occur |
| **Endpoints** | Single endpoint (`/graphql`) for all queries | Multiple endpoints for different resources (`/users`, `/posts`, etc.) |
| **Performance** | Efficient as only required data is fetched | Can be inefficient due to multiple round trips |
| **Flexibility** | Highly flexible; clients control response structure | Less flexible; fixed responses based on server design |
| **Versioning** | No versioning needed; schema evolves dynamically | Often requires versioning (`/v1/users`, `/v2/users`) |
| **Batch Requests** | Can retrieve multiple resources in one request | Requires multiple requests for multiple resources |
| **Real-time Support** | Built-in support via subscriptions | Requires WebSockets or polling for real-time data |
| **Ease of Use** | Requires learning a query language (GraphQL) | Familiar, follows standard HTTP methods (GET, POST, PUT, DELETE) |
| **Error Handling** | Centralized error handling with structured responses | Error handling varies by endpoint and implementation |
| **Caching** | More complex; requires custom caching strategies | Easily cached using HTTP caching mechanisms |