Daniel Rudd
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 schema

Example 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! :)

    Daniel Rudd

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade