website/versioned_docs/version-v20.0.0/guides/relay-resolvers/introduction.md
Relay Resolvers are a Relay feature which allow you to augment Relay’s GraphQL graph with values that are known only on the client. This allows you to schematize client state in the same way that you model server state, and to use Relay’s familiar data-fetching APIs to access that state. Client state can include both data from client-side data stores as well as derived data that is computed from other values in the graph.
By modeling derived and client state in the graph, Relay can present a unified data access API for product developers. All globally relevant data that a product engineer wants to access can be discovered and efficiently obtained from the same structured GraphQL schema. Additionally resolvers provide a number of runtime benefits:
You can think of resolvers as additional schema types and fields which are defined in your client code and are stitched into your server’s schema. Just like you define resolver methods/functions which model your fields on the server, Relay Resolves are defined using resolver functions.
Relay Resolvers are useful for modeling a number of different kinds of data. Here are some examples of types of data that can be schematized using Relay Resolvers and made available to product code:
Resolvers are defined using exported functions that are annotated with a special @RelayResolver docblock. These docblocks are visible to the Relay compiler, and allow the compiler to build up your client schema and automatically import your function in Relay’s generated artifacts. Resolver functions may be defined in any file in your Relay project, though you may wish to define some convention for where they live within your codebase.
The simplest resolver augments an existing type and does not have any inputs:
/**
* @RelayResolver Query.greeting: String
*/
export function greeting(): string {
return "Hello World";
}
Consuming resolvers is identical to consuming a server field. Product code doesn't need to know which kind of field it is reading.
import {useLazyLoadQuery, graphql} from 'react-relay';
import {useClientQuery, graphql} from 'react-relay';
function Greeting() {
const data = useClientQuery(graphql`
query GreetingQuery {
greeting
}`, {});
return <p>{data.greeting}</p>;
}
:::note
If your query contains only client-defined fields, you will need to use a a different query API to fetch data. Note how this example uses useClientQuery instead of useLazyLoadQuery or usePreloadedQuery. If your query also contains server data, you can use the standard useLazyLoadQuery or usePreloadedQuery APIs.
We intend to remove this requirement in future versions of Relay. :::