GraphQL: fulfill query from JSON file source GraphQL: fulfill query from JSON file source json json

GraphQL: fulfill query from JSON file source


The callback function you're passing to readFile runs asynchronously, which means returning a value from it doesn't do anything -- the function the readFile call is inside is done executing and has returned a value (null) by the time your callback is done.

As a rule of thumb, when dealing with GraphQL, you should stay away from callbacks -- your resolvers should always return a value or a Promise that will eventually resolve to a value.

Luckily, fs has an asynchronous method for reading files, so you can just do:

const root =  {  experiment: () => {    const file = fs.readFileSync('./data/experimentExample.json', 'utf8')    return JSON.parse(file)  }};// or even cleaner:const root =  {  experiment: () => JSON.parse(fs.readFileSync('./data/experimentExample.json', 'utf8'))};

As an additional example, here's how you would do that with a Promise:

// using Node 8's new promisify for our exampleconst readFileAsync = require('util').promisify(fs.readFile)const root =  {  experiment: () => readFileAsync('./data/experimentExample.json', {encoding: 'utf8'})    .then(data => JSON.parse(data))};// Or with async/await:const root =  {  experiment: async () => JSON.parse(await readFileAsync('./data/experimentExample.json', {encoding: 'utf8'}))};

Of course there's no need to promisify readFile since you already have an async method available, but this gives you an idea of how to work with Promises, which GraphQL is happy to work with.