Sep 4, 2018 · 1 min read
Very helpful post thank you!
I have now taken this up a level and created an autoloader for the schemas provided the schemas models are JS Objects with typeDef and resolvers properties available.
The following code loads files from the ./schema folder:
// ./schema.js
import fs from 'fs'
import {makeExecutableSchema} from 'graphql-tools'
const files = fs.readdirSync(__dirname).filter(file => (file !== 'index.js' && file.includes('.js')))
const schemas = files.map(file => require('./' + file).default)
const schema = makeExecutableSchema({
typeDefs: schemas.map(schema => schema.typeDef),
resolvers: schemas.map(schema => schema.resolvers)
})
export default schemaExample of a qualifying schema model with typeDef & resolvers as properties:
import {gql} from 'apollo-server-express'
import {Author} from "../connectors";
export default {
typeDef: gql`
extend type Query {
author(firstName: String, lastName: String): Author
allAuthors: [Author]
}
type Author {
id: Int
firstName: String
lastName: String
posts: [Post]
}
`,
resolvers: {
Query: {
author(_, args) {
return Author.query()
}
},
Author: {
posts(author) {
return author.getPosts();
}
}
}
}Again, THANK ALOT! :)