简体   繁体   English

如何使用节点 js 在 GraphQL 中获取响应状态?

[英]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.

相关问题 如何获取 Node.js 响应头和状态码? - How to get Node.js response headers and status code? 如何使用ajax从javascript中的node.js获得响应? - how to get response from node.js in javascript by using ajax? 如何使用 node-fetch 从 API 获取数据和响应状态? - How to get data and response status from API using node-fetch? 如何在使用GraphQl和Node.js时验证用户电子邮件 - How to verify user email while using GraphQl and Node.js 如何使用树莓派pi节点js获取加载时GPIO引脚的状态? - How do I get status of GPIO pin on load using raspberry pi node js? 使用node.js谷歌云语音文本,如何获取当前工作的状态? - Using the node.js google cloud speech to text, how can I get the status of a current job? 如何使用节点js从http(GET)响应中获取巨大的字符串数据而不进行缓冲? - How to get huge string data from http(GET) response using node js without buffering? 如何从 API 获取响应数据并从数据中编码函数以使用 node js JavaScript 获取结果 - How to get the response data from API and code the function from data to get the result using node js JavaScript 如何使用ajax.get调用从node.js服务器获取响应 - How to get the response from node.js server using ajax.get call 如何从node.js中获取请求的响应中获取数据? - How to get data from response to get request in node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM