@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 returns paginated results.

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.

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.

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.

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!