Semantic Introspection
Semantic introspection is experimental. It follows the GraphQL AI working group's
Semantic Introspection RFC, which is not
final, so its API may change in a minor release. Opt in with @OptIn(ExperimentalSemanticIntrospection::class).
Large schemas are too big to hand to an LLM. Semantic introspection lets an agent ask the server which schema members match a natural language question, then read only those through standard introspection.
{
_search(query: "what are the releases of a repository?", first: 3) {
coordinate
score
pathsToRoot
}
}
{
"data": {
"_search": [
{
"coordinate": "Repository.releases",
"score": 0.92,
"pathsToRoot": [["Query.repository", "Repository.releases"]]
}
]
}
}
Resolving searches
GraphQL Kotlin adds the field and its type; the application ranks the schema by implementing SemanticSearchResolver.
@OptIn(ExperimentalSemanticIntrospection::class)
class SchemaSearch : SemanticSearchResolver {
override suspend fun search(environment: DataFetchingEnvironment, request: SemanticSearchRequest): List<SemanticSearchResult> {
val schema = environment.graphQLSchema
// rank schema coordinates for request.query, e.g. with an index built once per schema
return listOf(
SemanticSearchResult(
coordinate = "Repository.releases",
pathsToRoot = listOf(listOf("Query.repository", "Repository.releases")),
score = 0.92
)
)
}
}
The resolver owns ranking:
- return at most
request.firstresults, best match first, each scoring at leastrequest.minScorewhen it is set - return schema coordinates of visible schema members
(
Type,Type.field,Input.field,Enum.VALUE,Type.field(arg:),@directive,@directive(arg:)), andpathsToRootas lists ofType.fieldcoordinates from a root field. Results that break these rules fail the field rather than being trimmed; paths through fields hidden by field visibility are dropped - read the schema from
environment.graphQLSchema, not by injecting it: the search is part of the schema it searches - throw a
GraphQLError(for exampleGraphqlErrorException) for an error meant for the client; other exceptions go to theDataFetcherExceptionHandler - apply its own limits on query length, timeouts and concurrency when it calls a model or another service
The resolver runs in the request's CoroutineScope when the GraphQL context has one, like suspend data fetchers.
Enabling it
With the Spring server, register a SemanticSearchResolver bean. Query._search is added to the schema bean unless
graphql.introspection.enabled is false.
Without Spring, apply it to the final schema:
val schema = toSchema(config, queries).withSemanticIntrospection(SchemaSearch())
The Ktor server builds its schema internally and does not support semantic introspection yet.
Like introspection, the field honors Introspection.enabledJvmWide(false) and the per request
Introspection.INTROSPECTION_DISABLED context flag, and withSemanticIntrospection refuses schemas built with
introspection disabled.
Differences from the RFC
- Names: the RFC's
__searchand__SearchResultare_searchand_SearchResult. graphql-java reserves names starting with__for its built-in introspection. - Results are coordinates only: there is no
definitionfield and no__definitionslookup. The RFC's definition union of introspection types does not survive an introspection round trip in graphql-java tooling. Read definitions with standard introspection, for example__type(name: "Repository") { fields { name description } }. - No pagination: there is no
afterargument orcursorfield; ask for more results withfirst. - Nullable list:
_searchreturns[_SearchResult!], so a failed search nulls only_search.
Printed SDL and federation
_search belongs to the running service, not to schemas composed or stitched from SDL. The print() extension leaves
it out unless introspection types are printed, and for federated services _service { sdl } never includes it. Query a
subgraph directly to search it; a federated router does not expose _search.