7

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

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 java Commented Oct 4, 2017 at 9:44

2 Answers 2

8

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());
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Documentation link unavalable, try this: graphql-java.com/documentation/schema/…
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.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.