Skip to content

How to validate graphql query against the schema without execution? #2737

@arjunballa

Description

@arjunballa

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());
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions