1

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);
      }

    }
  }
});
MT0
  • 143,790
  • 11
  • 59
  • 117
Morgan
  • 27
  • 5
  • Please avoid deleting and reposting questions. This is generally considered [abusive behavior](https://meta.stackoverflow.com/questions/251724/is-it-ok-to-repost-deleted-answers-as-new-answers). If you delete a question by accident, you can always undelete it. – Daniel Rearden May 23 '19 at 19:09
  • @Daniel Rearden, My bad. I wanted to put more detailed information on my question (newbie to stackoverflow). wanted to add more info too - my console.log output is like [1, 'ora@11.com' ] – Morgan May 23 '19 at 19:15
  • You can add information by editing a question. Please do not delete and repost. – Cody Gray - on strike May 23 '19 at 22:12

1 Answers1

0

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

Dan McGhan
  • 4,479
  • 1
  • 11
  • 15
  • Also see https://blogs.oracle.com/opal/demo-graphql-with-oracle-database-soda-and-node-oracledb – Christopher Jones May 24 '19 at 09:05
  • Thank you very much Dan. I am a big time reader of your blog on Nodejs with Oracle. Your references here helped me to clear lot of things on my end, Now I dont see that error anymore. One question though, For mutations, do we have any plugins like join-monster by any chance. – Morgan May 24 '19 at 18:03
  • I don't think so. Though, with mutations, it's probably not too bad to write your own resolvers. It's really just the queries that are difficult since they can be nested and refer to multiple types (foreign keys) which can result in complex dynamic SQL. – Dan McGhan May 24 '19 at 20:07
  • Thank you @DanMcGhan. The query is answered and resolved all of my issues. – Morgan May 25 '19 at 01:22
  • @DanMcGhan, I am in the process of taking this to Production. Would like to few things about Join-Monster. Is there a way we can use oracle hints in the select statements. How do we improve the performance of the joins ? Thanks in advance. If needed, I can open a new question. – Morgan May 29 '19 at 22:49
  • I don't think you can use hints with the code I've supplied. However, `sqlTable` can be an expression that generates a table: https://join-monster.readthedocs.io/en/latest/map-to-table/ I guess you could have hints there, but I'm not sure that would help with your use case. It's an open source project, so I suppose you could fork it and make changes as needed. They talk some about query perf here: https://join-monster.readthedocs.io/en/latest/query-planning/ Maybe reach out to the maintainer to see if he would accept pull requests: https://github.com/GlennMatthys – Dan McGhan May 30 '19 at 17:24