MadAlgos Blog
Graph QL and OData for System Design Interview
Graph QL for System Design Interviews
GraphQL is an open-source query language for APIs (Application Programming Interfaces) that was developed by Facebook. It provides a more efficient and flexible alternative to traditional RESTful APIs by allowing clients to specify exactly what data they need and receive only that data in a single request.
Here are some key aspects of GraphQL:
-
Schema: GraphQL has a strongly-typed schema that describes the capabilities of the API and defines the available data types, relationships, and operations. The schema serves as a contract between the client and the server.
-
Queries: Clients can send queries to the GraphQL server to request specific data. The queries are structured and match the shape of the data that the client wants to receive. This eliminates the issue of over-fetching or under-fetching data common in REST APIs.
-
Mutations: In addition to queries, GraphQL supports mutations, which allow clients to modify data on the server. Mutations are similar to queries in structure but are used for creating, updating, or deleting data.
-
Resolver Functions: GraphQL resolver functions are responsible for fetching the data requested by the client. Each field in the schema has a corresponding resolver function that determines how the data is retrieved. This allows developers to have fine-grained control over data fetching and can combine data from multiple sources.
-
Hierarchical Structure: GraphQL queries can have a hierarchical structure, where the client can request related data and nest fields to retrieve them in a single request. This reduces the number of round-trips to the server and improves performance.
-
Introspection: GraphQL supports introspection, which means clients can query the schema itself to discover the available types, fields, and their relationships. This feature enables tools like GraphiQL and GraphQL Playground to provide auto-completion and documentation to aid in API exploration.
-
Versioning: With GraphQL, there is no need for explicit versioning of APIs. Clients can evolve their queries over time without impacting existing consumers of the API. New fields can be added to the schema without breaking existing queries, as clients only receive the data they explicitly request.
GraphQL has gained significant popularity and is widely adopted by many organizations due to its flexibility, efficiency, and developer experience. It has become a popular choice for building APIs and is supported by various programming languages, frameworks, and tools.
OData and its filters
OData (Open Data Protocol) provides a standardized way of filtering data in RESTful APIs. OData defines a set of query options that can be used to filter, sort, paginate, and perform other operations on the data. Here are some commonly used OData filters:
-
$filter: The
$filteroption is used to specify filtering conditions to retrieve a subset of data based on specific criteria. OData supports various operators, including comparison operators (eq,ne,gt,ge,lt,le), logical operators (and,or,not), and functions (startswith,endswith,contains, etc.). For example:/api/employees?$filter=age gt 30retrieves employees older than 30./api/products?$filter=category eq 'Electronics' and price lt 100retrieves electronics products with a price less than 100.
-
$orderby: The
$orderbyoption is used to specify the sorting order of the retrieved data. It allows sorting based on one or more properties, ascending or descending. For example:/api/employees?$orderby=lastName ascretrieves employees sorted by their last names in ascending order./api/products?$orderby=price desc, name ascretrieves products sorted by price in descending order and then by name in ascending order.
-
$top and $skip: The
$topand$skipoptions are used for pagination.$topspecifies the maximum number of items to retrieve, while$skipspecifies the number of items to skip before retrieving the data. For example:/api/employees?$top=10retrieves the top 10 employees./api/employees?$top=10&$skip=20retrieves the 10 employees starting from the 21st employee.
-
$select: The
$selectoption is used to specify the properties to include in the response. It allows clients to retrieve only the required properties, reducing the amount of data transferred. For example:/api/employees?$select=firstName,lastNameretrieves only the first name and last name of employees.
Let's use the example URLs with the domain "madalgos.in" to demonstrate the usage of OData filters for a website selling mentorship programs and mock interviews.
-
Retrieve mentorships with a price less than 100:
madalgos.in/mentorships?$filter=price lt 100 -
Retrieve mentorships in the "Software Development" category:
madalgos.in/mentorships?$filter=category eq 'Software Development' -
Retrieve mock interviews with a duration of 60 minutes:
madalgos.in/mock?$filter=duration eq 60 -
Retrieve mentorships sorted by price in descending order:
madalgos.in/mentorships?$orderby=price desc -
Retrieve the top 5 mock interviews:
madalgos.in/mock?$top=5 -
Retrieve the 10 mentorships starting from the 11th mentorship:
madalgos.in/mentorships?$top=10&$skip=10 -
Retrieve only the mentorship names and prices:
madalgos.in/mentorships?$select=name,price
These examples demonstrate how OData filters can be applied to the URLs of "madalgos.in" website selling mentorship programs and mock interviews. The specific endpoints and query options may differ depending on the actual implementation of the API.
These are just a few examples of the filters provided by OData. OData also supports other options like $expand for including related entities, $count for retrieving the count of items, and more.
It's important to note that the availability and implementation of these filters may vary depending on the OData service or server you are using. It's recommended to consult the documentation of the specific OData implementation you are working with for detailed information on the supported filters and their usage.
Graph QL vs OData
GraphQL and OData are both query languages and specifications for building APIs, but they have different approaches and philosophies. Here's a comparison between GraphQL and OData:
-
Query Language Approach:
- GraphQL: GraphQL is a strongly-typed query language that focuses on providing clients with the ability to request specific data in a flexible and efficient manner. Clients can send a single query to retrieve only the data they need, avoiding over-fetching or under-fetching of data.
- OData: OData (Open Data Protocol) is a protocol that provides a standardized way of building and consuming RESTful APIs. It defines a set of conventions and query options for filtering, sorting, paging, and expanding related entities. OData is based on the REST principles and aims to provide a uniform way of accessing data across different systems.
-
Data Retrieval:
- GraphQL: With GraphQL, clients have fine-grained control over the data they receive. They can specify the fields, relationships, and even the shape of the data in their queries. This eliminates the problem of over-fetching or under-fetching data commonly found in REST APIs.
- OData: OData allows clients to filter, sort, and page through data using a set of query options like
$filter,$orderby, and$top. It supports querying related entities through navigation properties. OData provides a standardized way of expressing these operations across different data sources.
-
Schema and Metadata:
- GraphQL: GraphQL uses a strongly-typed schema to describe the capabilities of the API and the available data types. The schema serves as a contract between the client and the server, and clients can introspect the schema to discover available types and fields.
- OData: OData also uses metadata to describe the structure and capabilities of the API. The metadata provides information about the available entities, properties, relationships, and supported operations. Clients can query the metadata to understand the API's structure and perform dynamic queries.
-
Data Manipulation:
- GraphQL: GraphQL has built-in support for mutations, allowing clients to modify data on the server. Mutations are similar in structure to queries but are used for creating, updating, or deleting data.
- OData: OData provides standardized ways to perform CRUD (Create, Read, Update, Delete) operations on data. It defines specific HTTP methods and URI conventions for different operations like
POST,PUT,PATCH, andDELETE.
-
Ecosystem and Tooling:
- GraphQL: GraphQL has a growing ecosystem with libraries, tools, and frameworks available for different programming languages. It provides tools like GraphiQL and GraphQL Playground for API exploration and testing.
- OData: OData has been around for a longer time and has established support from various platforms and frameworks. It has tooling and libraries available for different programming languages, and some databases and platforms have built-in support for OData.
Hope you enjoyed reading!
Happy learning!