Câu hỏi phỏng vấn GraphQL
Câu hỏi

List the key concepts of the GraphQL query language

Câu trả lời

GraphQL is a powerful query language for APIs that allows clients to request exactly the data they need. Here are the key concepts of the GraphQL query language:

1. Schema

The schema is the core of any GraphQL API. It defines the types of data that can be queried and the relationships between them. The schema is written in the GraphQL Schema Definition Language (SDL) and serves as a contract between the client and the server. It includes:

  • Object Types: Represent entities with fields (e.g., User, Post).
  • Scalar Types: Basic data types like String, Int, Float, Boolean, and ID.
  • Enumeration Types: A set of predefined values (e.g., enum Role { ADMIN, USER }).
  • Interfaces: Abstract types that define fields that multiple object types can implement.
  • Unions: Similar to interfaces but without shared fields.
  • Input Types: Used for complex input data in mutations.

2. Queries

Queries are used to fetch data from the server. They are analogous to HTTP GET requests in REST APIs. A query specifies the exact fields needed, and the server responds with data in the same shape as the query. For example:

graphql Copy
query {
  user(id: 1) {
    id
    name
    posts {
      title
    }
  }
}

This query fetches a user by ID, including their name and the titles of their posts.

3. Mutations

Mutations are used to modify data on the server, similar to HTTP POST, PUT, PATCH, and DELETE requests in REST APIs. They allow clients to specify the data to be changed and the fields to be returned after the operation. For example:

graphql Copy
mutation {
  createUser(input: { name: "John Doe" }) {
    id
    name
  }
}

This mutation creates a new user and returns the user's ID and name.

4. Subscriptions

Subscriptions are used for real-time updates. They allow clients to subscribe to specific events and receive updates when those events occur. For example:

graphql Copy
subscription {
  newUser {
    id
    name
  }
}

This subscription notifies the client whenever a new user is created.

5. Fields

Fields are the basic units of data in GraphQL. Each field on an object type can have zero or more arguments, allowing for fine-grained data fetching. For example:

graphql Copy
query {
  user(id: 1) {
    id
    name
    posts(limit: 5) {
      title
    }
  }
}

Here, the posts field has an argument limit to limit the number of posts returned.

6. Arguments

Arguments are used to pass parameters to fields, queries, and mutations. They allow for dynamic and flexible data fetching. For example:

graphql Copy
query {
  user(id: 1) {
    id
    name
    posts(limit: 5) {
      title
    }
  }
}

The id and limit are arguments that refine the data returned by the query.

7. Variables

Variables allow for dynamic queries and mutations by passing external values into the query. They are defined separately from the query string and passe...

middle

middle

Gợi ý câu hỏi phỏng vấn

senior

How to implement a set of GraphQL mutations in single transaction?

expert

How to respond with different status codes in GraphQL?

junior

What is GraphQL schema ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào