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