-
-
Save Biromain/86190e2b9ad2e49d70f358a2ffe0ff78 to your computer and use it in GitHub Desktop.
Merge GraphQL schemas & resolvers in modules
This file contains hidden or 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
| let moduleQueries = []; | |
| let moduleTypeDefinitions = []; | |
| let moduleMutations = []; | |
| let moduleResolvers = []; | |
| let files = config.getGlobbedFiles(path.join(__dirname, "**", "*schema.js")); | |
| // Load schema files | |
| files.forEach((file) => { | |
| let moduleSchema = require(path.resolve(file)); | |
| moduleQueries.push(moduleSchema.schema.query); | |
| moduleTypeDefinitions.push(moduleSchema.schema.typeDefinitions); | |
| moduleMutations.push(moduleSchema.schema.mutation); | |
| moduleResolvers.push(moduleSchema.resolvers); | |
| }); | |
| // --- MERGE TYPE DEFINITONS | |
| const schema = ` | |
| type Query { | |
| ${moduleQueries.join("\n")} | |
| } | |
| ${moduleTypeDefinitions.join("\n")} | |
| type Mutation { | |
| ${moduleMutations.join("\n")} | |
| } | |
| schema { | |
| query: Query | |
| mutation: Mutation | |
| } | |
| `; | |
| // --- MERGE RESOLVERS | |
| function mergeModuleResolvers(baseResolvers) { | |
| moduleResolvers.forEach((module) => { | |
| baseResolvers = _.merge(baseResolvers, module); | |
| }); | |
| return baseResolvers; | |
| } | |
| module.exports = { | |
| schema: [schema], | |
| resolvers: mergeModuleResolvers({}) | |
| }; | |
This file contains hidden or 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 query = ` | |
| devices(limit: Int, offset: Int, sort: String): [Device] | |
| device(id: Int, code: String): Device | |
| `; | |
| const typeDefinitions = ` | |
| type Device { | |
| id: Int! | |
| code: String! | |
| address: String | |
| type: String | |
| name: String | |
| description: String | |
| status: Int | |
| lastCommunication: Timestamp | |
| } | |
| `; | |
| const mutation = ``; | |
| const resolvers = { | |
| Query: { | |
| devices(root, args, context) { | |
| return applyLimitOffsetSort(Device.find({}), args).exec(); | |
| }, | |
| ... | |
| }, | |
| Mutation: { | |
| } | |
| }; | |
| module.exports = { | |
| schema: { | |
| query, | |
| typeDefinitions, | |
| mutation | |
| }, | |
| resolvers | |
| }; |
This file contains hidden or 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 query = ` | |
| posts(limit: Int, offset: Int, sort: String): [Post] | |
| post(id: Int, code: String): Post | |
| `; | |
| const typeDefinitions = ` | |
| type Post { | |
| id: Int! | |
| code: String! | |
| title: String | |
| content: String | |
| author: User! | |
| views: Int | |
| voters(limit: Int, offset: Int, sort: String): [User] | |
| upVoters(limit: Int, offset: Int, sort: String): [User] | |
| downVoters(limit: Int, offset: Int, sort: String): [User] | |
| votes: Int, | |
| createdAt: Timestamp | |
| updatedAt: Timestamp | |
| } | |
| `; | |
| const mutation = ` | |
| upVote(postID: Int!): Post | |
| downVote(postID: Int!): Post | |
| `; | |
| const resolvers = { | |
| Query: { | |
| posts(root, args, context) { | |
| return applyLimitOffsetSort(Post.find({}), args).exec(); | |
| }, | |
| ... | |
| }, | |
| Mutation: { | |
| upVote(root, args, context) { | |
| ... | |
| }, | |
| downVote(root, args, context) { | |
| ... | |
| } | |
| } | |
| }; | |
| module.exports = { | |
| schema: { | |
| query, | |
| typeDefinitions, | |
| mutation | |
| }, | |
| resolvers | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment