Search code examples
node.jsexpressgraphqlapolloapollo-server

How to fetch the data from database for resolver in graphql


I Have Created two File

index.js

const {ApolloServer,gql} = require('apollo-server');
const fs = require('fs');
const path = require('path');

const typedefs = gql`
        type Query {
            info: String!
            ask: [Person!]
        }
        type Person {
            name: String!
            age: Int!
        }
`;
const resolvers = {
    Query: {
        info: () => `Hello World from Linux Fan`,
        ask: () => {
            return [fs.readFileSync(__dirname+path.join('/db.db'),'utf-8')]
        }
    }
}

const server = new ApolloServer({
    typedefs,
    resolvers
}).listen().then(({url}) => console.log(url)).catch(err => console.log(err));

and one More File for storing Database

db.db

{
    name:"Linux Age",
    age: 19
}

But The Problem is everytime I make a query for fetching name and age like

{
  info
  ask{
    name
  }
}

There is a problem which exist and say

"Cannot return null for non-nullable field Person.name"

Error

How to Solve ??


Solution

  • According to Node.js documentation (https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options), fs.readFileSync() returns a String or Buffer. From the schema, however, ask() returns an array of type Person which is an object. The result of fs.readFileSync() should be converted to object before returning:

    ask: () => {
      const person = JSON.parse(JSON.stringify(
        fs.readFileSync(__dirname + path.join('/db.db'), 'utf-8').toString()
      ));
      return [person];
    }
    

    Notice that I called JSON.stringify() before parsing it with JSON.parse(). The reason is the file db.db has a javascript object (keys, nanely name and age, without double quotes around them) and not a JSON object (keys with quotes as shown below):

    {
      "name":"Linux Age",
      "age": 19
    }
    

    Otherwise, JSON.parse() would have a problem parsing the invalid JSON format:

    {
      name:"Linux Age",
      age: 19
    }
    

    In addition, toString() after calling readFileSync() is needed to convert a Buffer to a string:

    fs.readFileSync(__dirname + path.join('/db.db'), 'utf-8').toString()