1

I'm using the graphql-js npm package (not Apollo Server or similar). I have my schemas separated out into different files so need to combine these somehow.

import { graphql, buildSchema } from 'graphql';
import { schema as bookSchema } from './book';
import { schema as authorSchema } from './author';
import root from './root';

const schema = buildSchema(bookSchema + authorSchema);

export default ({ query, variables }) => graphql(schema, query, root, null, variables);

https://github.com/graphql/graphql-js

In author.js:

export const schema = `
  type Query {
    getAuthors: [Author]
  }

  type Author {
    name: String
    books: [Book]
  }
`;

In book.js:

export const schema = `
  type Query {
    getBooks: [Book]
  }

  type Book {
    title: String
    author: Author
  }
`;

The Book and Author types can be combined fine but I get this error:

Error: There can be only one type named "Query".

I have seen extend used to solve this in other libraries but when I tried the getBooks query isn't picked up in the playground.

export const schema = `
  extend type Query {
    getBooks: [Book]
  }

  type Book {
    title: String
    author: Author
  }
`;
Evanss
  • 23,390
  • 94
  • 282
  • 505
  • Can you please update your question to include the actual issue you're seeing? I expect because you're defining `Query` twice, you're seeing an error to that effect, but that's not clear from the question in its current state. It's generally good to include the full error message if you're seeing one. This helps both answerers and future users searching for the same error. – Daniel Rearden May 31 '19 at 12:59
  • 1
    As an aside, you probably [don't want to use buildSchema](https://stackoverflow.com/a/53987189/6024220). – Daniel Rearden May 31 '19 at 13:01

0 Answers0