Spring GraphQLmultiple schemas with Query per file

Spring GraphQLmultiple schemas with Query per file


1

with Spring-GraphQl if I have following two schemas in the resources/graphql folder:

schema1:

type Query {
  bookById(id: ID): Book
}

type Book {
  id: ID
  name: String
  pageCount: Int
  author: Author
}

type Author {
  id: ID
  firstName: String
  lastName: String
}

schema2:

type Query {
  personByName(name: String): Person
}

type Person {
  id: ID
  firstName: String
  lastName: String
}

Spring-GraphQL seems to be merging them into one GraphQL schema file and starting of Spring-Boot Graphql app ends with following error:

Caused by: graphql.schema.idl.errors.SchemaProblem: errors=[‘Query’ type [@1:1] tried to redefine existing ‘Query’ type [@1:1]]

When I change it to:

schema1:

type Query {
  bookById(id: ID): Book
  personByName(name: String): Person
}

schema2:

type Book {
  id: ID
  name: String
  pageCount: Int
  author: Author
}

type Author {
  id: ID
  firstName: String
  lastName: String
}

type Person {
  id: ID
  firstName: String
  lastName: String
}

it works perfectly good and I am able to call both queries with graphiql. How graphql spring works with multiple schemas? It seems spring-graphql merges files into one schema so multiple Query types per file breaks the app.

Thanks for answer.

2

  • Thomas, were you able to resolve this issue?

    – shortduck

    Feb 9 at 15:56

  • Hi, yes I followed advice from Brian Clozel and it worked. I marked his answer as helpful.

    – Tomas Kloucek

    Feb 10 at 10:59


2 Answers
2


1

Spring GraphQL is loading all schema resources under the configured location and is using TypeDefinitionRegistry::merge to create a single schema out of them.

I think that redifining any type (even the Query one) should raise an error, otherwise this could hide important issues and conflicting schema definitions. That’s what GraphQL Java’s TypeDefinitionRegistry is doing.

You can organize your schema files like this:

graphql/schema.graphqls

type Query {

}

// add common directives, scalars, etc

graphql/books.graphqls

extend type Query {
  bookById(id: ID): Book
}

type Book {
  id: ID
  name: String
  pageCount: Int
  author: Author
}

type Author {
  id: ID
  firstName: String
  lastName: String
}

graphql/person.graphqls

extend type Query {
  personByName(name: String): Person
}

type Person {
  id: ID
  firstName: String
  lastName: String
}

0


-1

Brian Klosel’s solution works with several query files 🙂 But when I try to run the tests, I get one exception. Maybe someone knows how to solve it?

New contributor

Serhii Moskalenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1



Leave a Reply

Your email address will not be published. Required fields are marked *