Last active
September 9, 2021 00:04
-
-
Save arleyhr/68124e874dfda697859b442e80bd6906 to your computer and use it in GitHub Desktop.
GraphQL modularize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const typeDefinitions = ` | |
type User { | |
id: ID! | |
author: User! | |
summary: String! | |
content: String! | |
image: String! | |
tags: [Tag!]! | |
} | |
type Tag { | |
id: ID! | |
name: String! | |
posts: [Post] | |
} | |
input postData { | |
author: User! | |
summary: String! | |
content: String! | |
image: String! | |
tags: [Tag!]! | |
} | |
` | |
const queries = ` | |
posts: [Post] | |
tags: [Tag] | |
postsByTag(tagId: Int!): [Post] | |
` | |
const mutations = ` | |
postAdd(user: postData ): Post | |
postDelete(id: Int!): Post | |
` | |
const resolvers = { | |
Query: { | |
posts: (root, args) => { ... } // resolve | |
postsByTag: (root, args) => { ... } //resolve | |
}, | |
Mutation: { | |
postAdd: (root, args) => { ... } // resolve | |
postDelete: (root, args) => { ... } // resolve | |
} | |
} | |
module.exports = { | |
typeDefinitions, | |
queries, | |
resolvers, | |
mutations | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { makeExecutableSchema } from 'graphql-tools' | |
import glob from 'glob' | |
import path from 'path' | |
let typeDefs = [] | |
let allQueries = [] | |
let allResolvers = { Query: {}, Mutation: {} } | |
let allMutations = [] | |
// Search for files with the name 'schema' | |
const schemas = glob.sync(`${__dirname}/../../api/**/*schema.js`) | |
schemas.forEach(file => { | |
// Require each file | |
let { typeDefinitions, queries, resolvers, mutations } = require(path.resolve(file)) | |
typeDefs.push(typeDefinitions) | |
allMutations.push(mutations || '') | |
allQueries.push(queries) | |
Object.keys(resolvers).forEach(key => { | |
if(!allResolvers[key]) | |
allResolvers[key] = {} | |
Object.assign(allResolvers[key], resolvers[key]) | |
}) | |
}) | |
const mergedSchema = ` | |
${typeDefs.join('\r')} | |
type Query { | |
${allQueries.join('\r')} | |
} | |
type Mutation { | |
${allMutations.join('\r')} | |
} | |
` | |
const schema = makeExecutableSchema({ | |
typeDefs: mergedSchema, | |
resolvers: allResolvers | |
}) | |
export default schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const typeDefinitions = ` | |
type User { | |
id: ID! | |
email: String! | |
name: String! | |
picture: String | |
} | |
input userData { | |
name: String! | |
email: String! | |
picture: String! | |
} | |
` | |
const queries = ` | |
users: [User] | |
` | |
const mutations = ` | |
userAdd(user: userData ): User | |
userUpdate(id: Int!, user: userData): User | |
` | |
const resolvers = { | |
Query: { | |
users: (root, args) => { ... } // resolve | |
}, | |
Mutation: { | |
userAdd: (root, args) => { ... } // resolve | |
userUpdate: (root, args) => { ... } // resolve | |
} | |
} | |
module.exports = { | |
typeDefinitions, | |
queries, | |
resolvers, | |
mutations | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment