How can we pass datasource and merged graphql schema and resolvers in ApolloServer from apollo-server module?

Multi tool use
Multi tool use












0















I have a situation where , I have schema and reolvers are sitting in various different files. I have imported the schema and resolver to server.js file which is responsible for creating an apollo server. For now, one set of resolver and queries , I am importing and another set of queries and resolver is already there in server.js file.



This is the content for server.js file.



    import "graphql-import-node";
import { ApolloServer, gql } from "apollo-server";
import { makeExecutableSchema } from "graphql-tools";
import merge from "lodash/merge";
import GithubFetchHelper from "./github/GithubFetchHelper";
import {
resolvers as GithubResolvers,
typeDefs as GithubTypeDef
} from "./github";

/**
* Error Handler. Provides full stack - remove for production
*/

const books = [
{
title: "Harry Potter and the Chamber of Secrets",
author: "J.K. Rowling"
},
{
title: "Jurassic Park",
author: "Michael Crichton"
}
];

const typeDefs = gql`
# Comments in GraphQL are defined with the hash (#) symbol.

# This "Book" type can be used in other type declarations.
type Book {
title: String
author: String
}

# The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.)
type Query {
books: [Book]
}
`;

const resolvers = {
Query: {
books: () => books
}
};

// typeDefs: [
// project.typeDefs,
// task.typeDefs,
// search.typeDefs
// ].join(' '),
// resolvers: merge({}, project.resolvers, task.resolvers, search.resolvers),
// context: {
// models: {
// project: project.model,
// task: task.model
// },
// loaders: loaders()
// }

const server = new ApolloServer({
typeDefs: [typeDefs, GithubTypeDef],
resolvers: merge({}, resolvers, GithubResolvers),
dataSources: () => {
return {
gitHubApi: new GithubFetchHelper()
};
}
});

server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});

export default server;


Now When i run the server, I get the following error. Any help is appreciated.



Error: Type "Query" was defined more than once.


My tries I have referred apollo documentation for data sources and for merging multiple schema and resolvers using gql from graphql-tools. But i didnot get any implementation of gql where they have used datasources also.



Any help is appreciated.



Note: I am quite new to Graphql










share|improve this question



























    0















    I have a situation where , I have schema and reolvers are sitting in various different files. I have imported the schema and resolver to server.js file which is responsible for creating an apollo server. For now, one set of resolver and queries , I am importing and another set of queries and resolver is already there in server.js file.



    This is the content for server.js file.



        import "graphql-import-node";
    import { ApolloServer, gql } from "apollo-server";
    import { makeExecutableSchema } from "graphql-tools";
    import merge from "lodash/merge";
    import GithubFetchHelper from "./github/GithubFetchHelper";
    import {
    resolvers as GithubResolvers,
    typeDefs as GithubTypeDef
    } from "./github";

    /**
    * Error Handler. Provides full stack - remove for production
    */

    const books = [
    {
    title: "Harry Potter and the Chamber of Secrets",
    author: "J.K. Rowling"
    },
    {
    title: "Jurassic Park",
    author: "Michael Crichton"
    }
    ];

    const typeDefs = gql`
    # Comments in GraphQL are defined with the hash (#) symbol.

    # This "Book" type can be used in other type declarations.
    type Book {
    title: String
    author: String
    }

    # The "Query" type is the root of all GraphQL queries.
    # (A "Mutation" type will be covered later on.)
    type Query {
    books: [Book]
    }
    `;

    const resolvers = {
    Query: {
    books: () => books
    }
    };

    // typeDefs: [
    // project.typeDefs,
    // task.typeDefs,
    // search.typeDefs
    // ].join(' '),
    // resolvers: merge({}, project.resolvers, task.resolvers, search.resolvers),
    // context: {
    // models: {
    // project: project.model,
    // task: task.model
    // },
    // loaders: loaders()
    // }

    const server = new ApolloServer({
    typeDefs: [typeDefs, GithubTypeDef],
    resolvers: merge({}, resolvers, GithubResolvers),
    dataSources: () => {
    return {
    gitHubApi: new GithubFetchHelper()
    };
    }
    });

    server.listen().then(({ url }) => {
    console.log(`🚀 Server ready at ${url}`);
    });

    export default server;


    Now When i run the server, I get the following error. Any help is appreciated.



    Error: Type "Query" was defined more than once.


    My tries I have referred apollo documentation for data sources and for merging multiple schema and resolvers using gql from graphql-tools. But i didnot get any implementation of gql where they have used datasources also.



    Any help is appreciated.



    Note: I am quite new to Graphql










    share|improve this question

























      0












      0








      0








      I have a situation where , I have schema and reolvers are sitting in various different files. I have imported the schema and resolver to server.js file which is responsible for creating an apollo server. For now, one set of resolver and queries , I am importing and another set of queries and resolver is already there in server.js file.



      This is the content for server.js file.



          import "graphql-import-node";
      import { ApolloServer, gql } from "apollo-server";
      import { makeExecutableSchema } from "graphql-tools";
      import merge from "lodash/merge";
      import GithubFetchHelper from "./github/GithubFetchHelper";
      import {
      resolvers as GithubResolvers,
      typeDefs as GithubTypeDef
      } from "./github";

      /**
      * Error Handler. Provides full stack - remove for production
      */

      const books = [
      {
      title: "Harry Potter and the Chamber of Secrets",
      author: "J.K. Rowling"
      },
      {
      title: "Jurassic Park",
      author: "Michael Crichton"
      }
      ];

      const typeDefs = gql`
      # Comments in GraphQL are defined with the hash (#) symbol.

      # This "Book" type can be used in other type declarations.
      type Book {
      title: String
      author: String
      }

      # The "Query" type is the root of all GraphQL queries.
      # (A "Mutation" type will be covered later on.)
      type Query {
      books: [Book]
      }
      `;

      const resolvers = {
      Query: {
      books: () => books
      }
      };

      // typeDefs: [
      // project.typeDefs,
      // task.typeDefs,
      // search.typeDefs
      // ].join(' '),
      // resolvers: merge({}, project.resolvers, task.resolvers, search.resolvers),
      // context: {
      // models: {
      // project: project.model,
      // task: task.model
      // },
      // loaders: loaders()
      // }

      const server = new ApolloServer({
      typeDefs: [typeDefs, GithubTypeDef],
      resolvers: merge({}, resolvers, GithubResolvers),
      dataSources: () => {
      return {
      gitHubApi: new GithubFetchHelper()
      };
      }
      });

      server.listen().then(({ url }) => {
      console.log(`🚀 Server ready at ${url}`);
      });

      export default server;


      Now When i run the server, I get the following error. Any help is appreciated.



      Error: Type "Query" was defined more than once.


      My tries I have referred apollo documentation for data sources and for merging multiple schema and resolvers using gql from graphql-tools. But i didnot get any implementation of gql where they have used datasources also.



      Any help is appreciated.



      Note: I am quite new to Graphql










      share|improve this question














      I have a situation where , I have schema and reolvers are sitting in various different files. I have imported the schema and resolver to server.js file which is responsible for creating an apollo server. For now, one set of resolver and queries , I am importing and another set of queries and resolver is already there in server.js file.



      This is the content for server.js file.



          import "graphql-import-node";
      import { ApolloServer, gql } from "apollo-server";
      import { makeExecutableSchema } from "graphql-tools";
      import merge from "lodash/merge";
      import GithubFetchHelper from "./github/GithubFetchHelper";
      import {
      resolvers as GithubResolvers,
      typeDefs as GithubTypeDef
      } from "./github";

      /**
      * Error Handler. Provides full stack - remove for production
      */

      const books = [
      {
      title: "Harry Potter and the Chamber of Secrets",
      author: "J.K. Rowling"
      },
      {
      title: "Jurassic Park",
      author: "Michael Crichton"
      }
      ];

      const typeDefs = gql`
      # Comments in GraphQL are defined with the hash (#) symbol.

      # This "Book" type can be used in other type declarations.
      type Book {
      title: String
      author: String
      }

      # The "Query" type is the root of all GraphQL queries.
      # (A "Mutation" type will be covered later on.)
      type Query {
      books: [Book]
      }
      `;

      const resolvers = {
      Query: {
      books: () => books
      }
      };

      // typeDefs: [
      // project.typeDefs,
      // task.typeDefs,
      // search.typeDefs
      // ].join(' '),
      // resolvers: merge({}, project.resolvers, task.resolvers, search.resolvers),
      // context: {
      // models: {
      // project: project.model,
      // task: task.model
      // },
      // loaders: loaders()
      // }

      const server = new ApolloServer({
      typeDefs: [typeDefs, GithubTypeDef],
      resolvers: merge({}, resolvers, GithubResolvers),
      dataSources: () => {
      return {
      gitHubApi: new GithubFetchHelper()
      };
      }
      });

      server.listen().then(({ url }) => {
      console.log(`🚀 Server ready at ${url}`);
      });

      export default server;


      Now When i run the server, I get the following error. Any help is appreciated.



      Error: Type "Query" was defined more than once.


      My tries I have referred apollo documentation for data sources and for merging multiple schema and resolvers using gql from graphql-tools. But i didnot get any implementation of gql where they have used datasources also.



      Any help is appreciated.



      Note: I am quite new to Graphql







      typescript graphql apollo apollo-client apollo-server






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 15:46









      simbathesailorsimbathesailor

      2,26411119




      2,26411119
























          2 Answers
          2






          active

          oldest

          votes


















          0














          From the code, you are passing 2 typedefs as an array, probably the reason why you get the error. You need to stitch the schema together. See:




          • https://www.apollographql.com/docs/graphql-tools/schema-stitching.html

          • https://blog.apollographql.com/the-next-generation-of-schema-stitching-2716b3b259c0






          share|improve this answer































            0














            Assuming you have imported schema(s) and resolver(s) in your main js file you can stitch it in following way.
            you can use require-graphql-file module to simplify graphQL schema import.



            import {merge } from 'lodash'
            import requireGraphQLFile from 'require-graphql-file';
            import {makeExecutableSchema } from 'apollo-server';

            import resolver1 from './resolvers/resolver1';
            import resolver2 from './resolvers/resolver2';
            import resolver3 from './resolvers/resolver3';

            const schema1= requireGraphQLFile('./schema/schema1');
            const schema2= requireGraphQLFile('./schema/schema2');
            const schema3= requireGraphQLFile('./schema/schema3');

            const resolvers = merge(resolver1, resolver2, resolver3)
            const typeDef = gql `
            type Query {
            getData(filters:Filter,limit:Int,offset:Int):Result
            }`;

            const schema = makeExecutableSchema({
            typeDefs: [typeDef, schema1,schema2, schema3],
            resolvers
            });

            const server = new ApolloServer({
            schema
            });





            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%2f54009229%2fhow-can-we-pass-datasource-and-merged-graphql-schema-and-resolvers-in-apolloserv%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














              From the code, you are passing 2 typedefs as an array, probably the reason why you get the error. You need to stitch the schema together. See:




              • https://www.apollographql.com/docs/graphql-tools/schema-stitching.html

              • https://blog.apollographql.com/the-next-generation-of-schema-stitching-2716b3b259c0






              share|improve this answer




























                0














                From the code, you are passing 2 typedefs as an array, probably the reason why you get the error. You need to stitch the schema together. See:




                • https://www.apollographql.com/docs/graphql-tools/schema-stitching.html

                • https://blog.apollographql.com/the-next-generation-of-schema-stitching-2716b3b259c0






                share|improve this answer


























                  0












                  0








                  0







                  From the code, you are passing 2 typedefs as an array, probably the reason why you get the error. You need to stitch the schema together. See:




                  • https://www.apollographql.com/docs/graphql-tools/schema-stitching.html

                  • https://blog.apollographql.com/the-next-generation-of-schema-stitching-2716b3b259c0






                  share|improve this answer













                  From the code, you are passing 2 typedefs as an array, probably the reason why you get the error. You need to stitch the schema together. See:




                  • https://www.apollographql.com/docs/graphql-tools/schema-stitching.html

                  • https://blog.apollographql.com/the-next-generation-of-schema-stitching-2716b3b259c0







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 0:32









                  xwleexwlee

                  623721




                  623721

























                      0














                      Assuming you have imported schema(s) and resolver(s) in your main js file you can stitch it in following way.
                      you can use require-graphql-file module to simplify graphQL schema import.



                      import {merge } from 'lodash'
                      import requireGraphQLFile from 'require-graphql-file';
                      import {makeExecutableSchema } from 'apollo-server';

                      import resolver1 from './resolvers/resolver1';
                      import resolver2 from './resolvers/resolver2';
                      import resolver3 from './resolvers/resolver3';

                      const schema1= requireGraphQLFile('./schema/schema1');
                      const schema2= requireGraphQLFile('./schema/schema2');
                      const schema3= requireGraphQLFile('./schema/schema3');

                      const resolvers = merge(resolver1, resolver2, resolver3)
                      const typeDef = gql `
                      type Query {
                      getData(filters:Filter,limit:Int,offset:Int):Result
                      }`;

                      const schema = makeExecutableSchema({
                      typeDefs: [typeDef, schema1,schema2, schema3],
                      resolvers
                      });

                      const server = new ApolloServer({
                      schema
                      });





                      share|improve this answer






























                        0














                        Assuming you have imported schema(s) and resolver(s) in your main js file you can stitch it in following way.
                        you can use require-graphql-file module to simplify graphQL schema import.



                        import {merge } from 'lodash'
                        import requireGraphQLFile from 'require-graphql-file';
                        import {makeExecutableSchema } from 'apollo-server';

                        import resolver1 from './resolvers/resolver1';
                        import resolver2 from './resolvers/resolver2';
                        import resolver3 from './resolvers/resolver3';

                        const schema1= requireGraphQLFile('./schema/schema1');
                        const schema2= requireGraphQLFile('./schema/schema2');
                        const schema3= requireGraphQLFile('./schema/schema3');

                        const resolvers = merge(resolver1, resolver2, resolver3)
                        const typeDef = gql `
                        type Query {
                        getData(filters:Filter,limit:Int,offset:Int):Result
                        }`;

                        const schema = makeExecutableSchema({
                        typeDefs: [typeDef, schema1,schema2, schema3],
                        resolvers
                        });

                        const server = new ApolloServer({
                        schema
                        });





                        share|improve this answer




























                          0












                          0








                          0







                          Assuming you have imported schema(s) and resolver(s) in your main js file you can stitch it in following way.
                          you can use require-graphql-file module to simplify graphQL schema import.



                          import {merge } from 'lodash'
                          import requireGraphQLFile from 'require-graphql-file';
                          import {makeExecutableSchema } from 'apollo-server';

                          import resolver1 from './resolvers/resolver1';
                          import resolver2 from './resolvers/resolver2';
                          import resolver3 from './resolvers/resolver3';

                          const schema1= requireGraphQLFile('./schema/schema1');
                          const schema2= requireGraphQLFile('./schema/schema2');
                          const schema3= requireGraphQLFile('./schema/schema3');

                          const resolvers = merge(resolver1, resolver2, resolver3)
                          const typeDef = gql `
                          type Query {
                          getData(filters:Filter,limit:Int,offset:Int):Result
                          }`;

                          const schema = makeExecutableSchema({
                          typeDefs: [typeDef, schema1,schema2, schema3],
                          resolvers
                          });

                          const server = new ApolloServer({
                          schema
                          });





                          share|improve this answer















                          Assuming you have imported schema(s) and resolver(s) in your main js file you can stitch it in following way.
                          you can use require-graphql-file module to simplify graphQL schema import.



                          import {merge } from 'lodash'
                          import requireGraphQLFile from 'require-graphql-file';
                          import {makeExecutableSchema } from 'apollo-server';

                          import resolver1 from './resolvers/resolver1';
                          import resolver2 from './resolvers/resolver2';
                          import resolver3 from './resolvers/resolver3';

                          const schema1= requireGraphQLFile('./schema/schema1');
                          const schema2= requireGraphQLFile('./schema/schema2');
                          const schema3= requireGraphQLFile('./schema/schema3');

                          const resolvers = merge(resolver1, resolver2, resolver3)
                          const typeDef = gql `
                          type Query {
                          getData(filters:Filter,limit:Int,offset:Int):Result
                          }`;

                          const schema = makeExecutableSchema({
                          typeDefs: [typeDef, schema1,schema2, schema3],
                          resolvers
                          });

                          const server = new ApolloServer({
                          schema
                          });






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 23 at 17:50

























                          answered Jan 23 at 17:28









                          Amit BhoyarAmit Bhoyar

                          620115




                          620115






























                              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%2f54009229%2fhow-can-we-pass-datasource-and-merged-graphql-schema-and-resolvers-in-apolloserv%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







                              BosEIYX9N
                              jV6v4HblVQ,BS3Sj69o VjBqu 0oeT TS4,KkH,iz uA3O7x

                              Popular posts from this blog

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas