Skip to content

Latest commit

 

History

History
119 lines (105 loc) · 4.29 KB

API-ans.md

File metadata and controls

119 lines (105 loc) · 4.29 KB

Questions

  1. Construct a GraphQL query to show the repositories in the organization of this course.
  2. Construct a GraphQL query to fetch the title and author of the first 5 issues of the repository for the first lab in our course's Github organization.
  3. Construct a GraphQL query to show the first 5 commit messages, their dates, and the authors of those messages for each repository in the course organization.
  4. What do you observe about the difference between REST API and GraphQL API?

Answers

query {
  organization(login: "COMP3122-2425Sem2") {
    repositories(first: 10) {
      nodes {
        name
        issues {
          totalCount
        }
      }
    }
  }
}
query {
  organization(login: "COMP3122-2425Sem2") {
    repository(name: "lab_github_collaboration") {
      issues(first: 5) {
        nodes {
          title
          author {
            login
          }
          createdAt
        }
      }
 
    }
  }
}
query {
  organization(login: "COMP3122-2425Sem2") {
    repositories(first: 10) {
      nodes {
        name
        commits: object(expression: "main") {
          ... on Commit {
            history(first: 5) {
              nodes {
                message
                committedDate
                author {
                  name
                }
              }
            }
          }
        }
      }
    }
  }
}

The main differences between REST API and GraphQL API are:

  • In REST API, multiple requests to different endpoints are often necessary to fetch related data, while in GraphQL API, you can fetch all the related data in a single request.
  • In REST API, the server defines the structure of the response, while in GraphQL API, the client defines the structure of the response by specifying the fields in the query.
  • In REST API, the response structure is fixed, while in GraphQL API, the response structure is flexible and can be customized based on the query.
  • In REST API, the response may contain more data than needed, while in GraphQL API, the response contains only the data requested in the query.

What are the Pros and Cons of each type of API? REST API: Pros:

  • Well-established and widely used.
  • Simple and easy to understand.
  • Good for simple and straightforward use cases.
  • Good for caching and stateless communication.
  • Good for resource-oriented applications.
  • Good for server-to-server communication.

Cons:

  • Over-fetching and under-fetching of data.
    • Over-fetching means fetching more data than needed, and under-fetching means not fetching enough data.
    • For Github API, an example of over-fetching would be fetching all the details of a repository when only the name is needed.
    • An example of under-fetching would be needing additional data from related resources and having to make multiple requests to fetch that data.
  • Multiple requests to different endpoints are often necessary.
  • For instance in Github REST API, to fetch the details of a repository and its issues, you would need to make separate requests to the repository and issues endpoints.
  • When using GRAPHQL API, you can fetch all the related data in a single request.
  • Fixed response structure and lack of flexibility in response data.

GraphQL API: Pros:

  • Flexible and customizable response data.
    • You can request only the fields you need, such as repository names and their star counts, reducing the amount of data transferred.
  • Fetch all related data in a single request.
    • Example: You can fetch a repository's issues, pull requests, and commits in a single query.
  • Client defines the structure of the response.
    • The client can specify the exact structure of the response, such as including or excluding nested fields like issue comments.
  • Good for complex and nested data requirements.
    • You can retrieve deeply nested data, such as a repository's issues, each issue's comments, and each comment's author details, all in one query.

Cons:

  • Learning curve for developers.
    • Developers need to learn GraphQL syntax and concepts, which can be more complex than REST.
  • More complex to implement and maintain.
    • Setting up a GraphQL server and defining the schema can be more involved than setting up REST endpoints.
  • Not as widely adopted as REST API.
    • While GraphQL is gaining popularity, many APIs and tools still primarily support REST.