Following the solution provided in Demo: GraphQL with Oracle Database and node-oracledb, I created a simple Node JS application with Apollo GraphQL. However, I am stuck at converting the result set obtained from Oracle DB to desired form as expected by the GraphQL schema. If I use JSON.Parse on resultset as shown in the link I mentioned above, it is throwing error -
SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse () at Object.getAssets.. executeQuery.js:39:19
This is my first ever code in Node.js and I am still learning. Hence, Can someone please help me understand why is it not working for me. I am logging the results obtained from DB and it's returning 10 records as expected.
Here is my code:
GraphQL schema definition: schema.js
const { gql } = require("apollo-server");
const typeDefs = gql`
type Asset {
id: ID!
account_id: String
group_id: String
}
type Query {
assets: [Asset]
}
`;
module.exports = typeDefs;
Resolver : resolvers.js
const queryrunner = require("./executeQuery.js")
const resolvers = {
Query: {
assets: async() => queryrunner.getAssets(),
},
};
module.exports = resolvers;
Fetching connection from file oracledatasource.js
let connection;
var oracledb = require('oracledb');
module.exports = {
getConnection : function () {
connection = oracledb.getConnection({
user : "username",
password : "password",
connectString : "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))(CONNECT_DATA =(SERVICE_NAME = servicename)))"
});
console.log("Successfully connected to Oracle!")
return connection;
}
}
And executing query in executeQuery.js :
const conn = require('./oracledatasource')
module.exports = {
getAssets : async() => {
let connection = await conn.getConnection();
let query = "SELECT\r\n"
" CA.ASSET_ID as id,\r\n"
" CA.ACCNT_ID as account_id,\r\n"
" CA.GROUP_ID as group_id\r\n"
"FROM\r\n"
" SCHEMA.TABLE CA\r\n"
"WHERE\r\n"
" CA.ACTIVE_FLG = 1\r\n"
" AND ROWNUM <= 10";
console.log(query);
let result = await connection.execute(query, []);
connection.release();
console.log(result.rows);
return JSON.parse(result.rows);
}
}
index.js is my main file:
const { ApolloServer } = require("apollo-server");
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const server = new ApolloServer({
typeDefs,
resolvers
//mocks: true
});
server.listen().then(({url}) =>
console.log(`Server running at ${url}`)
);
CodePudding user response:
The solution given for this question - Question helped to get the desired response.
I followed this manual as suggested - node-oracledb and passed outformat to execute() function, which returned the response as an array of objects. This eliminated the need of using JSON.parse.
const conn = require('./oracledatasource')
var oracledb = require('oracledb');
module.exports = {
getAssets : async() => {
let connection = await conn.getConnection();
let query = "SELECT\r\n"
" CA.ASSET_ID \"id\",\r\n"
" CA.ACCNT_ID \"account_id\",\r\n"
" CA.GROUP_ID \"group_id\"\r\n"
"FROM\r\n"
" SCHEMA.TABLE CA\r\n"
"WHERE\r\n"
" CA.ACTIVE_FLG = 1\r\n"
" AND ROWNUM <= 10";
console.log(query);
let result = await connection.execute(query, [], { outFormat: oracledb.OUT_FORMAT_OBJECT });
connection.release();
console.log(result.rows);
return Object.values(result.rows);
}
}