1
votes

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

2
Where you able to figure out how to achieve this..?Mukesh Kumar

2 Answers

0
votes

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:

0
votes

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
});