I am learning about graphQL and trying to use modules for my resolvers.
This is my orders.model.js file:
const orders = [
{
date: '2005-05-05',
subtotal: 90.22,
items: [
{
product: {
id: 'redshow',
description: 'old red shoe',
price: 45.11
},
quantity: 2,
}
]
}
]
function getAllOrders() {
return orders
}
export {
getAllOrders
}
This is my orders.resolvers.mjs file:
import { getAllOrders } from "./orders.model"
export const orderResolver =
{
Query: {
orders: (parent) => {
console.log("Getting orders...")
return getAllOrders()
},
}
}
And this is the part of my server.js where I try to load the resolvers:
[...]
const typesArray = loadFilesSync('**/*', {
extensions: ['graphql']
})
const resolversArray = loadFilesSync('**/*', {
extensions: ['resolvers.mjs']
})
console.log(resolversArray)
const schema = makeExecutableSchema({
typeDefs: typesArray,
resolvers: resolversArray
})
[...]
The console.log output is this:
'import { getAllOrders } from "./orders.model"\r\n' +
'\r\n' +
'export const orderResolver = \r\n' +
'{\r\n' +
' Query: { \r\n' +
' orders: (parent) => {\r\n' +
' console.log("Getting orders...")\r\n' +
'\r\n' +
' return getAllOrders() // Best practice is to keep the resolver functions as thin as possible and use the functionality of the models to get the data\r\n' +
' },\r\n' +
'\r\n' +
' }\r\n' +
'}\r\n',
So, it seems like I am doing something wrong on the loadSyncFiles function, but I can't figure out what.
Here is the repo:https://github.com/RicSala/graphql