GraphQL application retruning null on query call

  graphql, javascript, node.js

I am trying to build an API that shortens links with nodejs, graphQL, and postgres database. My methodology is to get the url from my url variable in my shortenURL query via args.url in resolvers.js but this returns undefined. After receiving the url, I generate a shortlink and add it to my localhost i.e localhost:port/dyrnwt, I add no_of_clicks, date_created etc all to the database and then return all these values as the query result.

PROBLEMS

  • Fix args.url returning undefined
  • Fix query returning null i.e
{
  "data": {
    "shortenURL": null
  }
}
  • How to listen for when short url is used and redirect from short url to original url?

index.js

const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express')
const { graphqlHTTP } = require('express-graphql')
const { buildSchema } = require('graphql')
const expressPlayground = require('graphql-playground-middleware-express')
    .default


const app = express()


//loading type definitions from schema file
const fs = require('fs')
const typeDefs = fs.readFileSync('src/schema.graphql',{encoding:'utf-8'})

//loading resolvers
const resolvers = require('./resolvers')

//binding schema and resolver
const {makeExecutableSchema} = require('graphql-tools')
const schema = makeExecutableSchema({typeDefs, resolvers})


app.use(
    '/graphql',
    cors(), 
    bodyParser.json(),
    graphqlHTTP((req) => ({
        schema,
        rootValue: resolvers,
        graphiql:true,
    }))
)

const port = process.env.PORT

app.get('/', expressPlayground({ endpoint: '/graphql' }))

app.listen({ port }, () => {
    console.log(`🚀 Server ready at http://localhost:${port}/graphql`)
})

resolvers.js

const { client } = require("../db_files/connectdb.js");
const { GraphQLDateTime } = require('graphql-iso-date')
const { date } = require('../utils/date')
const { shortcode } = require('../utils/shortcode')


client.connect(function(err) {
  if(err) {
    return console.error('could not connect to postgres', err);
  }
});



const Date = {
Date : GraphQLDateTime,
}



const Link = {

   link_id:(root,args,context,info) => {
      return root.link_id
   },

   org_url:(root,args,context,info) => {
      return root.org_url
   },

   short_url:(root,args,context,info) => {
      return root.short_url
   },

   date_created:(root,args,context,info) => {
      return root.date_created
   },

   no_of_clicks:(root,args,context,info) => {
      return root.no_of_clicks
   }
}



const Query = {
    shortenURL: (args) => { 

        client.query(`SELECT org_url FROM links;`, function(err, links) {
    if(err) {
      return console.error('Error retrieving links', err);
    }
    console.log(links['rows']);
    
if (args.url in links['rows']) {

            //retrieve and return the ones in database
         client.query(` SELECT * FROM links WHERE org_url=${args.url};`, function(err, already_present_query) {
    if(err) {
      return console.error('error running query', err);
    }
    console.log('Link already exists and is retrieved from database');
  });

return already_present_query['rows'];

        
}

else {

    //Sets the database values and return values
        var link_id = shortcode
        var short_url = "shorts-url.herokuapp.com/" + link_id
        var date_created = date
        var org_url = args.url
        var no_of_clicks  = 0
        console.log(link_id, short_url,org_url,no_of_clicks,date_created);

        const set_new_query = { 

        text:'INSERT into links VALUES ( $1,$2,$3,$4,$5)',
        values: [link_id, org_url,short_url,no_of_clicks,date_created]

    };

//inputs the values in the database

   client.query(set_new_query, function(err, set_new_result) {
    if(err) {
      return console.error('error running query', err);
    }
     client.end();
    console.log('Link is successfully shortened');
    return set_new_result['rows'];
  });

  

}
  });

        
    }
 
}

module.exports = {Query, Date,Link}

schema.graphql

type Query {
    shortenURL(url: String!): Link
  }

scalar Date

type Link {
    link_id : ID!
    org_url : String
    short_url: String
    no_of_clicks  : Int
    date_created : Date

  }

Source: Ask Javascript Questions

LEAVE A COMMENT