How to parse multiple schema files in GraphQL Java
Asked Answered
H

2

6

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

Huge answered 4/10, 2017 at 9:16 Comment(1)
Hi, In node we are using library merge-graphql-schemas(github.com/okgrow/merge-graphql-schemas). Check if there is any supporting library like this in javaDrollery
T
7

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());
Tragicomedy answered 28/9, 2018 at 1:31 Comment(2)
This is the truth... though this merge is very limited. It doesn't merge queries, hence its purpose to split your 1 schema into bits, but not to merge schemas. Hence that doesn't work. In fact, if you concatenate all schemas into one string and make one schema out of that, you actually have the same result.Snoopy
Documentation link unavalable, try this: graphql-java.com/documentation/schema/…Is
D
0

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.

Dissolve answered 4/10, 2017 at 10:38 Comment(1)
when I use the .files with multiple files, my query object only ever shows from the last file entered in the array, any chance you have run into this?Incredible

© 2022 - 2024 — McMap. All rights reserved.