0

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

enter image description here

The result i want if query successfully execute

{
 "status" : "success"
  "data": {
    "getCandidateKey": {
      "CandidateID": 56
    }
  }
}

For any error

    {
     "status" : "error"   //for throwing error
      "data": null    
    }

P.S I am new to GraphQL and SQL Server.

1

1 Answer 1

2

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
    }
`);
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for help but not getting the desired result.
i have changed my code according to your answer but there is no key status showed in response also not getting candidateID in response. Here is the response i am getting after changing my code.{ "data": { "getCandidateKey": { "CandidateID": null } } }
@amanraj - Did you also modify the graphql types as required?
i haven't modified anything only changed GetCandidateKey given by you.
@amanraj - Now that your function returns a different object you should also change the type in the graphql schema, see my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.