Kepler
Kepler

Reputation: 429

How to parse multiple schema files in GraphQL Java

I am using GraphQL with java. I have several schema files to be parsed. Can anybody please let me know whether there is a way to do it. I have used the below way which lets only one schema file to be parsed.

@Value("classpath:dashboard.graphql")
private Resource schemaResource;

File schemaFile = schemaResource.getFile();
TypeDefinitionRegistry definitionRegistry = new SchemaParser().parse(schemaFile);

Supose I have two schema files such as dashboard.graphql and student.graphql. Then how can we parse these two files ?

Can anybody please explain me.

Thanks

Upvotes: 6

Views: 10535

Answers (2)

Doctor Eval
Doctor Eval

Reputation: 3961

I know this is an oldish question, but the SO answer appears before the official GraphQL-java documentation, so I thought I'd post it here:

SchemaParser schemaParser = new SchemaParser();
SchemaGenerator schemaGenerator = new SchemaGenerator();

File schemaFile1 = loadSchema("starWarsSchemaPart1.graphqls");
File schemaFile2 = loadSchema("starWarsSchemaPart2.graphqls");
File schemaFile3 = loadSchema("starWarsSchemaPart3.graphqls");

TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();

// each registry is merged into the main registry
typeRegistry.merge(schemaParser.parse(schemaFile1));
typeRegistry.merge(schemaParser.parse(schemaFile2));
typeRegistry.merge(schemaParser.parse(schemaFile3));

GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, buildRuntimeWiring());

Upvotes: 7

svensemilia
svensemilia

Reputation: 27

we did it like this using a schemaParserBuilder:

SchemaParserBuilder parser = SchemaParser.newParser();
parser.files(arrayOfFileNames);//fill the array with the paths to your schema files
//define other properties like resolvers here
GraphQLSchema schema = parser.build().makeExecutableSchema();

Another idea if you need that TypeDefinitionRegistry-object. Just read all your schema files separatly, merge them into a String and use schemaParser.parse(String schema). In fact parser.files is doing exactly that.

Upvotes: 0

Related Questions