How to Create Query for Getting User by ID in Graphql || Nestjs + Typeorm

In this blog, we will discuss how to make a query to fetch one user by id. This’s very simple. We only gonna modify 2 files, user.service and user.resolvers
We’ll make one function with just one line of code. The code is simply finding our expected user in our database. With Typeform, the code will basically look like:<your-user-repository>.find({where: {id: <inputedID>}});
This is our example of the complete function:
async findOne(id: number): Promise<User> {
return await this.userRepository.findOne({ where: { _id: id } });
}
In resolvers, we were just gonna take the input and pass it to the function that we just created.
The query that we created pretty much like the code below:
@Query(returns => User)
async findUser(@Args({name: 'id', type: () => ID}) id: string): Promise {
return await this.usersService.findOne(parseInt(id));
}
To that, our query is created. See also the demonstration on a video of how to create a query of get one user below:
Hi there! Today I will continue to develop a recent project in Strapi, called id-card-repository. If you are new, please check the previous article here. Specifically, the main topic is CRUD operations using GraphQL with Strapi. CRUD stands for Create, Read/Retrieve, Update, and Delete. CRUD operations are essential as data stored in databases dynamically change….
Have you ever heard of API? API stands for Application Programming Interface. As mobile applications became popular nowadays, the use of API also became a standard. For example, if a company has a website and a mobile application (both Android and iOS), they are not going to create separate databases. Instead, they use API to…
Apa itu strapi ?? Strapi adalah CMS Headless bersifat open-source, yang fleksibel dan memberi developer kebebasan untuk memilih alat dan framework favorit mereka sembari juga memungkinkan editor untuk dengan mudah mengelola dan mendistribusikan konten mereka. Dengan kata lain, strapi dapat menyediakan tampilan yang good-looking serta dapat diintegrasikan dengan berbagai framework. Apa saja yang harus disiapkan…
From our previous blog in making a register page with React js + Ant Design (see.. http://about.lovia.id/3-easy-step-to-create-signup-form-with-ant-design-reactjs/), for now we’re gonna connect it to our Strapi Backend and make it functional. The frontend is using the miluv-pwa repository and the backend is using lovia-profile repository. The core thing that we need to do is we…
What is CORS ? CORS stands for Cross-Origin Resource Sharing. According to developer.mozilla.org, Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin. Simply, it is a web application security feature in a…
Strapi GraphQL series is back! In the previous post related to Strapi and GraphQL, in a project named id-card-repository I did CRUD operations without being authenticated which is dangerous enough as it enables all users (public users) to modify data. Today, I would like to implement authentication and authorization so only authenticated users can conduct…