Split the graphql resolvers file into seperatefiles












2















I'm working with GraphQL and have a resolvers.js file that looks like this:



const User = require("../models/User");
const Post = require("../models/Post");

module.exports = {
Query: {
async users(){...},
async user(){...},
async posts(){...},
async post(){...},
},
User: {...},
Post: {...},
Mutation: {
createUser(){...},
login(){...},
createPost(){...},
},
}


But if I have more models, queries and mutations the file is gonna be very long. How can I split this into seperate files? One for user queries and mutations, one for posts and so. Or is that not possible? Maybe there's a way to combine this with the schema.js file? So that I can split the schema too and put schema/resolver from User into a file. I'm still a beginner in coding.










share|improve this question



























    2















    I'm working with GraphQL and have a resolvers.js file that looks like this:



    const User = require("../models/User");
    const Post = require("../models/Post");

    module.exports = {
    Query: {
    async users(){...},
    async user(){...},
    async posts(){...},
    async post(){...},
    },
    User: {...},
    Post: {...},
    Mutation: {
    createUser(){...},
    login(){...},
    createPost(){...},
    },
    }


    But if I have more models, queries and mutations the file is gonna be very long. How can I split this into seperate files? One for user queries and mutations, one for posts and so. Or is that not possible? Maybe there's a way to combine this with the schema.js file? So that I can split the schema too and put schema/resolver from User into a file. I'm still a beginner in coding.










    share|improve this question

























      2












      2








      2








      I'm working with GraphQL and have a resolvers.js file that looks like this:



      const User = require("../models/User");
      const Post = require("../models/Post");

      module.exports = {
      Query: {
      async users(){...},
      async user(){...},
      async posts(){...},
      async post(){...},
      },
      User: {...},
      Post: {...},
      Mutation: {
      createUser(){...},
      login(){...},
      createPost(){...},
      },
      }


      But if I have more models, queries and mutations the file is gonna be very long. How can I split this into seperate files? One for user queries and mutations, one for posts and so. Or is that not possible? Maybe there's a way to combine this with the schema.js file? So that I can split the schema too and put schema/resolver from User into a file. I'm still a beginner in coding.










      share|improve this question














      I'm working with GraphQL and have a resolvers.js file that looks like this:



      const User = require("../models/User");
      const Post = require("../models/Post");

      module.exports = {
      Query: {
      async users(){...},
      async user(){...},
      async posts(){...},
      async post(){...},
      },
      User: {...},
      Post: {...},
      Mutation: {
      createUser(){...},
      login(){...},
      createPost(){...},
      },
      }


      But if I have more models, queries and mutations the file is gonna be very long. How can I split this into seperate files? One for user queries and mutations, one for posts and so. Or is that not possible? Maybe there's a way to combine this with the schema.js file? So that I can split the schema too and put schema/resolver from User into a file. I'm still a beginner in coding.







      graphql apollo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 10:11







      user9877232































          2 Answers
          2






          active

          oldest

          votes


















          0














          Here is how I did it. Do note that this is in Typescript.



          You would define your resolvers in separate files, such as this:



          import { DateBidListResolvers } from "../../types/generated";

          export const DateBidList: DateBidListResolvers.Type = {
          ...DateBidListResolvers.defaultResolvers,

          list: (_, __) => { // This is an example resolver of Type DateBidList
          throw new Error("Resolver not implemented");
          }
          };


          Then you would aggregate them together in a single file like this:



          import { Resolvers } from "../../types/generated";

          import { Query } from "./Query";
          import { User } from "./User";
          import { DateBid } from "./DateBid";
          import { DateItem } from "./DateItem";
          import { Match } from "./Match";
          import { Mutation } from "./Mutation";
          import { Subscription } from "./Subscription";
          import { DateBidList } from "./DateBidList";
          import { DateList } from "./DateList";
          import { Following } from "./Following";
          import { MatchList } from "./MatchList";
          import { Message } from "./Message";
          import { MessageItem } from "./MessageItem";
          import { Queue } from "./Queue";

          export const resolvers: Resolvers = {
          DateBid,
          DateBidList,
          DateItem,
          DateList,
          Following,
          Match,
          MatchList,
          Message,
          MessageItem,
          Mutation,
          Query,
          Queue,
          Subscription,
          User
          };


          You could then import that resolvers export into your configuration setup:



          import { resolvers } from './resolvers/index';

          // ... other imports here

          export const server = {
          typeDefs,
          resolvers,
          playground,
          context,
          dataSources,
          };

          export default new ApolloServer(server);


          I hope this helps!






          share|improve this answer


























          • Never worked with Typescript. Will take a look at how to use it. Thanks.

            – user9877232
            Dec 1 '18 at 13:24











          • You don't need Typescript to do this. It was just to note on some of the lingo in the code. I would appreciate it if you would select my answer rather than yours so I get the points.

            – Cory McAboy
            Dec 3 '18 at 17:36



















          0














          I found a way to do it very easy actually. In the schema.js I can use lodash merge to combine multiple resolver files and for the typedefs I just use an array. This way I can split everything into seperate files.



          const { merge } = require("lodash");

          module.exports = makeExecutableSchema({
          typeDefs: [typeDefs, userTypeDefs],
          resolvers: merge(resolvers, userResolvers)
          });





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466486%2fsplit-the-graphql-resolvers-file-into-seperatefiles%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown
























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Here is how I did it. Do note that this is in Typescript.



            You would define your resolvers in separate files, such as this:



            import { DateBidListResolvers } from "../../types/generated";

            export const DateBidList: DateBidListResolvers.Type = {
            ...DateBidListResolvers.defaultResolvers,

            list: (_, __) => { // This is an example resolver of Type DateBidList
            throw new Error("Resolver not implemented");
            }
            };


            Then you would aggregate them together in a single file like this:



            import { Resolvers } from "../../types/generated";

            import { Query } from "./Query";
            import { User } from "./User";
            import { DateBid } from "./DateBid";
            import { DateItem } from "./DateItem";
            import { Match } from "./Match";
            import { Mutation } from "./Mutation";
            import { Subscription } from "./Subscription";
            import { DateBidList } from "./DateBidList";
            import { DateList } from "./DateList";
            import { Following } from "./Following";
            import { MatchList } from "./MatchList";
            import { Message } from "./Message";
            import { MessageItem } from "./MessageItem";
            import { Queue } from "./Queue";

            export const resolvers: Resolvers = {
            DateBid,
            DateBidList,
            DateItem,
            DateList,
            Following,
            Match,
            MatchList,
            Message,
            MessageItem,
            Mutation,
            Query,
            Queue,
            Subscription,
            User
            };


            You could then import that resolvers export into your configuration setup:



            import { resolvers } from './resolvers/index';

            // ... other imports here

            export const server = {
            typeDefs,
            resolvers,
            playground,
            context,
            dataSources,
            };

            export default new ApolloServer(server);


            I hope this helps!






            share|improve this answer


























            • Never worked with Typescript. Will take a look at how to use it. Thanks.

              – user9877232
              Dec 1 '18 at 13:24











            • You don't need Typescript to do this. It was just to note on some of the lingo in the code. I would appreciate it if you would select my answer rather than yours so I get the points.

              – Cory McAboy
              Dec 3 '18 at 17:36
















            0














            Here is how I did it. Do note that this is in Typescript.



            You would define your resolvers in separate files, such as this:



            import { DateBidListResolvers } from "../../types/generated";

            export const DateBidList: DateBidListResolvers.Type = {
            ...DateBidListResolvers.defaultResolvers,

            list: (_, __) => { // This is an example resolver of Type DateBidList
            throw new Error("Resolver not implemented");
            }
            };


            Then you would aggregate them together in a single file like this:



            import { Resolvers } from "../../types/generated";

            import { Query } from "./Query";
            import { User } from "./User";
            import { DateBid } from "./DateBid";
            import { DateItem } from "./DateItem";
            import { Match } from "./Match";
            import { Mutation } from "./Mutation";
            import { Subscription } from "./Subscription";
            import { DateBidList } from "./DateBidList";
            import { DateList } from "./DateList";
            import { Following } from "./Following";
            import { MatchList } from "./MatchList";
            import { Message } from "./Message";
            import { MessageItem } from "./MessageItem";
            import { Queue } from "./Queue";

            export const resolvers: Resolvers = {
            DateBid,
            DateBidList,
            DateItem,
            DateList,
            Following,
            Match,
            MatchList,
            Message,
            MessageItem,
            Mutation,
            Query,
            Queue,
            Subscription,
            User
            };


            You could then import that resolvers export into your configuration setup:



            import { resolvers } from './resolvers/index';

            // ... other imports here

            export const server = {
            typeDefs,
            resolvers,
            playground,
            context,
            dataSources,
            };

            export default new ApolloServer(server);


            I hope this helps!






            share|improve this answer


























            • Never worked with Typescript. Will take a look at how to use it. Thanks.

              – user9877232
              Dec 1 '18 at 13:24











            • You don't need Typescript to do this. It was just to note on some of the lingo in the code. I would appreciate it if you would select my answer rather than yours so I get the points.

              – Cory McAboy
              Dec 3 '18 at 17:36














            0












            0








            0







            Here is how I did it. Do note that this is in Typescript.



            You would define your resolvers in separate files, such as this:



            import { DateBidListResolvers } from "../../types/generated";

            export const DateBidList: DateBidListResolvers.Type = {
            ...DateBidListResolvers.defaultResolvers,

            list: (_, __) => { // This is an example resolver of Type DateBidList
            throw new Error("Resolver not implemented");
            }
            };


            Then you would aggregate them together in a single file like this:



            import { Resolvers } from "../../types/generated";

            import { Query } from "./Query";
            import { User } from "./User";
            import { DateBid } from "./DateBid";
            import { DateItem } from "./DateItem";
            import { Match } from "./Match";
            import { Mutation } from "./Mutation";
            import { Subscription } from "./Subscription";
            import { DateBidList } from "./DateBidList";
            import { DateList } from "./DateList";
            import { Following } from "./Following";
            import { MatchList } from "./MatchList";
            import { Message } from "./Message";
            import { MessageItem } from "./MessageItem";
            import { Queue } from "./Queue";

            export const resolvers: Resolvers = {
            DateBid,
            DateBidList,
            DateItem,
            DateList,
            Following,
            Match,
            MatchList,
            Message,
            MessageItem,
            Mutation,
            Query,
            Queue,
            Subscription,
            User
            };


            You could then import that resolvers export into your configuration setup:



            import { resolvers } from './resolvers/index';

            // ... other imports here

            export const server = {
            typeDefs,
            resolvers,
            playground,
            context,
            dataSources,
            };

            export default new ApolloServer(server);


            I hope this helps!






            share|improve this answer















            Here is how I did it. Do note that this is in Typescript.



            You would define your resolvers in separate files, such as this:



            import { DateBidListResolvers } from "../../types/generated";

            export const DateBidList: DateBidListResolvers.Type = {
            ...DateBidListResolvers.defaultResolvers,

            list: (_, __) => { // This is an example resolver of Type DateBidList
            throw new Error("Resolver not implemented");
            }
            };


            Then you would aggregate them together in a single file like this:



            import { Resolvers } from "../../types/generated";

            import { Query } from "./Query";
            import { User } from "./User";
            import { DateBid } from "./DateBid";
            import { DateItem } from "./DateItem";
            import { Match } from "./Match";
            import { Mutation } from "./Mutation";
            import { Subscription } from "./Subscription";
            import { DateBidList } from "./DateBidList";
            import { DateList } from "./DateList";
            import { Following } from "./Following";
            import { MatchList } from "./MatchList";
            import { Message } from "./Message";
            import { MessageItem } from "./MessageItem";
            import { Queue } from "./Queue";

            export const resolvers: Resolvers = {
            DateBid,
            DateBidList,
            DateItem,
            DateList,
            Following,
            Match,
            MatchList,
            Message,
            MessageItem,
            Mutation,
            Query,
            Queue,
            Subscription,
            User
            };


            You could then import that resolvers export into your configuration setup:



            import { resolvers } from './resolvers/index';

            // ... other imports here

            export const server = {
            typeDefs,
            resolvers,
            playground,
            context,
            dataSources,
            };

            export default new ApolloServer(server);


            I hope this helps!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 29 '18 at 19:24

























            answered Nov 29 '18 at 19:13









            Cory McAboyCory McAboy

            1314




            1314













            • Never worked with Typescript. Will take a look at how to use it. Thanks.

              – user9877232
              Dec 1 '18 at 13:24











            • You don't need Typescript to do this. It was just to note on some of the lingo in the code. I would appreciate it if you would select my answer rather than yours so I get the points.

              – Cory McAboy
              Dec 3 '18 at 17:36



















            • Never worked with Typescript. Will take a look at how to use it. Thanks.

              – user9877232
              Dec 1 '18 at 13:24











            • You don't need Typescript to do this. It was just to note on some of the lingo in the code. I would appreciate it if you would select my answer rather than yours so I get the points.

              – Cory McAboy
              Dec 3 '18 at 17:36

















            Never worked with Typescript. Will take a look at how to use it. Thanks.

            – user9877232
            Dec 1 '18 at 13:24





            Never worked with Typescript. Will take a look at how to use it. Thanks.

            – user9877232
            Dec 1 '18 at 13:24













            You don't need Typescript to do this. It was just to note on some of the lingo in the code. I would appreciate it if you would select my answer rather than yours so I get the points.

            – Cory McAboy
            Dec 3 '18 at 17:36





            You don't need Typescript to do this. It was just to note on some of the lingo in the code. I would appreciate it if you would select my answer rather than yours so I get the points.

            – Cory McAboy
            Dec 3 '18 at 17:36













            0














            I found a way to do it very easy actually. In the schema.js I can use lodash merge to combine multiple resolver files and for the typedefs I just use an array. This way I can split everything into seperate files.



            const { merge } = require("lodash");

            module.exports = makeExecutableSchema({
            typeDefs: [typeDefs, userTypeDefs],
            resolvers: merge(resolvers, userResolvers)
            });





            share|improve this answer




























              0














              I found a way to do it very easy actually. In the schema.js I can use lodash merge to combine multiple resolver files and for the typedefs I just use an array. This way I can split everything into seperate files.



              const { merge } = require("lodash");

              module.exports = makeExecutableSchema({
              typeDefs: [typeDefs, userTypeDefs],
              resolvers: merge(resolvers, userResolvers)
              });





              share|improve this answer


























                0












                0








                0







                I found a way to do it very easy actually. In the schema.js I can use lodash merge to combine multiple resolver files and for the typedefs I just use an array. This way I can split everything into seperate files.



                const { merge } = require("lodash");

                module.exports = makeExecutableSchema({
                typeDefs: [typeDefs, userTypeDefs],
                resolvers: merge(resolvers, userResolvers)
                });





                share|improve this answer













                I found a way to do it very easy actually. In the schema.js I can use lodash merge to combine multiple resolver files and for the typedefs I just use an array. This way I can split everything into seperate files.



                const { merge } = require("lodash");

                module.exports = makeExecutableSchema({
                typeDefs: [typeDefs, userTypeDefs],
                resolvers: merge(resolvers, userResolvers)
                });






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 3 '18 at 8:00







                user9877232





































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466486%2fsplit-the-graphql-resolvers-file-into-seperatefiles%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin