Resolvers

A resolver class is only needed for the fields that take logic. Everything else is read off the parent value.

The convention

For a schema type TypeName with field fieldName, the module looks for {resolverBasePackage}/{TypeName}Resolver.bx. If the class exists and has a fieldName() method, that method is called. Otherwise the field is read from the parent:

  • from a struct, the key of the same name;
  • from a class instance, get{FieldName}() (generated by accessors="true" or written by hand), else a public member of that name, else null.

Arguments

Resolver methods receive four named arguments:

bxsany function fieldName( any source, struct args, any context, any env ){
}
ArgumentWhat it holds
sourceThe parent object: whatever the parent resolver returned. Null for root Query and Mutation fields, so don't mark it required.
argsThe field's GraphQL arguments as an ordinary struct. graphql-java's maps and lists arrive as structs and arrays; every other value arrives as it was coerced, so a custom scalar's parseValue result is passed as it is.
contextWhatever was passed as context to execute(), typically the current request.
envThe raw graphql.schema.DataFetchingEnvironment, for advanced use such as batch loading.

Example

schema.graphqls:

graphqltype Query {
	widget(id: ID!): Widget
}

type Widget {
	id: ID!
	name: String
	slug: String
}

models/resolvers/QueryResolver.bx:

bxsclass {
	any function widget( any source, required struct args, any context, any env ){
		return { "id" : arguments.args.id, "name" : "Test Widget", "slug" : "test-widget" };
	}
}

No WidgetResolver.bx is needed: id, name and slug are read off the struct widget() returned. If slug later has to be computed, add models/resolvers/WidgetResolver.bx with only a slug() method; id and name keep resolving on their own.

Mutations

Mutation is another type name to the same convention: MutationResolver.bx, with the same four arguments. Top-level mutation fields in one request run one after another, as the GraphQL spec requires, where top-level query fields may run in parallel. graphql-java handles that; there is nothing to configure.

Dependencies

Resolver classes are built with a bare, no-argument new unless you set instanceFactory, which is asked for each one with kind set to "resolver". Without it, a resolver reaches its dependencies by whatever means the rest of your app uses, such as an application-scope reference.

Where resolvers are looked for

resolverBasePackage is a dotted package path (models.resolvers), resolved the way expandPath() resolves any relative path. A value containing / is treated as a literal path instead.

expandPath() starts from the entry pointexpandPath() resolves relative to the entry point of the running script, not the file that calls it. When boxlang app.bxs starts your app, models.resolvers is looked for beside app.bxs, wherever the settings struct was written.