@resolver

Specifies the name of a FaunaDB user-defined function to use instead of a default field value resolver.

Location

Fields within the Query or Mutation types.

Arguments

Argument Type Required Default Description

name

String

No

The field’s name.

The name for the resolver function.

paginated

Boolean

No

false

When true, the resolver accepts three arguments, size, afterCursor, and beforeCursor, and then returns a Page of results. See the Example below, and the UDF that returns a database page.

Description

The @resolver directive marks a Query or Mutation that has an associated user-defined function in the database. Queries to fields annotated with the @resolver directive are resolved by calling the underlying user-defined function, which is a Fauna Query Language Lambda function.

The name of the function is controlled by the name argument, which defaults to the name of the field. The paginated argument controls pagination support for the values returned by the user-defined function.

When a schema is imported into the GraphQL API, resolvers for your schemas fields are automatically generated. When you apply the @resolver directive to a query field, the named resolver replaces the automatically-generated resolver for the field.

User-defined functions cannot be created with GraphQL queries; you must use the Fauna Query Language to call the CreateFunction function.

For more information, see User-defined functions.

Example

The following two FQL queries each create a function:

CreateFunction({
  name: "say_hello",
  body: Query(Lambda([], "hello"))
})
CreateFunction({
  name: "function_names",
  body: Query(Lambda(["size", "afterCursor", "beforeCursor"],
    Map(
      Paginate(Functions()),
      Lambda("ref",
        Select("name", Get(Var("ref")))
      )
    )
  ))
})

The first function, called say_hello, only returns the string hello. The second function, called function_names, returns the list of functions defined in the database. For a more in-depth example, see A UDF that returns a database page.

We can use these function in GraphQL queries by using the following schema:

type Query {
  sayHello: String! @resolver(name: "say_hello")
  functionNames: [String!] @resolver(name: "function_names", paginated: true)
}

With the functions and the schema in place, we can call the first function with this GraphQL query:

{
  sayHello
}

which should produce the following response:

{
  "data": {
    "sayHello": "hello"
  }
}

Calling the second function would look like this:

{
  functionNames {
    data
  }
}

And the response would look like:

{
  "data": {
    "functionNames": {
      "data": [
        "function_names",
        "say_hello"
      ]
    }
  }
}

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!