[英]How to get response status in GraphQL using node js?
In my node js code在我的节点 js 代码中
var express_graphql = require('express-graphql');
var { buildSchema } = require('graphql');
var schema = buildSchema(`
type Query {
getCandidateKey(email : String): GetCandidateKey
}
type GetCandidateKey{
CandidateID : Int
}
`);
here i am executing the stored procedure这里我正在执行存储过程
var GetCandidateKey = async function (args) {
let email = args.email
let query = "exec getCandidateKey @EmailID='" + email + "';"; //executing stored procedures
const pool = await poolPromise
const result = await pool.request().query(query)
return result.recordset[0]
}
Root resolver根解析器
var root = {
getCandidateKey: GetCandidateKey,
};
Create an express server and a GraphQL endpoint创建一个快速服务器和一个 GraphQL 端点
app.use('/graphql', express_graphql({
schema: schema,
rootValue: root,
graphiql: true
}));
The result i am getting我得到的结果
The result i want if query successfully execute如果查询成功执行,我想要的结果
{
"status" : "success"
"data": {
"getCandidateKey": {
"CandidateID": 56
}
}
}
For any error对于任何错误
{
"status" : "error" //for throwing error
"data": null
}
PS I am new to GraphQL and SQL Server. PS 我是 GraphQL 和 SQL 服务器的新手。
Modify this function so that it'll send the required object as the response.修改此 function 以便它发送所需的 object 作为响应。
var GetCandidateKey = async function (args) {
try {
let email = args.email
let query = "exec getCandidateKey @EmailID='" + email + "';"; //executing stored procedures
const pool = await poolPromise
const result = await pool.request().query(query)
return {
status: 'success',
data: result.recordset[0]
};
} catch (e) {
return {
status: 'error',
data: null
};
}
}
And also change the GraphQL types as needed.并根据需要更改 GraphQL 类型。
Update更新
You can use the following schema:您可以使用以下架构:
var schema = buildSchema(`
type Query {
getCandidateKey(email : String): CandidateResponse
}
type CandidateResponse {
status: String!
data: GetCandidateKey
}
type GetCandidateKey {
CandidateID : Int
}
`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.