GraphQL & Pulling Data from Oracle DB: Query Responds with the Resultset and Doesn't Come to The Graphql resolver Window

怎甘沉沦 提交于 2019-12-25 01:19:17

问题


I am new to GraphQL.

Started developing an GraphQL app to pull the data from oracle database.

It is very simple application. The query responds with the resultset and can be seen results in console.log; however, it doesn't come to the graphql window (response/resolver window). It throws the error

Cannot return null for non User.email

I tried the promise in the oracle connection. Not sure, why it is not showing the data in GraphQL.

UserType.js

module.exports = new GraphQLObjectType({
  name: 'User',

  fields: () => {
    return{
      id: { type: GraphQLID },
      email: { type: new GraphQLNonNull(GraphQLString) }
    }
  }
});

DBConnection.js

module.exports = oraPool => {
  return  {
    getUsers(apiKey){
      return oracledb.createPool(oraConfig).then(function() {
    console.log("Connection Pool created");
    return oracledb.getConnection().then(function(conn) {
      return conn.execute(
        //`SELECT 'User ' || JSON_OBJECT ('id' VALUE id, 'email' VALUE email) FROM users where id = :id`
        `SELECT  * FROM users WHERE id = :id`
        ,[apiKey]).then(result => {
          //console.log(result.metaData);
          console.log(humps.camelizeKeys(result.rows[0]));
          conn.close();
          return humps.camelizeKeys(result.rows[0]);
        })
        .catch(function(err) {
          console.log(err.message);
          return connection.close();
        });
      })
      .catch(function(err) {
        console.error(err.message);
      });
    })
      }
    }
  }

Type.js

const RootQueryType = new GraphQLObjectType({
  name: 'RootQueryType',

  fields: {
    user: {
      type: UserType,
      args: {
    key: { type: new GraphQLNonNull(GraphQLString) }
      },
      resolve: (obj, args, { oraPool }) => {
    return oradb(oraPool).getUsers(args.key);
      }

    }
  }
});

回答1:


This code is pretty tangled, I recommend starting from scratch. For example, you're calling createPool each time a query is run. You should create the pool during the initialization of your app instead.

Although you said this will be a simple app, it could always grow. Creating a from scratch GraphQL server isn't trivial. I recommend bringing in some help via join-monster. Unfortunately, join-monster is no longer being actively developed. But it's pretty stable and it's a lot better than starting from scratch. Here's a nice overview of how it works: https://github.com/stems/join-monster/tree/master/src

I recently did a talk on GraphQL which you can see here: https://www.slideshare.net/DanielMcGhan/intro-to-graphql-for-database-developers

For the demo, I took a simple API pattern I describe in this blog series and adapted it to be a GraphQL server on the common EMP and DEPT demo tables. You can access the code here: https://www.dropbox.com/s/cnvyrlik7irtbwm/graphql-api.zip?dl=0

Also, another one of my colleagues talks about GraphQL here: https://blogs.oracle.com/opal/demo:-graphql-with-node-oracledb



来源:https://stackoverflow.com/questions/56281502/graphql-pulling-data-from-oracle-db-query-responds-with-the-resultset-and-doe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!