Auto-generated resolvers

In the previous section, you learned how to use custom resolvers to authenticate users and manage access to documents. In this section, you learn how to automatically generate user-defined functions (UDFs) for CRUD operations on simple GraphQL types in Fauna.

Auto-generated UDF resolvers

In Uploading your schema you learned that Fauna automatically generates queries and mutations for types you specify in your schema. You can generate UDFs for these resolvers by adding the @generateUDFResolvers directive to the type definitions in your schema.

Auto-generated UDF resolvers are only available for simple types, that is, types that do not have relations. Add the following Category type to your schema with the @generateUDFResolvers directive.

type Category @generateUDFResolvers {
    name: String!
    description: String!
}

Return to the GraphQL section of the Fauna dashboard and replace your schema with the updated schema.

Navigate to the Functions section of the Fauna dashboard and notice that Fauna has created five new UDFs for you:

  • createCategory - Creates a new Category with the information you specify.
  • deleteCategory - Deletes a Category with the specified ID.
  • findCategoryByID - Returns the information for a Category with the specified ID.
  • listCategory - Returns a paginated list of all Category objects.
  • updateCategory - Updates the information for a Category with the specified ID.
Five new user-defined functions

Each UDF is associated with the GraphQL query or mutation of the same name. For example, the listCategory UDF shown in the previous image is the implementation of the listCategory GraphQL query.

The listCategory UDF and GraphQL query demonstrate how to handle pagination in custom resolvers.

Changing auto-generated UDF resolvers

Once you have generated UDFs, you can change the behavior of a GraphQL query or mutation by editing the relevant UDF. For example, you can modify the listCategory UDF to return only the Categories that were created by a specific user, or are not “soft deleted.”

Do not change the parameters or the shape of the return value of a UDF. If you do, the associated GraphQL query or mutation will fail due to type checks!

Removing auto-generated UDF resolvers

If you want to return to using Fauna’s auto-generated queries and mutations, you can remove the auto-generated UDF resolvers by removing the @generateUDFResolvers directive from the type definition in your schema. For example, to remove the connection to the auto-generated resolvers for the Category type, update the type definition in your schema as follows.

type Category {
    name: String!
    description: String!
}

Return to the GraphQL section of the Fauna dashboard and replace your schema with the updated schema. Navigate to the Functions section of the Fauna dashboard and notice that the five auto-generated UDFs remain, but they are no longer connected to your GraphQL queries and mutations. You can safely remove these UDFs.

Review

In this section, you learned how to automatically generate user-defined functions (UDFs) for CRUD operations on simple GraphQL types in Fauna.

Congratulations! You have completed the first chapter of this workshop, Getting started with Fauna.

In the next chapter, Building with Fauna, you apply what you have learned so far to build a fullstack serverless web app with Fauna, GraphQL, and Next.js or SvelteKit.