Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphql schema returning null [duplicate]

I am trying to return a authentication token in response to a graphql query with username and password as arguments, but somehow graphql is always returning null. I am able to print the token just before returning it.

var {
  GraphQLObjectType,
  GraphQLInt,
  GraphQLList,
  GraphQLString,
  GraphQLSchema
} = require('graphql');
let MyUser = require('./modules/user/user');


const Query = new GraphQLObjectType({
  name: 'Query',
  description : 'UserQuery',
  fields: function(){
    return {
      Token :{
        type: GraphQLString,
        description : "Authentication token",
        args: {
          user_name: { type: GraphQLString },
          password: { type: GraphQLString },
        },
        resolve(root, args){
          let user = new MyUser();
          user.authenticateUser(args.user_name, args.password, function(result){
            console.log("token :"+result);
            return result;
          })
        }
      }
    }
  }
});


const Schema = new GraphQLSchema({
  query: Query
});

module.exports = Schema;

Query look like

query{
  Token(user_name: "[email protected]" 
    password: "abc123")
}

Result

{
  "data": {
    "Token": null
  }
}

what I am doing wrong here ?

like image 782
Reneshankar Avatar asked Oct 31 '16 18:10

Reneshankar


2 Answers

The resolver must return the JSON value, or a promise that results in that value. In your example you're using a callback to fetch the actual REST query:

user.authenticateUser(args.user_name, args.password, function(result){
  console.log("token :"+result);
  return result;
});

This won't work since it doesn't immediately return a value. You can try something like this:

resolve(root, args){
  let user = new MyUser();
  return new Promise((resolve, reject) => {
    user.authenticateUser(args.user_name, args.password, function(result){
      console.log("token :"+result);
      resolve(result);
    });
  })
}

I haven't ran this code to ensure it works, but this basic idea is to always return a value for your resolvers and callbacks, unfortunately, don't allow for this

like image 78
browserless Avatar answered Dec 07 '22 10:12

browserless


Not related to your case, but as you are the first result for the Google search "GraphQl query always returning null", I share my experience, with the hope to help some.

Here was my original query, always returning null:

query: new GraphQLObjectType({
    name: 'RootQueryType2',
    fields: {
        allDatalogs: {
            type: new GraphQLList(dataLogType),

            resolve: (root, source, fieldASTs) => {
                var foundItems = new Promise((resolve, reject) => {
                    DataLogMongo.find({}, (err, users) => {
                        if (err) {
                            reject(err);
                        } else {
                            resolve(users);
                            users.forEach(function (myDoc) {
                                console.log("datalog: " );
                                console.log(myDoc);
                            });
                        }
                    })
                    return foundItems
                })

            }
        }

As you can see, the return statement was inside the Promise(..) brackets. However, it had to be put obviously after, which gave:

allDatalogs: {
            type: new GraphQLList(dataLogType),

            resolve: (root, source, fieldASTs) => {
                var foundItems = new Promise((resolve, reject) => {
                    DataLogMongo.find({}, (err, users) => {
                        if (err) {
                            reject(err);
                        } else {
                            resolve(users);
                            users.forEach(function (myDoc) {
                                console.log("datalog: " );
                                console.log(myDoc);
                            });
                        }
                    })

                })
                return foundItems
            }
        }

Then I was able to get the results. Hope I helped :)

like image 42
Bonjour123 Avatar answered Dec 07 '22 09:12

Bonjour123