GraphQL Terminology
What is GraphQL?
GraphQL is a query language for APIs and a runtime for executing those queries with your existing data. It allows clients to request only the data they need, which helps reduce the amount of data transferred over the network.
Schema
The schema is a blueprint of the GraphQL API. It defines the types of data that can be queried and the relationships between those types. A typical schema is written using the GraphQL Schema Definition Language (SDL).
type User {
id: ID!
name: String!
email: String!
}
Query
A query is a request for data from the GraphQL server. It specifies what data the client needs. Queries can also be nested to fetch related data in a single request.
query {
user(id: "1") {
name
email
}
}
Mutation
Mutations are used to modify server-side data. They are similar to queries but are distinguished by their intent to change data.
mutation {
createUser(name: "Alice", email: "alice@example.com") {
id
name
email
}
}
Subscription
Subscriptions are used to maintain a real-time connection to the server. They allow clients to receive updates on specific events.
subscription {
userCreated {
id
name
}
}
Types
GraphQL supports various types, including:
- Scalar Types: Int, Float, String, Boolean, ID
- Object Types: Custom types defined in the schema
- Enum Types: A set of predefined values
- Interface Types: Abstract types that define a set of fields
- Union Types: A type that can represent multiple object types
FAQ
What is the difference between REST and GraphQL?
REST uses multiple endpoints to fetch different resources, while GraphQL uses a single endpoint to handle all requests, allowing clients to specify exactly what data they need.
Can GraphQL be used with any programming language?
Yes, GraphQL can be implemented in any programming language that can handle HTTP requests.
How does GraphQL handle versioning?
GraphQL typically does not require versioning. Instead, it allows you to add new fields and types to your schema, improving the API without breaking existing queries.