Fauna + Azure Functions
Tags: fauna, azure, serverless
Published: 2020-11-25 06:50:11

This post discusses how to use Fauna and Azure Functions. Fauna provides transactional NoSQL on a global scale with the added bonus of providing ACID transactions out of the box which implies that Fauna can be used for transactions that require high integrity like banking applications. You can read more about Fauna [here](https://docs.fauna.com/fauna/current/introduction.html).

Fauna is quite generous in their free tier. If you are coming from an SQL background, you can read up the comparison between Fauna’s language called FQL and SQL DB [here](https://docs.fauna.com/fauna/current/start/fql_for_sql_users).

### Below is a summary of what we would be building using Fauna and Azure Function:

We are building a todo list app that allows users to login and view their own created todo list. A business logic would be implemented to prevent a user from having access to another user's todo list.

An authorization token would be used to grant users access to only their own todo list.

Communication to Fauna would be made using the GraphQL API provided by Fauna.

Azure Functions would serve as the middleman between Fauna and your client for login and signup requests.

FaunaDB can be used in the client without a server but then there might be cases where you would need to send emails to new users during signup or logins and that is why I chose to use Azure Functions to handle logins and signups.

## The flow

-   The client makes a request to Azure Function to retrieve an authorization token which would enable communication between client and Fauna.
    
-   Azure Function handles two requests from the client: Signup and login. The signup request stores users information and credentials on Fauna and the login request returns an authorization token to the client.
    
-   The client uses the token to make subsequent requests to Fauna.
    

## This tutorial covers:

-   How to create a todo list on Fauna
    
-   How to setup a GraphQL API on Fauna
    
-   Relations in GraphQL schema in Fauna using directives
    
-   How to create custom resolvers for your schema using the resolver directive provided by Fauna
    
-   A little bit of Fauna’s domain specific language called FQL
    
-   How to use Attribute-based access control to restrict a user to having access to personal data
    
-   How to Setup Azure functions
    
-   How to use Apollo Client in Azure functions
    

## Step 1: Creating an Account and Database

-   Create an account on fauna by clicking [here](https://dashboard.fauna.com/accounts/register). After registering you would be redirected to your dashboard
    

![](https://lh4.googleusercontent.com/CLi32pAm12_nxecuI2aY4pS6E7qBVE11MATe914rOU2e8G9u5cG206fc0c-N3ATjUviFy98vBBBzpnBeULYAuSaYrnp2fZcdw0_YBIivLn5nWoCoNEqqxXWY9RrbC9CKrqUT0hFN)

-   Click on the “NEW DATABASE” button.
    
-   Typing “TODO” as the name of the database to be created.
    

![](https://lh3.googleusercontent.com/yv5Dri2rlCDtfNz14ZSNESvdCO1vocIYXcYhii0BwTwXmr3fV8wV12fRtEHdhOL4dO637Y1uElgH9eYBHh9uXQlJsXOeWWfKrGMdVeO2vIBCbh8rFe5xH0VDkHsdvlYBqhCNqLgx)

-   After clicking on the save button you would now have access to the dashboard of your newly created database.
    

![](https://lh3.googleusercontent.com/9HeEcNPxe6EMDRs5Lv-R9EojzpQBNgfsVowP9LGvZvAwXjs1h2Z8btUPBF2tbD9uDkjnXARfk5BYA0zU4HTtCmEfNGmch309RbZTXBIC29ZhPGASoKW7ckaB_uVUug_L16NOo96W)

## Step 2: Importing GraphQL Schema

Not only does Fauna give you access to your data via REST API, it also offers a GraphQL service which you can use to query your database. And if you are a GraphQL fan guy you’ll probably love this feature.

Now the awesome thing I love about Fauna’s GraphQL service is the automatic creation of default resolvers for your schema, which means you don’t need to create your own resolver except you want to integrate some custom logic.

We would continue by creating a schema which I would break in chunks and explain
```
type User {
  username: String! @unique(index: "unique_username")

  password: String!

  todos: [Todo] @relation
}

type Todo {
  text: String!

  completed: Boolean!

  user: User!
}

type Query {
  todos: [Todo!]
}

input CreateUserInput {
  username: String!

  password: String!
}

input LoginUserInput {
  username: String!

  password: String!
}

type Mutation {
  createUser(input: CreateUserInput): User! @resolver(name: "create_user")

  loginUser(input: LoginUserInput): String! @resolver(name: "login_user")
}

  ```

Copy the schema above and paste it locally in your computer using the name “schema.gql”.

Below is the explanation of the schema fields above
```
type User {
  username: String! @unique(index: "unique_username")

  password: String!

  todos: [Todo] @relation
}


 ``` 

In the username field, the unique directive is used to enforce this constraint.

The unique directive accepts an index argument. I chose to use “unique_username” to create the index but you can use whatever name you like.

In Fauna, an Index is used to improve the lookup performance of data in the database. So rather than use the ID of a document (which is similar to a record in an SQL database) to find a specific document or group of documents, with the Index feature you can lookup data using their attributes. For instance, the “unique_username” Index would allow us to search for data via username rather than References( similar to an ID). To know more about types in Fauna click [here](https://docs.fauna.com/fauna/current/api/fql/types?lang=javascript).

  

In the todos field, the @relation directive shows that it is not just a list of Todos but a Relation field. It also means that a user can have from zero to any amount of todos.

  
```
type Mutation {
  createUser(input: CreateUserInput): User! @resolver(name: "create_user")

  loginUser(input: LoginUserInput): String! @resolver(name: "login_user")
}

```
There are only two mutations listed above but after the schema is imported in Fauna extra mutations needed for CRUD (create, read, update and delete) operations would be added.

createUser: This mutation allows the creation of a new user in the database. Since it is a custom mutation we would need to add our custom resolver to it using the @resolver directive. The name, “create_user” is the name of the function that we would have to create to handle the mutation.

loginUser: This mutation allows the creation of authorization tokens which would grant users permission to have access to personal data( todos). The @resolver directive was also used here to handle the login mutation. We are yet to define the function properly.

Note that when the schema is imported, other mutations would be created, Indexes would be created, Functions would be created too waiting to be defined properly.

One thing you should note is that any type created that isn’t using an @embedded directive creates a collection in the database. The directives used in Fauna are not standard GraphQL directives but unique to just Fauna

Next click on the GraphQL Menu.

![](https://lh3.googleusercontent.com/5hKXGd9A4y_uULcoxwvXolLmlcZcz239et26etTU3bsAEh9higgkUuCiEgs3SkJibC4YQcXQ_ULYMT_RYJoqbHYAKDt7fNV2JgXpIXO6C9Zm_kVf80sxPTStYliZoNzNp8TKF4DY)

Next click on the “Import Schema” button

![](https://lh4.googleusercontent.com/BuZILAgxlpyeIoGHpVDhX_GEl3QslBtltnR-8saPepH5KcqLIBpD5zucEWdzgjdobfC8tNHyaPDW9Hon9T5lyXkl-eyabsgOGIg6cX_f-dAdOG4pC91KuukaYmnDj5IlyxNd0wnB)

Your file explorer dialog box would open up waiting for you to select the “schema.gql” file.

Select the “schema.gql” file and wait for Fauna to open up the GraphQL playground.

![](https://lh6.googleusercontent.com/XkZQGnuKZ69YUwk2Q9j8BGAcfZm5lzmNUPG1fq9wWSdiIZ0t0JiU5P46JgLwasW_82h1n0x6A_wSMcELqJfKUHwwQN5Z1jTwBfwIS5O0Ta4FbO11saAUVbsT8kZWOT1toQww3Pb8)

Finally click on the Schema button by the right side of the playground and see the computed schema.

![](https://lh3.googleusercontent.com/jwDztkNKqg4U7Y-mb049aVfUuqiUCZyfX0PPAk-piOwoilEur2nDj2b2YvEL5Ms_vV1QtTDeOLLMeMs9_SvGMzN8Aj0efiTFYbLxYUwaR67S1F3_n49o8OjbO7Pq800jGQ8oHMgt)

You can see some new mutations created to enable CRUD operations and new fields created in the Query type.

The resolvers for the CRUD mutations are handled by Fauna. You don’t need to create a custom resolver except the default one isn’t tailored to what you need.

Currently, the functions created for the custom mutations( createUser and loginUser ) aren't working as needed. So in the next step, we would update the created functions to work properly

## Step 3: Update Custom Functions

So why I say update and not create is because Fauna by default creates custom resolver functions for you if defined in the Schema but doesn’t make it functional.

Okay, so let us confirm if the resolver functions were created.

Click on the functions menu.

![](https://lh4.googleusercontent.com/D3Z1R_EyBzvP29zXjOUJQvfxI5A0ZonBzLhydRKspi4y6sIQC45j0Y_jGQkoB_oIcpZTLBM69Dmz-CLmMuhEtoNPk-xochSoznDDLbQ2_6qiTAQv26RU2I2D6AWEpufeT86AZdiz)

And you should see the unconfigured functions available

![](https://lh5.googleusercontent.com/XeOltcEjrRazGMm3qY57S3EvTvv4r1H7lbblyPTjcemK7uw7L3QRWnDJRKxCuRFHGAZ15An7aZolkoRF9E05xz_QDZsIcf24uFbileF86ypk5O87fcMMKxyvewqUlqfrUSzIjiUW)

To update the function to work properly we would be writing in Fauna’s custom language called FQL.

FQL might seem esoteric to write in but with a little bit of practice, you would see that it is quite easy.

FQL is an adoption of the functional programming paradigm and provides a large library of default functions for developers to use. Some examples of the functions are:

-   Reduce: If you are familiar with the Reduce function in JavaScript you would also be glad to see that Fauna has an inbuilt function that does the same thing. The reduce function in FQL iterates through a set of values in an array and returns a single value. Imagine being able to compose power mathematical computations in an easy way using the FQL functions.
    
-   Map: The map function iterates through an array and also accepts a lambda function that defines how each value of the array is manipulated. The map function returns a new array.
    
-   Lambda: The lambda function in FQL is an anonymous function that performs lazy execution of custom code
    

There are tons of functions in Fauna that give you the freedom to perform lots of manipulation and tasks easily. You can check out this [link](https://docs.fauna.com/fauna/current/api/fql/functions/) to know more about Fauna’s FQL syntax.

Next, click on the “SHELL” menu and see an interactive shell that allows admins to write commands in FQL.

  

![](https://lh4.googleusercontent.com/RbjoWknBH-iespds_E_JyECbdXa8dFCEN4x1ciygQmNjGYtW8Diy5YNXK72IBZcJRrqFU4iIhlVJ5AapMpOFwmPLRTwucqEGJxxZmSedGjEiyaW_KFkm1RiIJCLTjpnWylC3bUHC)

In the button area of the shell area you would see some default functions that populate line 1 through 3.

The Functions are:

-   Paginate(Collections())
    
-   Paginate(Indexes())
    
-   Paginate(Databases())
    

Custom functions in Fauna are built using a combination of default functions. To know more about Functions in Fauna click [here](https://docs.fauna.com/fauna/current/api/fql/functions).

The Paginate function returns a list of results. The Collections function returns a set reference of the collections in a database. You need to call it with the Paginate function to see the list of collections in a database.

The Indexes function when called in the Paginate function returns a list of created indexes in a database.

The Databases function when called in the Paginate function returns a list of databases in a database. Fauna allows nested databases so you can actually create a database in a database.

OK, So click on the “RUN QUERY” button to see the output of the functions to be executed.

![](https://lh4.googleusercontent.com/PyZaXhejQchjGmuqN-XUxbjgtf4iXDoe0AXfYxVG08sh-kyj2Odm0BiPlElLDruT8wIDqiwvlza3ImVLRLCxK1GC5JxVW7FwFxfHBIE9mHt6BzIpadvuq-kG2fX8pZ17fTXwwD8V)

  

We would be updating the “create_user” and “login_function” created by mutation to suit our needs.

Custom functions in Fauna are called User-defined functions(UDF) and when creating a UDF for a GraphQL resolver, the UDF must accept an array or arguments, the same order and number defined in your GraphQL schema.

The two UDF’s can be seen below:
```
//create_user function

Update(Function("create_user"), {
  body: Query(
    Lambda(
      ["input"],

      Create(Collection("User"), {
        data: {
          username: Select("username", Var("input")),
        },

        credentials: {
          password: Select("password", Var("input")),
        },
      })
    )
  ),
});

//login_user function

Update(Function("login_user"), {
  body: Query(
    Lambda(
      ["input"],

      Select(
        "secret",

        Login(
          Match(Index("unique_username"), Select("username", Var("input"))),

          { password: Select("password", Var("input")) }
        )
      )
    )
  ),
});


  ```

We would be dealing a lot with documents when using Fauna. So below is what a document looks like:
```
{
  "ref": Ref(Collection("User"), "281178277009162753"),
  "ts": 1604411351335000,
  "data": {
    "username": "Obi"
  }
}
```
Every document has a ref/reference property, which is a combination of the location where a document is found along with its unique id.

A little explanation of some of the built-in functions seen in the UDF above:

-   Update function: The Update function accepts two params as an argument: a ref and a param_object. The param object contains the fields that should be updated in an identified document.
    
-   Function function: The Function function accepts the name of a function as an argument and returns the ref to the function.
    
-   Lambda function: The Lambda function accepts two arguments: params and an expression to be evaluated. It returns the evaluation of the expression.
    
-   Query function: The Query function wraps lambda functions. They prevent the wrapped function from being immediately executed while making them available in UDF’s, index-bindings and Attribute-based access control predicates.
    
-   Select function: The Select function extracts a single value from a document. A document has similarities with javascript objects.
    
-   Create function: The create function adds a new document to a collection. It accepts two arguments which are: the collection and the param_object. The param_object also includes a credential property which is used to set the permission of a document.
    
-   Login function: The login function accepts two arguments: the ref of the document and the param_object which contains the password used in the protected document. This function returns the access tokens for a user.
    
-   Match function: The match function finds the search term used to match documents in the provided index. It takes in the index and search term as an argument and returns the ref if found documents.
    
-   Index function: The index function returns a valid reference to documents registered to the index.
    

To know more about Fauna Functions and examples click [here](https://docs.fauna.com/fauna/current/api/fql/functions/create).

Next, we would run the functions above in the shell. clear the default functions in the shell and paste the UDF’s above. Then click on the “RUN QUERY” button.

![](https://lh4.googleusercontent.com/BqQeecuYoEcC8EehdUWhe_ZzZSK1_LyrwUgFgmV6sgAUHExoETJuVettmdtLxmF-l_LALhZLUQzgiPOO1CTK2gYMuTau-qZht7y9rtOMQsYCz399KSz9qYezlHux2GmxMp2jvrE1)

At this point, we are ready to start running GraphQL queries in the playground.

## Step 4: Creating users and todos

Click on the “GRAPHQL” button to access the GraphQL playground.

Lets start out by creating two users. Paste the code below in the first tab
```
mutation CREATEUSER1($input: CreateUserInput!) {
  createUser(input: $input) {
    _id
    username
  }
}


Let the variables be:

{
    "input": {
        "username": "User1",
        "password": "User1"
    }
}
```
And click on the run button to process the mutation.

![](https://lh6.googleusercontent.com/7pp8l9bBot2fzptYp8ahtSRd_dEE_Gh-Xgmm7IZ-eBuroGLydqb0RwPLL2AaT1bkXcbHgTs3XECXa49gnMBnJ-n2lxnRiP8kLt3Sys3K9Q6I41X75nUVIAUbaebLfbucs2YEg2iG)

You should also note that adding the password field to the mutation would cause an error. This is because the “create_user” function makes use of the password field as a credential. This implies that the data of the password field can’t be queried.

The _id field was created automatically for us by Fauna and that would be used in creating a user to todo relation
```
mutation CREATEUSER2($input: CreateUserInput!) {
  createUser(input: $input) {
    _id
    username
  }
}

```
Use this as the variable
```
{
    "input": {
        "username": "User2",
        "password": "User2"
    }
}
```
Click on the run button to process the mutation

![](https://lh6.googleusercontent.com/MQhAVq58IWdNXcdypaarovz_YUarYdp6P7OhXzVNO8yA1ML2bgpHZpEVEB5PCsVF-2MGk29VQDA8LPKeQ0j6ggirA6_p4MeHEAuRCvlLR80INs8MdOv88hRJQyP-IeMkCtgZyZgv)

Next, let us create todos for “User1”. Open a new tab and copy the code below
```
mutation CREATETODO1($data: TodoInput!) {
  createTodo(data: $data) {
    text
    completed
    user {
      username
    }
  }
}
```
The variable should be
```
{
    "data": {
        "text": "User1 todo",
        "completed": false,
        "user": {
            "connect": "281305036730925569"
        }
    }
}
```
Note that the id used in the connect property should defer in your own case. The id I used for the connect property is the id generated during the creation of “User1”. So use the id generated for you when creating “User1”

Click on the run button to process the mutation

![](https://lh6.googleusercontent.com/oA9GE5IQ5Wy_rTLrqx5qVqxn7Ani9amibLLVaFQf3wNy2VGGRd_EATpqDonRs58Sk2I4t70wyZnhHF8OdC4N_czCJv6-AoalA38v_XIOBfmBhdqsylopZ_pCUY3yQB1tDkiO237N)

Open a new tab and copy the code below to generate a todo for “User2”
```
mutation CREATETODO2($data: TodoInput!) {
  createTodo(data: $data) {
    text
    completed
    user {
      username
    }
  }
}
```
Use the code below as the variable
```
{
    "data": {
        "text": "User1 todo",
        "completed": false,
        "user": {
            "connect": "281305112648876551"
        }
    }
}
```
Also here I used the id generated for “User2” to link this todo to “User2”

![](https://lh3.googleusercontent.com/7WGLsa9t5zjvAO7bvvwLGvtgwgjaG3HHSEfXLeld53xvqWmGaE9QYbWAvKTrISWeT_9kbJpOwqHe8zBZnOgwS99SJWN_sWO7XZar3ytZcNuqeRln_qVD2qO_8zeWvpM0Dr3EecXB)

Next let us run a query to list all todos. Open a new tab and paste the code below
```
query ListTodos {
  todos {
    data {
      text
      completed
      user {
        username
      }
    }
  }
}
```
Run the query and you should have the result below

![](https://lh4.googleusercontent.com/f1AcNKMrn8rBOakRFTlgXSa9CjXVwrPoZNVSL8WCXH2XnO5EUzHIofxmevrC10RZvnhpW98i9BcFeZw5LYbXV28qIjaYfv8JxsFxL3nbF9-R9Igc-VOSyjXr0kr-Yu99G_gfVQwv)

So at this point you might be asking yourself why we are able to query the data of users.

The reason behind this is due to the fact that by default the GraphQL playground makes use of server keys to make requests.

You can see that in the HTTP headers tab section.

![](https://lh4.googleusercontent.com/BRHtBS2NA6r2qzZU6lCox7LgCv7nQKN0dnWTX0uEjI4sDgGIof5tL11znixaF9ldA1ul_PffuP5xr8Q1d5uTF5XtrLy0eOT4f4imMAQv-FsDKAjykd5_NwrdBNoAafz59TLgsjMk)

In the next step, we would deal with using user tokens during request and adding access constraint to requests. To know more about the authorization header click [here](https://docs.fauna.com/fauna/current/api/graphql/endpoints).

## Step 5: Using ABAC to define access constraints

ABAC stands for Attribute-based Access Control. Attribute-Based Access Control is an extension of the role-based access control(RBAC) where the attributes or fields of a user document can be used to determine access privilege to data in the Fauna. To know more about ABAC click [here](https://docs.fauna.com/fauna/current/security/abac).

A user would need to login to Fauna to obtain access tokens. We established a login mutation to take care of that. to know more about the Login function click [here](https://docs.fauna.com/fauna/current/api/fql/functions/login?lang=javascript).

So next, we would define access roles to registered users in the database by using the “CreateRole” inbuilt function. The code below defines the privileges given to users.
```
CreateRole({
  name: "todo_access",

  membership: [{ resource: Collection("User") }],

  privileges: [
    {
      resource: Collection("User"),

      actions: { read: true },
    },

    {
      resource: Index("todos"),

      actions: { read: true },
    },

    {
      resource: Collection("Todo"),

      actions: {
        create: true,

        delete: Query(
          Lambda(
            "todo_ref",

            Equals(Select(["data", "user"], Get(Var("todo_ref"))), Identity())
          )
        ),

        write: Query(
          Lambda(
            "todo_ref",

            Equals(Select(["data", "user"], Get(Var("todo_ref"))), Identity())
          )
        ),

        read: Query(
          Lambda(
            "todo_ref",

            Equals(Select(["data", "user"], Get(Var("todo_ref"))), Identity())
          )
        ),
      },
    },
  ],
});
```
  

The function above creates a Role named “todo_access”. The membership property defines which authenticated resource are members of a given role. Since user data is stored in the User collection, the only membership location used would be Collection(“User”).

Privileges define the actions permitted by an authenticated user to perform. the privileges property takes in an array of objects which define various permission levels.

So I set the read action to be true for the Collection(“User”) and Index(“todos”) resource.

```
{
    resource: Collection("User"),
    actions: { 
        read: true
    },
},
{
     resource: Index("todos"),
     actions: { 
         read: true
    },
},
```

This would grant GraphQL queries direct access to these two resources. As you can see no condition is defined for access to the resources above.

When a user is trying to read, write or delete data from the Collection(“Todo”) resource, a function is called to verify if the user has access to that particular document.

```
{

    resource: Collection("Todo"),
    
    actions: {
    
    create: true,
    
    delete: Query(
    
        Lambda(
    
            "todo_ref",
    
            Equals(Select(["data", "user"], Get(Var("todo_ref"))), Identity())
    
         )
    
    ),
    
    write: Query(
    
            Lambda(
    
             ["old", "new", "todo_ref"],
    
             Equals(Select(["data", "user"], Get(Var("todo_ref"))), Identity())
    
        )
    
    ),
    
    read: Query(
    
            Lambda(
    
                "todo_ref",
    
                 Equals(Select(["data", "user"], Get(Var("todo_ref"))), Identity())
    
             )
    
    ),
    
    },
    
    },
```

So how the function works is that it compares the id of the user related to a particular document or todo to the id returned from the Identity() function call.

The Identity function call returns a reference to a user document using the authorization tokens sent when making a request to Fauna using the GraphQL API.

To know more about various privileges and membership click [here](https://docs.fauna.com/fauna/current/security/roles).

Next, let us copy the CreateRole function defined earlier and run it in the shell. Delete the former query in the shell and paste the new function in the shell. Also, clear the previous query output to have a neater interface

![](https://lh6.googleusercontent.com/CpRsA6YZPAbpGVEZprFPxiilHUPm2N7d93wp2uPM3Gdh3EYVtKS3eWcVVr9wT1QnTcIlexghPahE4_klFAuzzug9KJR8nmOn8DzX4T_RwXuKfmeBseIVnp93-OZYFczOvlc4zNTC)

## Step 6: Testing the set privileges

To see if the privileges are working properly we would need to run the login mutation for “User1”

So open the GraphQL playground and create a new tab. Paste this mutation code inside it.

```
mutation LOGINUSER1($input: LoginUserInput!) {

   loginUser(input: $input)

}

Use this variable below to get the login token for User1

{

   "input": {

      "username": "User1",

      "password": "User1"

   }

}
```

Run the mutation and you should see the login token displayed for the User1

![](https://lh6.googleusercontent.com/dcb-8O5_AkFFxhvOHBQIgwuxgQPznqp8SjzXIwjaYa2CZc0p-_uewijPyojGirRKtu-O73DSO6-gMuI1FR31y6yVzQfAe_b4iXNR92rU2Y0fBmpbDsbEfZtSsRuufpo2I4PHWBGd)

Copy the login user token. It would be used as the Bearer token in the Authorization header.

Click on the HTTP HEADERS tab and Change the “Basic” keyword to “Bearer” also change the default token beside the “Basic” keyword to the token obtained from the login mutation.

-   ![](https://lh3.googleusercontent.com/8rqNiXwuuUxyney0_0ijTKvkNp7p5JpSaNrc5bkfCDkrjeNoQyQc7klEE7yohP9TioB_64YJlAjYaRuD7KOSaoBIrg_ky5qzVXvMsT66M6QIYt-yS7hYR_QT2B38t3iTejs3MfbS)
    
-   ![](https://lh6.googleusercontent.com/dFYLh-tnxD084SyhnHCu_VLm6g2veYT6gpvs5NSfLLCMrslWQ1HOpgKNpHRedId1QhSfjyIdIb32fiMc0ODE_-A8u1lgkYMStuNyRQRADtHkz22p_qwbHsDYLJcuuuX-IQf9aSmh)
    

Now you are set to run queries using the identity of User1

Paste the code below in the same tab that you set the authorization header and run the code below. You would see that only to-dos for User1 would be listed.

```
query ListTodos {
  todos {
    data {
      text
      completed
      user {
        username
      }
    }
  }
}

```

![](https://lh5.googleusercontent.com/BT1VHTh126O5nlpsVkmHrTToUA8ehPDmLKqjuFYQzWLbmERx4qpvL5o2zKF6x0aHMkTmlYNSqOwUtloWRWBi1p7-CTQkmayxjWKbMF_67pjlDpu1kvMNsDnDb4Xiq16LIOowtT_5)

So now we have the attribute-based access control setup.

But recall that the initial plan of this tutorial is to handle the login and createuser mutation from Azure functions.

Once your client( maybe an app) obtains the token from Azure functions then subsequent requests would be made directly to the database using Fauna’s GraphQL API.

## Step 7: Obtaining server tokens

To run the createuser and login mutation from Azure functions we would need to make use of a server token to grant us access

Click on the Security tab and click on the “New Key” button”

![](https://lh5.googleusercontent.com/MeqNWawiQj9Hlw31ic4uavNinVvBB6grx_8I4Vm34pOXrT0Pmk_y3YPcbvqm6k1u-wUdHdOdMLZuTzYRWEtqqTIeLKRtUQV8Yl0A79onro7Z2FLpvjxFptgWMIhBytJnwofB4agJ)

In the key name field you can type in any name of your choice, then click on the save button.

![](https://lh6.googleusercontent.com/oUWAi7-YEVPrsBAxfelaxX6rNk-6-t1dlT-FUm8pKAukoHDQ4NOvBM7fXFuvshWQJ1WRKGmw9TD0eVYpabUTKT-Lp_s4GEoAIgboMeyShVvcAu-LkpuE6lIEMSYUfrrASeVNuRqn)

You would see your created tokens. Save that token somewhere as it would be used in Azure functions.

![](https://lh5.googleusercontent.com/kHNMo-u9i2bUP_up5UIPBqlmMrvzxIP1ZyowL6sjGUHZuudY1WIwZinnwGo8pjSgnzLRldomvwVIS7YGrWIfzWGfB6rsfNAswtE-CSPJm2EE_h5VDciHwRYAqqC16_5EFg81vJ3W)

## Step 8: Setting up Azure functions

For the sake of grouping resources, use this [link](https://portal.azure.com/#blade/HubsExtension/BrowseResourceGroups) to navigate to the resource group page on the Azure portal. Click on the add button.

![](https://lh4.googleusercontent.com/Keiyk_b__WrVZNXVqo24gEQ7szkI2ddoT1NeC68s5cPB4Bo5aLqJYBPtj-z1hXnmNFXX6hD41uJN9-b9DXghYN0fmG_uKmGM0ib1Z2bx5hif83z6e68W1VkKSgYxwAE1JgTcyxWc)

Type in the name of the resource group in the page you would be redirected to. I chose “faunaresource” as the resource group name. Click on the review + create button. And finally, click on the create button.

![](https://lh5.googleusercontent.com/vzvmQ2azLt_UPdZxd3t2SORFzEyOc-_SJ5eaFv-rtx8G-OtTwC5y0-oAajW0q5UO9FG8-OBTRazR--brK-0wkAvrRopp-qEjF6aQuy8E-wR_eBp1cifHzLsDcThNEQKXkrByWiTh)

Wait a little bit for your resource group to be created. Once it is created you would be redirected to the resource group page again and you should see the “faunaresource” group name.

![](https://lh3.googleusercontent.com/ccicQM9xusQkSZkEWr-cbMTpIZu2_XmXQmSRzIu3cYgKI0gh2MisNDqUtdPyR48_eupwpyWGS9lhrrid-AaM4D85AFH59IfMb5GGtHatmwOBzC-8WxOTNepvsb7nZv3pTxOWuRuf)

Click on the “faunaresource” group name and click on the Add button on the right pane.

![](https://lh3.googleusercontent.com/f7oLx0NrSsqL3077vkMYg4CI8s_NMyhuiBjzftKZJuWN6dUweOWMvX8itNSYf2_Yv6Be7YXPeKBSzvGFwqZkXig2gABBHWgICxS9WO0QPEYz3xmK00F6kF7G5aHBl7fAfojl-kee)

You would be redirected to a new page that displays a list of resources. Click on Function App to create an Azure Function resource or you can search it in the search bar.

![](https://lh5.googleusercontent.com/UG4BYr63MwiaS_GHrP2cJPJm3NGTu6IeAvrCpRFtU5VkAih6tO-7e7m4RVzj39ep3CGDFHMiolvzZy3tx3f0ISLFIJRzspiSkLg0Kquw9Uwnih_0f5i436RbDJcCPn72uMrfBK6Q)

You would be redirected to a new page to fill in the function details. I chose “faunagrapql” as the Azure function app’s name and NODE.js as the runtime. After that, click on the review+create button to create the function. Finally, click on the create button.

You would be redirected to a new page that reads “Deployment in progress”

![](https://lh6.googleusercontent.com/JnbG3ddUe6mUsARmL3QrAOs4mDFr-XeZqFmfG3r0M1iFMMNVBhfX_L5NcvgIqS_ZBRw8UiQrgznUQzeg_egLZ3ODTWJcjYpJ4BGUBWU9OOVWEGDxb4g0T4xDZnpuAkHaBnzCk2Xo)

Wait a little bit until the “Go to Resource” button displays

![](https://lh5.googleusercontent.com/MYeNl5neBxotXdHiIzZueNTSpoJdHaBJpkXL3KJHNdkxQq7hmIteXVX0D2uLNiRAt9k6snhMQedWI10Fd82wwabejeeNPXFfkqSgIGC4ACORXOPVBnXW5K9eA4usIxUT7HlcBc6V)

You would be redirected to the created function’s dashboard. Click on the functions menu on the left pane.

![](https://lh4.googleusercontent.com/oym4ChbFia11Ys0YiX51FmLvpXOimyThPML4tGxDp-j8i1i7oxWLiS-znO1lSOlPTOZNbApvGubeuN_ZyT9UwWezYMpbXmmQb8CQMfirIAjzFwG7VEPelp8Y3hHSdBgVWhBMumv9)

Clicking on the functions menu would display an interface that allows you to create functions for an Azure function app. Click on the Add button displayed on the top of the dashboard.

A right pane would appear. The pane contains various standard templates for creating an Azure function. We would be using the HTTP template so click on the HTTP trigger card.

![](https://lh5.googleusercontent.com/RwARc2NXXh3p3iqDgGvGuY3Xa6RniBhpYzgKEG34vBDXFOcv2xxBsU4YjQIwuJwkSR9pdBVlbkAoYav-_w_BczKDMYBZjyup6uaiamVMCnOKojkaLWcWiWNLkoBxJMeHIds009Dc)

After clicking on the HTTP trigger card, use “createuser” as the name of the function. But you can decide which name you want to use. Click on the Create functions button and wait for your function to be created

![](https://lh4.googleusercontent.com/fHuCVSeTBUiQfyHqXR-JK8aE0KXtdCQPavLRcokA9Ev2GCBZ8VlHsxfB8CP8AiL0dsaMGNpnrXEW95oGZ76sGzJ88vdJ_2T--s-pLgQ7wdkHVdQexHEEYS1JWQRcDgzXY1EClAQ_)

Go back to the previous overview dashboard and repeat the function creation process for our second mutation. I named the function “loginuser”.

Once you are done creating the login function go back to the Function app overview dashboard. On the left pane, scroll down and click on the “App Service Editor” menu. After that click on the Go button. And you would be redirected to a new interface.

![](https://lh6.googleusercontent.com/unWE73vMreG2SJElzM4nZeD0BJv8fFDiCpmCkrnDvPHOQVULdy0Ffh9g1xPpbnXyQoCJ38PRSlMcX9hW2kInOk7SZGItjYk6HF10A2sRRtpf_MWxYkT5kMHchthr7zKHwcYTzOmh)

Once you’re in the editor click on the “open console” button on the left pane. The console would enable us to install some node modules that would be used for our project.

![](https://lh3.googleusercontent.com/a7K5ayGix6uMkd18pCI0m2D2YmJtqDpXvvhjsEFpXqGtG8cAycmHUc54nV7TgHc1VQD45sxpBf4D-CAMzkMFsIG8821WhQgGQzXAF05CRSbMRgnql96BS8u1Ma1RHexbHW7BJmSX)

Install these dependencies: cross-fetch and @apollo/client

Apollo client requires that a fetch API is available but since this environment is a node and not a windows environment, we would be making use of the cross-fetch node module. Which provides a fetch API for a node environment.

Run the code below in the console

npm install cross-fetch @apollo/client graphql

![](https://lh6.googleusercontent.com/1YL4E6BtwRNEZ5YglAm03gn-0D8swugFxn0b5Vmk1oURHs2AZMTIGFE1WzU0FuAEk0yTx6k4LtSQvOGzBA5FopZzmsGULBxF70PnqVoZi_Edk70fB_GQrVtbYYVNf8O6o2Bd_J9r)

After installing the node modules open the “createuser” folder and click on the index.js file. Delete the default code available in the editor and paste the code below into the editor.
```
let {
  ApolloClient,

  gql,

  HttpLink,

  InMemoryCache,
} = require("@apollo/client/core");

let fetch = require("cross-fetch");

const client = new ApolloClient({
  link: new HttpLink({
    uri: "https://graphql.fauna.com/graphql",

    headers: {
      authorization: "Bearer fnAD5490IkACAXthzw97lnjON82Kwjyq-2_qsIZ8",
    },

    fetch,
  }),

  cache: new InMemoryCache(),
});

module.exports = async function (context, req) {
  context.log("JavaScript HTTP trigger function processed a request.");

  let signUpMutation = gql`
    mutation CREATEUSER($input: CreateUserInput!) {
      createUser(input: $input) {
        username
      }
    }
  `;

  let graphRequest;

  try {
    graphRequest = await client.mutate({
      mutation: signUpMutation,

      variables: {
        input: {
          username: req.body.username,

          password: req.body.password,
        },
      },
    });
  } catch (errror) {
    graphRequest = error;
  }

  context.res = {
    // status: 200, /* Defaults to 200 */

    body: graphRequest,
  };
};
```
Now there are some things that you would notice about the code above.

I imported @apollo/client/core instead of @apollo/client. This is because @apollo/client/core entry allows you to use the Apollo client without requesting for react to be installed.

The HttpLink constructor allows the configuration of a custom fetch API and setting of headers.

I made use of the server token created from Fauna’s dashboard to set the authorization header. So use your own server code there.

The function above is designed to receive the username and password from the request body which it uses to make a GraphQL request to Fauna (registering a user) and sends the response to the client.

Next, let us set up the function that would retrieve the login token from Fauna and send it to the client.

Open the “loginuser” folder. Click on the index.js file and paste the code below
```
let {
  ApolloClient,

  gql,

  HttpLink,

  InMemoryCache,
} = require("@apollo/client/core");

let fetch = require("cross-fetch");

const client = new ApolloClient({
  link: new HttpLink({
    uri: "https://graphql.fauna.com/graphql",

    headers: {
      authorization: "Bearer fnAD5490IkACAXthzw97lnjON82Kwjyq-2_qsIZ8",
    },

    fetch,
  }),

  cache: new InMemoryCache(),
});

module.exports = async function (context, req) {
  context.log("JavaScript HTTP trigger function processed a request.");

  let loginMutation = gql`
    mutation LOGINUSER($input: LoginUserInput) {
      loginUser(input: $input)
    }
  `;

  let graphRequest;

  try {
    graphRequest = await client.mutate({
      mutation: loginMutation,

      variables: {
        input: {
          username: req.body.username,

          password: req.body.password,
        },
      },
    });
  } catch (errror) {
    graphRequest = error;
  }

  context.res = {
    // status: 200, /* Defaults to 200 */

    body: graphRequest,
  };
};
```
  

The code above is created to receive the username and password from the request body. It uses these credentials to make a GraphQL request to Fauna (login a user). The retrieved user token is sent to the client to enable it to make direct authorized requests to Fauna.

Also, note that the code saves automatically as you write.

Now we need to test the function and see if it works.

## Step 9: Test the function

Let us test the login function. Go to the “faunagraphql” azure function, click on the functions menu and select the “loginuser” function.

In the new page select “Code + Test” menu and wait for the editor to load.

![](https://lh4.googleusercontent.com/2fGpBpAdRp_bNV59UVISZ71RDa0WdJVOa6e9YKB4AQVnSycwsVMFuxwaPYpNOObizRphdl1G0qUqUtmEZ-fTywjrcCE3vPcITpR39nlDIGSe7ZwbFPEqVZXI3uUWONqb_DY2yeXN)

You should see an editor with prepopulated code to handle the login mutation. Click on the “Test/Run” button.

![](https://lh5.googleusercontent.com/Y26P6fiqbffVbh8qKYr0kyaQR-k0THA-MCmsUzxZtIHgWwWpM5e4VWT5Gz6WO9ESkLmwSHJb6gd96XpEDegC2oZHMXsn0MnvsOszLQTFOsHdkaf2QHNgiSy8nhTwDsHmpvzfgKwF)

A right pane would appear. In the right pane scroll down to the body section and paste this code in the body section to see what the request would return.
```
{

   "username":"User1",

   "password":"User1"

}
```
![](https://lh3.googleusercontent.com/eGHE90AsI7asYzOyIDwJjiciDZbvobCDmU5zlf8p7yjl3-q72PpxYpAQBRdzM6Z3w2UBf8W-A3j7hAd0pNLShk0R2-GfjaQb_NzA-32zoNryIbf1XMr7A3_qdbTNLqqdmkOMbHKv)

Click on the run button and wait for a response.

![](https://lh4.googleusercontent.com/0QQ8ycUPxaqDHCjatzlgm1lpIR8JJKHseDqyc0DJDYfkfV5Xh1cpJ80pGNWXUnfRIMSjphKBkBq6nneebWLQF6tNFW3KQrgOhzPFgZ82mByP672S3YRIlCTNJuNa1-OcwDlRjo7M)

Copy the loginUser token and paste in fauna’s GraphQL playground as the authorization token.

Try and run this query
```
query ListTodos {
  todos {
    data {
      text
      completed
      user {
        username
      }
    }
  }
}
```
And you should see that it only displays the todo of “User1”.

To see the Function’s URL, go to the “loginuser” function dashboard and click on the copy URL option at the top of the dashboard.

You would see the URL to your Azure function which you can use in your client.

By the time you would be reading this post, the created tokens I used won’t be functional.

Thanks for reading through and learning a little bit about the awesomeness of Fauna.