Week 4
Building RESTful APIs with Express
Assignment Reminder
Assignment 1 - Basic CRUD - is due before next week's class.
# Agenda
- AMA (15 min)
- Building RESTful APIs with Express
- Manual testing with Postman
- GraphQL
- Assignment 1: Basic CRUD (30 mins)
# Manual testing with Postman
Manual testing with Postman is a powerful way to test your API's functionality and behavior. Here are a few more tips for testing your API using Postman:
- Use the
Save
button to save your requests in a collection, so you can run them again later or share them with others. - Use the
Duplicate
button to make a copy of a request, so you can easily create similar requests with slight modifications. - Use the
History
tab to view a history of all the requests you've made, and quickly re-run any of them. - Use the
Tests
tab to write test scripts in JavaScript, you can use these scripts to perform assertions on the response, set environment variables, or even generate data for the next request. - Use the
Pre-request
Script" tab to write scripts that run before the request is sent, you can use these scripts to generate data, set variables, or even modify the request. - Use the
Variables
tab to manage environment variables, you can use these variables to store and reuse values such as API keys and URLs. - Use the
Auth
tab to set up authentication for your request, you can use this feature to test your API's authentication and authorization functionality. - Use the
Headers
tab to add or modify headers for your request, you can use this feature to test your API's functionality that depends on headers, such as content-type or user-agent - Use the
Body
tab to add or modify the body of your request, you can use this feature to test your API's functionality that depends on the body of the request, such as a JSON payload - Use the
Send and download
button to download the response as a file, you can use this feature to test your API's functionality that returns files.
By following these tips, you can take full advantage of Postman's features and capabilities to test your API effectively and efficiently.
Looking to learn about Advanced option about Postman, follow along:
- Use the
Environment
feature to easily switch between different environments, such as development, staging, and production. This allows you to test your API in different configurations, and to ensure that it behaves correctly in each environment. - Use the
Runner
feature to run a collection of requests in a specific order and with specific data. This allows you to test your API's functionality that depends on multiple requests, such as a login flow or a checkout process. - Use the
Monitor
feature to schedule and run your requests on a regular basis, this allows you to test your API's functionality that is dependent on time, such as a scheduled job or a recurring event - Use the
Mocks
feature to simulate a response from an endpoint that is not yet implemented, this allows you to test your API's functionality that depends on other APIs, such as a third-party service or a microservice. - Use the
API documentation
feature to generate documentation for your API, this allows you to share your API with other developers and stakeholders and communicate how your API works. - Use the
API testing
feature to test the performance and security of your API, this allows you to test your API's performance, security and other aspects such as compliance Use theIntegration
feature to integrate with other tools, such as Github, Jira, and Slack, this allows you to integrate your API testing process with your development workflow and receive notifications and reports in your preferred platform
By following these tips, you can take full advantage of Postman's features and capabilities to test your API effectively and efficiently. And also you can use the Postman's cloud-based version, to share the collections and environments with your team members, and collaborate on testing and documenting your APIs.
# GraphQL
# What is GraphQL
GraphQL is a query language for APIs and a runtime for executing those queries against your data. It was developed and open-sourced by Facebook in 2015. GraphQL allows client applications to request only the specific data they need, and it allows for more efficient and flexible communication between the client and the server. It is often used as an alternative to RESTful API architecture.
# Examples of GraphQL
Here are a few examples of how GraphQL can be used:
Retrieving information about a specific user: A client application can send a query that requests the name, email, and address of a user with a specific ID. The server can then respond with only the requested information.
Updating multiple fields on an object: A client application can send a single mutation (a special kind of query) that updates the name, email, and address of a user with a specific ID all in one request, rather than having to send multiple requests to update each field individually.
Filtering a list of items: A client application can request a list of items, but also specify certain filters to apply to the list. For example, it can request a list of all "red" items that are priced under $50.
Nested queries: A client can request a complex object and its related object, like asking a question "give me all the books of a specific author and also give me all the comments made by the readers on the book"
These are just a few examples, but GraphQL's flexibility allows for many other use cases as well.
# Code examples of GraphQL
Here's an example of a GraphQL query that retrieves information about a specific user:
query {
user(id: "123") {
name
email
address
}
}
And here's an example of a GraphQL mutation that updates multiple fields on a user object:
mutation {
updateUser(id: "123", name: "John Smith", email: "john@example.com", address: "123 Main St") {
name
email
address
}
}
And here's an example of a GraphQL query that gets all the books of a specific author and also gives all the comments made by the readers on the book
query {
author(id: 1) {
name
books {
title
comments {
text
user {
name
}
}
}
}
}
These examples are written in the GraphQL query language and they can be sent to a GraphQL server, which would then execute the query and return the requested data.
It's worth noting that the GraphQL query language is agnostic to the programming language or framework you're using, so the examples above can be used in any language or framework that has a GraphQL library or implementation.