For example:
typeDefs
:
type User {
userId: ID!
userNme: String
userEmail: String
}
In database, users
table has three columns: user_id
, user_nme
, user_email
.
Now, the client sends a GraphQL
query named userById
with two fields: user_id
and user_nme
.
The SQL
statements of my DAO
layer is:
async function findUserById(id: string) {
const sql = `
SELECT * FROM users WHERE user_id = ?;
`
return db('users').raw(sql, [id]);
}
As you can see, the SQL
statement selects all columns of the users
table from the database.
How to map the specific fields
of client sends to the SQL
select
statement?
Expected result:
async function findUserById(id: string) {
const sql = `
SELECT
user_id,
user_nme
FROM users WHERE user_id = ?;
`
return db('users').raw(sql, [id]);
}
Simply say is: I just want to query the fields that the client wants to query.