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