How to connect the service layer to the database?
To establish the connection to the database and manage real data in our application, we use SQLite. This is defined in the service layer, where the business model is implemented and the necessary queries are performed. Having the file structure well organized facilitates this process.
How are the services declared?
In the Product Resolvers
file, we manage the declaration of the product service. Following the example of routes is useful. In routes, each endpoint has specific validations and services, such as the product service. An instance of this service is needed:
const productService = new ProductService();
This service contains the logic needed to manage products. For example, to get a product, the input parameters are used and a query is performed using findOne
.
How to resolve queries to the database?
Using GraphQL allows us to specify what exact data we want to retrieve. When making a query, we can request only the necessary fields. For example, to get a list of products:
query { getProduct { id name name description price }}
This way we get a list of products with the required fields, directly from the database.
How to handle requests and errors in GraphQL?
When resolving requests in GraphQL, it is crucial to properly handle errors to ensure clear feedback to the user.
What happens when a product is not found?
When we request a product with an ID that does not exist, the application responds with a clear error message, thanks to the Boom library. This library facilitates the handling of HTTP errors, and although it was designed for REST API, it adapts to GraphQL. When the product is not found, Boom returns a "Product not-found" error message and a 404 code:
async findOne(id) { const product = await db.Product.findByPk(id); if (!product) { throw Boom.notFound('Product not-found'); } return product;}
This structure allows Apollo Server to interpret and reuse errors, generating a consistent response in GraphQL.
How are these responses verified in testing tools?
By using tools like Insomnia to verify responses, you can see how, when sending a request with an ID that does not exist, GraphQL returns a message similar to that of a REST API, showing that the integration is efficient and adaptive.
Exploring Apollo Server and error handling
Apollo Server has proven to be able to integrate with standard error messages, effortlessly combining REST and GraphQL architectures. This offers valuable flexibility for developers, allowing reuse of existing logic and ensuring a consistent workflow.
How to take advantage of these features?
A best practice is to always use popular and proven libraries for error handling in our applications, ensuring that messages are clear and behavior is consistent across all technologies used. This demonstrates an adaptive and user-centric approach to building modern applications.
With this knowledge, we are ready to continue extending our application capabilities, ensuring robust integration between services and databases. Continue exploring and learning to create exceptional applications!
Want to see more contributions, questions and answers from the community?