Defining Fields
Defining fields on a client type is as simple as defining a resolver function which accepts an instance of your model type as its first argument and returns the field value. Note that the exported function name must match the field name.
Syntaxβ
Relay resolvers are marked via docblocks above a resolver function. @RelayResolver
is the tag to indicate the start of any Relay resolver definition. To define a field on a GraphQL model type TypeName
:
- Docblock
Add TypeName
followed by a dot followed by the field definition using GraphQL's schema definition language: https://spec.graphql.org/June2018/#FieldDefinition
/**
* @RelayResolver TypeName.fieldName(arg1: ArgTypeName): FieldTypeName
*/
A simple field might look something like this:
- Docblock
/**
* @RelayResolver User.name: String
*/
export function name(user: UserModel): string {
return user.name;
}
Relay will take care of efficiently recomputing resolvers when any of their inputs (in this case the model instance) change, so you donβt need to worry about memoizing your resolver function.
This is just a simple resolver that reads from the model type and returns a scalar value. To learn about the full menu of capabilities that resolver fields support see: