Does TypeGraphQl have the capability to automatically format SQL queries?

I am utilizing TypeORM in conjunction with TypeGraphQL. I am curious about the SQL query result that TypeGraphQL provides. For instance, if I have a User Table with numerous columns and a simple resolver like this: @Resolver() class UserResolver { @Query(() => [User]) async user(): Promise { return await User.find(); } } When the client requests user data like this: { user{ id, name } } What SQL query will be executed on the database? Will it be: "SELECT * FROM USER"; Or will it be: "SELECT id, name FROM USER"; If the first SQL query is run, then why should I use GraphQL at all? Wouldn't REST be better? Alternatively, how can I generate a dynamic SQL query based on the user's request?

Answer №1

GraphQl is a powerful tool for flexible field requests. For instance, imagine you have a user table in your database with columns like Id_User, email, and name, but only want to query the Id_User.

Your query would resemble:

 {
  GetUser{
    id
  }
} 

If your resolver is set up like this:

@Resolver()
class UserResolver {
  @Query(() => [User])
  async GetUser(): Promise<User[]> {
    return await User.find();
  }
}

Typeorm will generate a query that selects all fields: "SELECT Id_User, Name, Email FROM USER". Even if you're only requesting one field, Typeorm will fetch all.

To address this issue and specifically request just the "Id_User" field, you can utilize the @Info decorator in TypeGraphQL. Learn more here.

Additonally, Typeorm offers query builder functions to assist in crafting dynamic SQL statements.

Why choose GraphQL over REST? With REST, you are limited in selecting particular fields. GraphQL excels in scenarios where you require different queries for various platforms (web, app, desktop). By using one endpoint like GetUser, you can dynamically choose fields without needing multiple endpoints as with traditional REST APIs.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Halt period indicated in the document specifying the designated timeframe

If I have two files named index.php and fetch.php The contents of index.php are as follows: <script> $(document).ready(function(){ setInterval(function(){ $('#fetch').load('fetch.php') }, 1000); }); </sc ...

Interacting with a JavaScript button using C# WebBrowser

Can someone please assist me with a problem I'm facing? I have a webpage coded in HTML and Javascript. Whenever I use webBrowser1.Document.GetElementById("hmenu").InvokeMember("click");, the menu pops up on my screen. However, I am unsure how to cli ...

Why does the implementation of my interface differ from what is specified in the TypeScript documentation?

Currently delving into the world of TypeScript documentation https://www.typescriptlang.org/docs/handbook/2/classes.html Specifically focusing on the section implements Clauses, an interesting revelation surfaces: A Word of Caution It’s worth noting t ...

Tips for adding an svg element to an existing svg using d3.js

Can another SVG be appended to an existing SVG parent using d3.js? I have tried using the 'svg:image' attribute, but unfortunately, I lose full control over the inner SVG child. The DOM node is created by d3, but it is not rendered, resulting i ...

The struggle between Node.js 404 errors and Angular's URL refresh issue

I am currently developing a Node.js and AngularJS application. I encountered an issue where a page loads successfully when the URL is entered, but then I added a script to redirect to a 404 error page. Now, only one of the criteria works at a time - either ...

Having trouble closing the phonegap application using the Back Button on an Android device

I've encountered an issue with my code for exiting the application. It works perfectly the first time, but if I navigate to other screens and then return to the screen where I want to close the app, it doesn't work. <script type="text/javascr ...

Send an object through the ejs include function

When attempting to pass an object within an ejs include statement, I encountered an unusual error. Despite finding similar questions regarding the issue, I receive the following error when implementing the code below: <div> <%- include (folder/in ...

Issue: React child components cannot be objects (received: object with keys)

Hey everyone, I could really use some help figuring out what I'm doing wrong. Here is the error message I'm receiving: Error: Objects are not valid as a React child (found: object with keys {id, title, bodyText, icon}). If you meant to render a ...

Tips for resolving the error "Encountered duplicate registration of views named RNGestureHandlerButton" in ReactNative

Seeking guidance on implementing a Swipe-to-Delete feature in my App, I manually installed the react-native-gesture-handler. This action triggered an error message which persists even after attempting to uninstall the gesture handler. Any suggestions or so ...

Error Detection: Unable to modify headers after they have been sent to the user in my PassportJS application

I encountered this error while working on my code. I'm not just looking for the location of the error, but also seeking a better method to log errors so that I can identify where they are occurring in my code. Even after checking the error log, I am u ...

Will the callback chain in ExpressJS still execute if the response returns early?

Let's consider a scenario where we have defined a route with a chain of callbacks like below: app.route('/myroute').post(callback1, callback2, callback3); In this setup, each callback has a call to next(), except for the last one. Imagine ...

Utilizing NodeJS and Express to enhance the client side for showcasing an uploaded file

My knowledge of nodeJS, AJAX requests, and routing is still in its infancy. After following a tutorial on nodejs and express examples, I managed to get everything working on the server-side. However, I'm facing difficulty displaying the uploaded file ...

The variable referencing an unidentified function has not been defined

I've created an anonymous function assigned to a variable in order to reduce the use of global variables. This function contains nested functions for preloading and resizing images, as well as navigation (next and previous). However, when I try to run ...

Failing to retain hyperlinks with ajax

Currently, I am utilizing ajax to transmit data from a sophisticated custom field wysiwyg editor. Within this setup, the specific div with the class 'bio' is what I'm addressing. The issue arises when the data is retrieved - all the original ...

Duplicating an Angular 2 reactive form without retaining the original reference

I am facing a challenge with my reactive form where I need to round certain values when the form is submitted, without altering their appearance on the page. To achieve this, I devised a method that generates a new form, rounds the specified values, and t ...

Obtain the rotational value in 3D CSS using JavaScript by extracting it from the matrix3d()

My element has been 3D transformed in the following way: .foo { transform: rotateX(-30deg) rotateY(35deg); } Now, I am looking to retrieve these values using JavaScript. Extracting the 3D matrix is simple: var matrix = $('.foo').css('tr ...

What varieties of ajax styles are there?

Although I am still relatively new to utilizing ajax, I have found a lot of success in my endeavors so far. The majority of my ajax calls tend to follow this format: function saveQueryProf(){ var currentDate = new Date(); var date=currentDate. ...

Unable to modify the value of data using the data() method

Just a basic HTML code snippet <div class="this" data-info="false"></div> $('.this').data('info'); This will correctly output: false $('.this').data('info', 'true'); data-info remains u ...

Experience the combined power of addthis, isotope, and nicescroll - all in a single

I am utilizing a WordPress template that includes a set of share buttons from AddThis. <ul class="addthis extra"> <li class="addthis-hold"> <div class="addthis_toolbox" addthis:url="<?php the_permalink( ...

How do I delete an attached file in an input document? (Firefox)

Is there a way to smoothly remove an attachment selected in the <input type="file"> element? In Firefox, removing an already selected attachment can be tricky. Simply deleting the name or trying to click open may not work. An additional solution mi ...