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:
In Strapi website, when installing Strapi using Docker, the available guide only tells you how to create a brand new Strapi app. On the other hand, they do not give a step-by-step guide how to build your Strapi app image from an existing project 😔 In this article, we are going to learn how to…
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…
Strapi contains a feature in which anonymous and otherwise non-sensitive data is collected. Like any other software, the main aim is to provide the best experience to users. “ What data is collected ? “ Unique project ID (generated with UUID) Unique machine ID (generated with node-machine-id) Environment state (development, staging, production) System information (OS)…
Installation for MongoDB locally on windows computer Download Mongodb Download MongoDB here. Choose the MongoDB Community Server. Run the Installer Just click next.. Edit Path Edit your environment variable in System Properties. Edit path variable on your User Variables. Add The MongoDB’s Bin folder, usually in C:Program FilesMongoDBServer4.2bin Run MongoDB $ mongod $ mongo…
Hello, today I am going to get in touch with something called API. I guess it is very familiar among people who are interested in software engineering. Let’s get started! Continuing development using Strapi First, open Command Line Interface (CLI) and change the directory to the imagerepo folder by typing : $ cd imagerepo Start…
Hi! Today I am going to create a new project in Strapi, called id-card-repository. The database, which will be used for this project is MongoDB. Prerequisites : However, before starting to create the project, I have to make sure that the MongoDB version is 3.6 or above. To check MongoDB version, open the terminal then…