website/versioned_docs/version-v20.0.0/guides/relay-resolvers/defining-fields.md
import {FbInternalOnly, fbContent} from 'docusaurus-plugin-internaldocs-fb/internal'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
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.
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:
<Tabs groupId="resolver" defaultValue="Docblock" values={fbContent({ internal: [ {label: 'Docblock', value: 'Docblock'}, {label: 'Flow', value: 'Flow'}, ], external: [ {label: 'Docblock', value: 'Docblock'}, ] })}> <TabItem value="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
*/
Import and use the Flow type for the object, Relay finds the GraphQL type linked to TypeName, and use the function name as the field name
import {TypeName} from 'TypeObject';
/**
* @RelayResolver
*/
export function fieldName(user: TypeName): string {
return user.name;
}
A simple field might look something like this:
<Tabs groupId="resolver" defaultValue="Docblock" values={fbContent({ internal: [ {label: 'Docblock', value: 'Docblock'}, {label: 'Flow', value: 'Flow'}, ], external: [ {label: 'Docblock', value: 'Docblock'}, ] })}> <TabItem value="Docblock">
/**
* @RelayResolver User.name: String
*/
export function name(user: UserModel): string {
return user.name;
}
/**
* @RelayResolver
*/
export function name(user: UserModel): string {
return user.name;
}
:::note 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:
<FbInternalOnly>If you have a "weak" type, you can easily define a simple resolver that just returns a property from the underlying model. For example, take a resolver being defined on the UserModel that looks like:
/**
* @RelayResolver
*/
export function name(user: UserModel): string {
return user.name;
}
When defining the weak type, this resolver can by automatically generated by using a docblock with @gqlField over the field you want to expose.
/**
* @RelayResolver
*/
export type UserModel = {
/**
* @gqlField
*/
name: string,
}
You can optionally include a description or @deprecated tag in the docblock
/**
* @gqlField
* @deprecated Do not use this field anymore
*
* This is a description. Include more information
* about your field here.
*/