In my node js code
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
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.
Modify this function so that it'll send the required object as the response.
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.
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
}
`);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.