Skip to content

client_introspection

etienne-sf edited this page Jun 22, 2024 · 17 revisions

Use GraphQL introspection with the GraphQL client mode

On server side, the introspection is directly managed by graphql-java. There is no additional work to do, to make it work.

On client side, graphql-java-generator makes it easy to execute GraphQL introspection queries. And the query's response is stored in java classes that match the GraphQL introspection schema.

As specified in the GraphQL specification, there are two main queries: __schema and __type.

You can access to the introspection GraphQL capabilities through the _IntrospectionQuery class. This class is generated in the same package as the other classes (see the client exec GraphQL requests page for details).

Introspection queries

You can then execute introspection queries like this:

// your.target.package is the package defined in the pom or build.gradle file, as the 'packageName'
import your.target.package.QueryExecutor;
import your.target.package.__Schema;
import your.target.package.__Type;

@Component
public class MyClass {

	@Autowired
	QueryExecutor executor;

	public __Schema getSchema() {
		return executor.__schema("{types {name fields {name type {name}}}}");
	}

	public __Type getType() {
		return executor.__type("{name fields {name type {name}}}", "MyGraphQLType");
	}
}

__typename in GraphQL types

In the generated code, a field named __typename is added into each java class that is actually a GraphQL type. So you can ask for the GraphQL type name of a response with a query request like this one:

AllFieldCases allFieldCases = queryExecutor.allFieldCases("{id __typename {friends __typename}}", null);

How the code is generated

When the plugin is in client mode, the introspection capabilities are added to the Query type defined in the GraphQL schema you've provided. And the __Schema and __Type are added to your GraphQL schema content. So these types are generated in the same package as the other classes.

Clone this wiki locally