-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed as not planned
Closed as not planned
Copy link
Labels
Description
Discussed in #2739
Originally posted by arjunballa February 25, 2022
How to validate graphql query against the schema without execution?
I tried below methods and all failed.
@bbakerman @andimarek Any pointers?
Schema: No mutations defined
Request: mutation
Method: ParseAndValidate.validate
Returns it as valid. If I add mutation with dummy FieldDefinition it works.
@Test
public void testValidateWithInvalidRequestUsingSchemaWithOnlyQueryAndInputWithMutation()
throws SchemaProblem, FileNotFoundException {
InputStream in = this.getClass().getResourceAsStream("/only-query.graphql");
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(in);
GraphQLSchema schema = UnExecutableSchemaGenerator.makeUnExecutableSchema(typeDefinitionRegistry);
String request = "mutation MyMutation { mymutation }";
Document inputDocument = new Parser().parseDocument(request);
List<ValidationError> errors = ParseAndValidate.validate(schema, inputDocument);
assertEquals(1, errors.size());
}
Schema: No mutations defined
Request: mutation
Method: ParseAndValidate.parseAndValidate
Returns it as valid. If I add mutation with dummy FieldDefinition it works.
@Test
public void testParseAndValidateWithInvalidRequestUsingSchemaWithOnlyQueryAndInputWithMutation()
throws SchemaProblem, FileNotFoundException {
InputStream in = this.getClass().getResourceAsStream("/only-query.graphql");
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(in);
GraphQLSchema schema = UnExecutableSchemaGenerator.makeUnExecutableSchema(typeDefinitionRegistry);
String request = "mutation MyMutation { mymutation }";
ExecutionInput input = ExecutionInput.newExecutionInput(request).build();
ParseAndValidateResult result = ParseAndValidate.parseAndValidate(schema, input);
assertEquals(1, result.getErrors().size());
}
Schema: Has mutation with not null return type
Request: mutation
Method: ParseAndValidate.parseAndValidate
MockedWiringFactory is expecting the actual return.
Error:
java.lang.AssertionError: expected same:<> was not:<[graphql.execution.MissingRootTypeException:
Schema is not configured for mutations.]>
@Test
public void testMockedWiringFactoryWithValidRequest() throws SchemaProblem, FileNotFoundException {
InputStream in = this.getClass().getResourceAsStream("/mutation-query.graphql");
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(in);
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().wiringFactory(new MockedWiringFactory()).build();
SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();
String request = "mutation MyMutation { mymutation }";
ExecutionInput input = ExecutionInput.newExecutionInput(request).build();
ExecutionResult executionResult = graphQL.execute(input);
assertSame("", executionResult.getErrors().toString());
assertEquals(0, executionResult.getErrors().size());
}