<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sammychinedu2ky</title>
    <description>The latest articles on DEV Community by sammychinedu2ky (@sammychinedu2ky).</description>
    <link>https://dev.to/sammychinedu2ky</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F200099%2F4bbc0c8d-2075-49a7-96e8-48fdd4739777.jpeg</url>
      <title>DEV Community: sammychinedu2ky</title>
      <link>https://dev.to/sammychinedu2ky</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sammychinedu2ky"/>
    <language>en</language>
    <item>
      <title>Voucher Based Ethereum System</title>
      <dc:creator>sammychinedu2ky</dc:creator>
      <pubDate>Mon, 18 Jan 2021 10:58:34 +0000</pubDate>
      <link>https://dev.to/sammychinedu2ky/voucher-based-ethereum-system-a94</link>
      <guid>https://dev.to/sammychinedu2ky/voucher-based-ethereum-system-a94</guid>
      <description>&lt;p&gt;Yello guys, hope you all are doing great and out of a hollow because of a mellow kicking you to euphoria. No pun intended 🥴.&lt;/p&gt;

&lt;p&gt;We all get inspired to initiate an amount of creativity by some sort of muse. Well, this post was inspired by a list of groovy thoughts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an Ethereum Token (Swac Coin 😜).&lt;/li&gt;
&lt;li&gt;Using Ethereum tokens to replace Vouchers in schools.&lt;/li&gt;
&lt;li&gt;Thinking further of creating a custom blockchain payment system using NFC's.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So to kick off, I have a little story to tell. I once attended a boarding school for some years during part of my secondary education. &lt;/p&gt;

&lt;p&gt;The school I attended made use of a voucher payment system for within school purchases. It required students to pay in money( real cash) to the bursary office during resumption. &lt;br&gt;
The cash paid is converted to vouchers for the student to utilize for daily school expenses. The voucher was a printed paper with a text written on it to denote its value. &lt;/p&gt;

&lt;p&gt;Well, there are some advantages that this system has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The school's grocery vendors receive money in voucher currency. In this way, the school accounting department tries to prevent the school vendors from keeping away some cash during stock taking.&lt;/li&gt;
&lt;li&gt;It also prevents students from purchasing things that are off the radar of the school's stores inventory.&lt;/li&gt;
&lt;li&gt;It allows students to receive vouchers on a timely basis until their initial paid cash equivalent is exhausted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well in reality people still smuggle in real cash but see an outline of its disadvantages below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Vouchers can still be stolen like real cash&lt;/li&gt;
&lt;li&gt; Counterfeit Vouchers can be made by students&lt;/li&gt;
&lt;li&gt; They can easily be torn (printed vouchers)&lt;/li&gt;
&lt;li&gt; High burden of security consciousness&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So after delving a little bit into the Ethereum planet. I began to imagine a custom replacement to the voucher-based system in my school.&lt;/p&gt;
&lt;h2&gt;
  
  
  My Thought
&lt;/h2&gt;

&lt;p&gt;Instead of having a voucher-based system, a custom Ethereum token can be used. Students would be given an NFC card during their registration. &lt;/p&gt;

&lt;p&gt;The NFC card would store a token address which would be unique to each registered student. The NFC card would be deemed to be a  physical wallet, a proxy for payment ( juxtaposed to an ATM card). &lt;/p&gt;

&lt;p&gt;During the registration process, the students would need to create a one time password that would be used to permit transactions using their NFC card. &lt;/p&gt;

&lt;p&gt;Since this is a custom payment system, a custom Point of Sale (POS) machine would need to be created to permit students to make payment in school shops.&lt;/p&gt;

&lt;p&gt;If the Ethereum contract is deployed on the main-net,  parents of students can transfer money to the contract of the custom Ethereum token, with this students can easily call their parents for financial support.&lt;/p&gt;

&lt;p&gt;Parents sending money to the main-net isn't the only way to buy more tokens for his/child. It all depends on what the school wants. &lt;/p&gt;
&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;Student A gets registered as a student of School B. During the registration process, Student A's parent pays in money to the bursary department of school B, the school gives student A an NFC payment card which serves as a blockchain wallet for student A. &lt;/p&gt;

&lt;p&gt;The student is requested to create a one time password for the NFC payment card. The password would be used in the POS machine of the school's shop to permit transactions.&lt;/p&gt;

&lt;p&gt;The wallet can permit transactions up to the equivalent amount deposited by student A's parent. &lt;/p&gt;

&lt;p&gt;Student A goes to School B's shop to purchase snacks. The student is requested to place his/her card on the POS machine with a request to insert student A's password to permit the transaction. Let's say student A has 10 token coins before the transaction. If the snacks cost 2 token coins, then after the transaction, student A would have 8 token coins available for future purchase. &lt;/p&gt;

&lt;p&gt;In the future, after student A has consumed his/her token, student A can contact his/her parent to transfer more money to his/her account. This can be achieved by student A's parent transferring some ethers to the contract's address of the token on Ethereum's mainnet.&lt;/p&gt;

&lt;p&gt;An alternative method of abstraction could still be used to prevent parents from dealing with creating an Ethereum account. A payment gateway company can be used. &lt;/p&gt;
&lt;h2&gt;
  
  
  Benefits of using the custom Ethereum blockchain token
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Unlike using vouchers that can be torn into pieces, using the NFC card prevents that, and even if misplaced, the student can request for a new NFC card with the same address. &lt;/li&gt;
&lt;li&gt;Students can't use a students NFC card if stolen without knowing its access password.&lt;/li&gt;
&lt;li&gt;Counterfeit tokens can't be made since it is secured by the blockchain architecture.&lt;/li&gt;
&lt;li&gt;School doesn't need to worry about printing vouchers, a single card can handle all transactions&lt;/li&gt;
&lt;li&gt;The school's shop seller doesn't need to count the cash because a custom POS system is provided.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adopting this system would need the school to set up some new infrastructure to support it but I believe it has more benefits to offer than its downside.&lt;/p&gt;
&lt;h2&gt;
  
  
  Swac Token
&lt;/h2&gt;

&lt;p&gt;To make clients and developers easily build tokens (eg USDT) on top of the Ethereum blockchain, a standard was created to help make this possible.&lt;br&gt;
It is called the ERC-20 standard. &lt;br&gt;
It details the interface that an Ethereum token should adhere to. You can see it &lt;a href="https://eips.ethereum.org/EIPS/eip-20"&gt;here&lt;/a&gt;.&lt;br&gt;
So using the standard I decided to create my custom token. Just to try and see 😂. The code below is the contract for my Swac token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT&lt;/span&gt;
&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ERC20&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;totalSupply&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;external&lt;/span&gt; &lt;span class="nx"&gt;view&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;external&lt;/span&gt; &lt;span class="nx"&gt;view&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;external&lt;/span&gt;
        &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bool&lt;/span&gt; &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;transferFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;external&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bool&lt;/span&gt; &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;approve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_spender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;external&lt;/span&gt;
        &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bool&lt;/span&gt; &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;allowance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_spender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;external&lt;/span&gt;
        &lt;span class="nx"&gt;view&lt;/span&gt;
        &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;remaining&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="nx"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;indexed&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;indexed&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="nx"&gt;Approval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;indexed&lt;/span&gt; &lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;indexed&lt;/span&gt; &lt;span class="nx"&gt;spender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="nx"&gt;Swac&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;ERC20&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SWAC COIN&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;symbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SWC&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;decimals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;override&lt;/span&gt; &lt;span class="nx"&gt;totalSupply&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;ethToSwac&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;payable&lt;/span&gt; &lt;span class="nx"&gt;contractOwner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nx"&gt;allowed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;initialAmount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;totalSupply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialAmount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialAmount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;contractOwner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;view&lt;/span&gt; &lt;span class="nx"&gt;override&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt;
        &lt;span class="nx"&gt;override&lt;/span&gt;
        &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;transferFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;override&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_from&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;allowed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_from&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_from&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;allowed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_from&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;emit&lt;/span&gt; &lt;span class="nx"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;approve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_spender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt;
        &lt;span class="nx"&gt;override&lt;/span&gt;
        &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;allowed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;_spender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;emit&lt;/span&gt; &lt;span class="nx"&gt;Approval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_spender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;allowance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="nx"&gt;_spender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt;
        &lt;span class="nx"&gt;view&lt;/span&gt;
        &lt;span class="nx"&gt;override&lt;/span&gt;
        &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;allowed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_owner&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;_spender&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;external&lt;/span&gt; &lt;span class="nx"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;ethToSwac&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;contractOwner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;contractOwner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;contractOwner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;emit&lt;/span&gt; &lt;span class="nx"&gt;Transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;contractOwner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;NB: My contract doesn't handle overflow situations of uint and a little adjustment can be made to the approve function to prevent &lt;a href="https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/edit"&gt;this&lt;/a&gt; attack&lt;/em&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GPzCvKq3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/36219292/104900357-45fda680-597c-11eb-9b27-fd3c84737493.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GPzCvKq3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/36219292/104900357-45fda680-597c-11eb-9b27-fd3c84737493.png" alt="swaccoin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I might consider building the NFC payment machine as a school project. I think it is cool.&lt;br&gt;
If you like my thought on this please do share this post and if you have a comment to make please do 😁.&lt;br&gt;
Thanks&lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>solidity</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Making a small Library in Solidity</title>
      <dc:creator>sammychinedu2ky</dc:creator>
      <pubDate>Mon, 11 Jan 2021 20:59:04 +0000</pubDate>
      <link>https://dev.to/sammychinedu2ky/making-a-small-library-in-solidity-5917</link>
      <guid>https://dev.to/sammychinedu2ky/making-a-small-library-in-solidity-5917</guid>
      <description>&lt;p&gt;Hello guys, hope you all are doing great!!! 😎. &lt;br&gt;
Blockchain is quite popular these days. I mean it is the frenzy on the net especially with the upsurge of Bitcoin's value. Following the trend, I would be writing the greatest article of all time. 😈. I'm joking.. lol.&lt;br&gt;
So in this post, I would be talking about how I created a small Library in Solidity.&lt;br&gt;
What prompted me to do this was due to the fact that I needed a way to recreate a function in Solidity that behaves like the JavaScript function below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getPositiveAndSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getPositiveAndSquare&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;span class="c1"&gt;// returns [4,9,16,4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function above takes in an array as an argument, filters the numbers in the array by leaving only the positive numbers and finally it transforms the positive numbers to its square 🥱. Ok, this seems quite easy to do in JavaScript but trying to replicate a function that has similar logic in Solidity wasn't that straight forward.&lt;/p&gt;

&lt;p&gt;So to work it out, I entered the avatar state to unlock my chakra 🦾. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C3kq7ErN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.pixabay.com/photo/2019/03/07/04/55/cyborg-4039511_960_720.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C3kq7ErN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.pixabay.com/photo/2019/03/07/04/55/cyborg-4039511_960_720.jpg" alt="AvatarState"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But it was too much to handle, making me spend a lot of time figuring it out. This made me ask my self the big question, &lt;strong&gt;Is my state authentic?&lt;/strong&gt;, &lt;strong&gt;can I bend?&lt;/strong&gt;, &lt;strong&gt;can I code?&lt;/strong&gt;, &lt;strong&gt;Is solidity for me?&lt;/strong&gt;. I felt like crying but then I heard a voice from above speak to me. &lt;br&gt;
The voice made me feel energized and I felt at one with my code. I call this &lt;strong&gt;entangled enlightenment&lt;/strong&gt; or what mortals call the &lt;strong&gt;flow state&lt;/strong&gt;. &lt;br&gt;
I began to see the hidden codes of the universe unfettered. I could see images and text in plain binary format. At this point, I was ready to solve this 😎&lt;/p&gt;
&lt;h2&gt;
  
  
  My Solution to the problem
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT&lt;/span&gt;
&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;solidity&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;library&lt;/span&gt; &lt;span class="nx"&gt;util&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;filterFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;int256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;pure&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;internal&lt;/span&gt; &lt;span class="nx"&gt;pure&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="nx"&gt;tempArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="nx"&gt;MyContract&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;using&lt;/span&gt; &lt;span class="nx"&gt;util&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;int256&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getPositiveAndSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt;
        &lt;span class="nx"&gt;pure&lt;/span&gt;
        &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filterFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int256&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;pure&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Code flow
&lt;/h2&gt;

&lt;p&gt;First I created a contract named, &lt;strong&gt;MyContract&lt;/strong&gt;.  Inside the contract, I created a function named, &lt;strong&gt;getPositiveAndSquare&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getPositiveAndSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kr"&gt;public&lt;/span&gt;
        &lt;span class="nx"&gt;pure&lt;/span&gt;
        &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filterFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;getPositiveAndSquare&lt;/strong&gt; function  accepts an array with values of unsigned integers with &lt;strong&gt;memory&lt;/strong&gt; as its storage location and &lt;strong&gt;arr&lt;/strong&gt; as its parameter name. It returns an array of unsigned integers.&lt;/p&gt;

&lt;p&gt;Within the  &lt;strong&gt;getPositiveAndSquare&lt;/strong&gt; function you can see the &lt;strong&gt;filterFunc&lt;/strong&gt; method being appended to the &lt;strong&gt;arr&lt;/strong&gt; parameter. This is made possible with the creation of a solidity library which has a function (&lt;strong&gt;filterFunc&lt;/strong&gt;) that conforms to the same type as  &lt;strong&gt;arr&lt;/strong&gt; (int256 ).  The &lt;strong&gt;filterFunc&lt;/strong&gt; function accepts a function named &lt;strong&gt;square&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Checking the definition of the &lt;strong&gt;filterFunc&lt;/strong&gt; function in the Library section you can see that it accepts two parameters.   &lt;/p&gt;

&lt;p&gt;So why am I passing only the &lt;strong&gt;square&lt;/strong&gt; function into it? This is because the &lt;strong&gt;arr&lt;/strong&gt; parameter conforms with the type definition of the first &lt;strong&gt;filterFunction&lt;/strong&gt; parameter. So the &lt;strong&gt;arr&lt;/strong&gt; paremter becomes the first argument of the &lt;strong&gt;filterFunc&lt;/strong&gt; function while the &lt;strong&gt;square&lt;/strong&gt; function becomes its second argument.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;square&lt;/strong&gt; function takes in a signed integer and returns an unsigned integer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int256&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;pure&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because the &lt;strong&gt;square&lt;/strong&gt; function acts on the signed integers of the &lt;strong&gt;self&lt;/strong&gt; parameter of the &lt;strong&gt;filterFunc&lt;/strong&gt; function. The types have to be same to prevent errors. It returns an unsigned integer because the returned value must be a positive integer.&lt;/p&gt;

&lt;p&gt;In the contract, &lt;strong&gt;MyContract&lt;/strong&gt;, you can see this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;using&lt;/span&gt; &lt;span class="nx"&gt;util&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;int256&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means attach Solidity's library functions to a type of int256[] in the Contract.&lt;br&gt;
That is what made attaching &lt;strong&gt;filterFunc&lt;/strong&gt; to &lt;strong&gt;arr&lt;/strong&gt; possible. It also made &lt;strong&gt;arr&lt;/strong&gt; to be the first argument of the &lt;strong&gt;filterFunc&lt;/strong&gt; function.&lt;/p&gt;

&lt;p&gt;In the library, I only have one function named, &lt;strong&gt;filterFunc&lt;/strong&gt;. It has two parameters &lt;strong&gt;self&lt;/strong&gt; and &lt;strong&gt;f&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT&lt;/span&gt;
&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;solidity&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;library&lt;/span&gt; &lt;span class="nx"&gt;util&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;filterFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;int256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;pure&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;internal&lt;/span&gt; &lt;span class="nx"&gt;pure&lt;/span&gt; &lt;span class="nx"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="nx"&gt;tempArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uint256&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;newArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tempArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason why this took longer than expected to implement is that memory arrays don't implement the &lt;strong&gt;pop&lt;/strong&gt; and &lt;strong&gt;push&lt;/strong&gt; functions as a member, the &lt;strong&gt;length&lt;/strong&gt; member is read-only and Solidity's library doesn't allow defining state variables in it. &lt;/p&gt;

&lt;p&gt;So to get around this barrier, I had to create a new memory array called &lt;strong&gt;tempArr&lt;/strong&gt;.  The &lt;strong&gt;tempArr&lt;/strong&gt; was created to be one index longer than the &lt;strong&gt;self&lt;/strong&gt; array. I used the last index of the &lt;strong&gt;tempArr&lt;/strong&gt; to store the number of positive values in the &lt;strong&gt;self&lt;/strong&gt; array and the other indexes to store the square of the positive values in &lt;strong&gt;self&lt;/strong&gt; array. &lt;/p&gt;

&lt;p&gt;When creating a dynamic memory array in solidity, all the indexes are initialized with 0 for the int type.&lt;br&gt;
So that means all or not all the indexes of the &lt;strong&gt;tempArr&lt;/strong&gt; array would be updated. Some would remain zero.  For instance, check the pseudo code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//self array&lt;/span&gt;
&lt;span class="nb"&gt;self&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;//during initialization of tempArr&lt;/span&gt;
&lt;span class="nx"&gt;temArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;//after lopping through self array&lt;/span&gt;
&lt;span class="nx"&gt;tempArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, I created a dynamic memory array called &lt;strong&gt;newArr&lt;/strong&gt;.  It was constructed using the value of the last index of &lt;strong&gt;tempArr&lt;/strong&gt; as its length while being populated with the values of tempArr up to the index of a value of the last index of &lt;strong&gt;tempArr&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So that is how I was able to implement the filter function in solidity&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: this post is based on solidity version 0.8.0&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And I hope you learnt something new about solidity 😁. You can help me share it too and comment. Thanks a lot 🙏&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>solidity</category>
      <category>ethereum</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Fauna + Azure Functions</title>
      <dc:creator>sammychinedu2ky</dc:creator>
      <pubDate>Wed, 25 Nov 2020 06:50:11 +0000</pubDate>
      <link>https://dev.to/sammychinedu2ky/azure-functions-fauna-466g</link>
      <guid>https://dev.to/sammychinedu2ky/azure-functions-fauna-466g</guid>
      <description>&lt;p&gt;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 &lt;a href="https://docs.fauna.com/fauna/current/introduction.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://docs.fauna.com/fauna/current/start/fql_for_sql_users"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Below is a summary of what we would be building using Fauna and Azure Function:
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;An authorization token would be used to grant users access to only their own todo list.&lt;/p&gt;

&lt;p&gt;Communication to Fauna would be made using the GraphQL API provided by Fauna.&lt;/p&gt;

&lt;p&gt;Azure Functions would serve as the middleman between Fauna and your client for login and signup requests.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The flow
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The client makes a request to Azure Function to retrieve an authorization token which would enable communication between client and Fauna.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The client uses the token to make subsequent requests to Fauna.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  This tutorial covers:
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  Step 1: Creating an Account and Database
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Create an account on fauna by clicking &lt;a href="https://dashboard.fauna.com/accounts/register"&gt;here&lt;/a&gt;. After registering you would be redirected to your dashboard&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nra1-s3R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/CLi32pAm12_nxecuI2aY4pS6E7qBVE11MATe914rOU2e8G9u5cG206fc0c-N3ATjUviFy98vBBBzpnBeULYAuSaYrnp2fZcdw0_YBIivLn5nWoCoNEqqxXWY9RrbC9CKrqUT0hFN" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nra1-s3R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/CLi32pAm12_nxecuI2aY4pS6E7qBVE11MATe914rOU2e8G9u5cG206fc0c-N3ATjUviFy98vBBBzpnBeULYAuSaYrnp2fZcdw0_YBIivLn5nWoCoNEqqxXWY9RrbC9CKrqUT0hFN" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Click on the “NEW DATABASE” button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Typing “TODO” as the name of the database to be created.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---pU12U7y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/yv5Dri2rlCDtfNz14ZSNESvdCO1vocIYXcYhii0BwTwXmr3fV8wV12fRtEHdhOL4dO637Y1uElgH9eYBHh9uXQlJsXOeWWfKrGMdVeO2vIBCbh8rFe5xH0VDkHsdvlYBqhCNqLgx" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---pU12U7y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/yv5Dri2rlCDtfNz14ZSNESvdCO1vocIYXcYhii0BwTwXmr3fV8wV12fRtEHdhOL4dO637Y1uElgH9eYBHh9uXQlJsXOeWWfKrGMdVeO2vIBCbh8rFe5xH0VDkHsdvlYBqhCNqLgx" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  After clicking on the save button you would now have access to the dashboard of your newly created database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZSoKbjrc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/9HeEcNPxe6EMDRs5Lv-R9EojzpQBNgfsVowP9LGvZvAwXjs1h2Z8btUPBF2tbD9uDkjnXARfk5BYA0zU4HTtCmEfNGmch309RbZTXBIC29ZhPGASoKW7ckaB_uVUug_L16NOo96W" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZSoKbjrc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/9HeEcNPxe6EMDRs5Lv-R9EojzpQBNgfsVowP9LGvZvAwXjs1h2Z8btUPBF2tbD9uDkjnXARfk5BYA0zU4HTtCmEfNGmch309RbZTXBIC29ZhPGASoKW7ckaB_uVUug_L16NOo96W" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Importing GraphQL Schema
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;We would continue by creating a schema which I would break in chunks and explain&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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")
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the schema above and paste it locally in your computer using the name “schema.gql”.&lt;/p&gt;

&lt;p&gt;Below is the explanation of the schema fields above&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type User {
  username: String! @unique(index: "unique_username")

  password: String!

  todos: [Todo] @relation
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the username field, the unique directive is used to enforce this constraint.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://docs.fauna.com/fauna/current/api/fql/types?lang=javascript"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Mutation {
  createUser(input: CreateUserInput): User! @resolver(name: "create_user")

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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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&lt;/p&gt;

&lt;p&gt;Next click on the GraphQL Menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9mYFr9mo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/5hKXGd9A4y_uULcoxwvXolLmlcZcz239et26etTU3bsAEh9higgkUuCiEgs3SkJibC4YQcXQ_ULYMT_RYJoqbHYAKDt7fNV2JgXpIXO6C9Zm_kVf80sxPTStYliZoNzNp8TKF4DY" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9mYFr9mo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/5hKXGd9A4y_uULcoxwvXolLmlcZcz239et26etTU3bsAEh9higgkUuCiEgs3SkJibC4YQcXQ_ULYMT_RYJoqbHYAKDt7fNV2JgXpIXO6C9Zm_kVf80sxPTStYliZoNzNp8TKF4DY" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next click on the “Import Schema” button&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ltf_wKxv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/BuZILAgxlpyeIoGHpVDhX_GEl3QslBtltnR-8saPepH5KcqLIBpD5zucEWdzgjdobfC8tNHyaPDW9Hon9T5lyXkl-eyabsgOGIg6cX_f-dAdOG4pC91KuukaYmnDj5IlyxNd0wnB" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ltf_wKxv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/BuZILAgxlpyeIoGHpVDhX_GEl3QslBtltnR-8saPepH5KcqLIBpD5zucEWdzgjdobfC8tNHyaPDW9Hon9T5lyXkl-eyabsgOGIg6cX_f-dAdOG4pC91KuukaYmnDj5IlyxNd0wnB" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your file explorer dialog box would open up waiting for you to select the “schema.gql” file.&lt;/p&gt;

&lt;p&gt;Select the “schema.gql” file and wait for Fauna to open up the GraphQL playground.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xF1bO8VT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/XkZQGnuKZ69YUwk2Q9j8BGAcfZm5lzmNUPG1fq9wWSdiIZ0t0JiU5P46JgLwasW_82h1n0x6A_wSMcELqJfKUHwwQN5Z1jTwBfwIS5O0Ta4FbO11saAUVbsT8kZWOT1toQww3Pb8" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xF1bO8VT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/XkZQGnuKZ69YUwk2Q9j8BGAcfZm5lzmNUPG1fq9wWSdiIZ0t0JiU5P46JgLwasW_82h1n0x6A_wSMcELqJfKUHwwQN5Z1jTwBfwIS5O0Ta4FbO11saAUVbsT8kZWOT1toQww3Pb8" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally click on the Schema button by the right side of the playground and see the computed schema.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RnKQu26X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/jwDztkNKqg4U7Y-mb049aVfUuqiUCZyfX0PPAk-piOwoilEur2nDj2b2YvEL5Ms_vV1QtTDeOLLMeMs9_SvGMzN8Aj0efiTFYbLxYUwaR67S1F3_n49o8OjbO7Pq800jGQ8oHMgt" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RnKQu26X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/jwDztkNKqg4U7Y-mb049aVfUuqiUCZyfX0PPAk-piOwoilEur2nDj2b2YvEL5Ms_vV1QtTDeOLLMeMs9_SvGMzN8Aj0efiTFYbLxYUwaR67S1F3_n49o8OjbO7Pq800jGQ8oHMgt" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see some new mutations created to enable CRUD operations and new fields created in the Query type.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Update Custom Functions
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Okay, so let us confirm if the resolver functions were created.&lt;/p&gt;

&lt;p&gt;Click on the functions menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mjA9mtOR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/D3Z1R_EyBzvP29zXjOUJQvfxI5A0ZonBzLhydRKspi4y6sIQC45j0Y_jGQkoB_oIcpZTLBM69Dmz-CLmMuhEtoNPk-xochSoznDDLbQ2_6qiTAQv26RU2I2D6AWEpufeT86AZdiz" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mjA9mtOR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/D3Z1R_EyBzvP29zXjOUJQvfxI5A0ZonBzLhydRKspi4y6sIQC45j0Y_jGQkoB_oIcpZTLBM69Dmz-CLmMuhEtoNPk-xochSoznDDLbQ2_6qiTAQv26RU2I2D6AWEpufeT86AZdiz" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you should see the unconfigured functions available&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XSt4EGpg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/XeOltcEjrRazGMm3qY57S3EvTvv4r1H7lbblyPTjcemK7uw7L3QRWnDJRKxCuRFHGAZ15An7aZolkoRF9E05xz_QDZsIcf24uFbileF86ypk5O87fcMMKxyvewqUlqfrUSzIjiUW" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XSt4EGpg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/XeOltcEjrRazGMm3qY57S3EvTvv4r1H7lbblyPTjcemK7uw7L3QRWnDJRKxCuRFHGAZ15An7aZolkoRF9E05xz_QDZsIcf24uFbileF86ypk5O87fcMMKxyvewqUlqfrUSzIjiUW" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To update the function to work properly we would be writing in Fauna’s custom language called FQL.&lt;/p&gt;

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

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lambda: The lambda function in FQL is an anonymous function that performs lazy execution of custom code&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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 &lt;a href="https://docs.fauna.com/fauna/current/api/fql/functions/"&gt;link&lt;/a&gt; to know more about Fauna’s FQL syntax.&lt;/p&gt;

&lt;p&gt;Next, click on the “SHELL” menu and see an interactive shell that allows admins to write commands in FQL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iSRaCfWV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/RbjoWknBH-iespds_E_JyECbdXa8dFCEN4x1ciygQmNjGYtW8Diy5YNXK72IBZcJRrqFU4iIhlVJ5AapMpOFwmPLRTwucqEGJxxZmSedGjEiyaW_KFkm1RiIJCLTjpnWylC3bUHC" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iSRaCfWV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/RbjoWknBH-iespds_E_JyECbdXa8dFCEN4x1ciygQmNjGYtW8Diy5YNXK72IBZcJRrqFU4iIhlVJ5AapMpOFwmPLRTwucqEGJxxZmSedGjEiyaW_KFkm1RiIJCLTjpnWylC3bUHC" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the button area of the shell area you would see some default functions that populate line 1 through 3.&lt;/p&gt;

&lt;p&gt;The Functions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Paginate(Collections())&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Paginate(Indexes())&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Paginate(Databases())&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Custom functions in Fauna are built using a combination of default functions. To know more about Functions in Fauna click &lt;a href="https://docs.fauna.com/fauna/current/api/fql/functions"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The Indexes function when called in the Paginate function returns a list of created indexes in a database.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;OK, So click on the “RUN QUERY” button to see the output of the functions to be executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KW12Mta---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/PyZaXhejQchjGmuqN-XUxbjgtf4iXDoe0AXfYxVG08sh-kyj2Odm0BiPlElLDruT8wIDqiwvlza3ImVLRLCxK1GC5JxVW7FwFxfHBIE9mHt6BzIpadvuq-kG2fX8pZ17fTXwwD8V" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KW12Mta---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/PyZaXhejQchjGmuqN-XUxbjgtf4iXDoe0AXfYxVG08sh-kyj2Odm0BiPlElLDruT8wIDqiwvlza3ImVLRLCxK1GC5JxVW7FwFxfHBIE9mHt6BzIpadvuq-kG2fX8pZ17fTXwwD8V" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We would be updating the “create_user” and “login_function” created by mutation to suit our needs.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The two UDF’s can be seen below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//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")) }
        )
      )
    )
  ),
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We would be dealing a lot with documents when using Fauna. So below is what a document looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "ref": Ref(Collection("User"), "281178277009162753"),
  "ts": 1604411351335000,
  "data": {
    "username": "Obi"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every document has a ref/reference property, which is a combination of the location where a document is found along with its unique id.&lt;/p&gt;

&lt;p&gt;A little explanation of some of the built-in functions seen in the UDF above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function function: The Function function accepts the name of a function as an argument and returns the ref to the function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lambda function: The Lambda function accepts two arguments: params and an expression to be evaluated. It returns the evaluation of the expression.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select function: The Select function extracts a single value from a document. A document has similarities with javascript objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Index function: The index function returns a valid reference to documents registered to the index.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To know more about Fauna Functions and examples click &lt;a href="https://docs.fauna.com/fauna/current/api/fql/functions/create"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f4BoUVkt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/BqQeecuYoEcC8EehdUWhe_ZzZSK1_LyrwUgFgmV6sgAUHExoETJuVettmdtLxmF-l_LALhZLUQzgiPOO1CTK2gYMuTau-qZht7y9rtOMQsYCz399KSz9qYezlHux2GmxMp2jvrE1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f4BoUVkt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/BqQeecuYoEcC8EehdUWhe_ZzZSK1_LyrwUgFgmV6sgAUHExoETJuVettmdtLxmF-l_LALhZLUQzgiPOO1CTK2gYMuTau-qZht7y9rtOMQsYCz399KSz9qYezlHux2GmxMp2jvrE1" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, we are ready to start running GraphQL queries in the playground.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Creating users and todos
&lt;/h2&gt;

&lt;p&gt;Click on the “GRAPHQL” button to access the GraphQL playground.&lt;/p&gt;

&lt;p&gt;Lets start out by creating two users. Paste the code below in the first tab&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mutation CREATEUSER1($input: CreateUserInput!) {
  createUser(input: $input) {
    _id
    username
  }
}


Let the variables be:

{
    "input": {
        "username": "User1",
        "password": "User1"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And click on the run button to process the mutation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TMEekCad--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/7pp8l9bBot2fzptYp8ahtSRd_dEE_Gh-Xgmm7IZ-eBuroGLydqb0RwPLL2AaT1bkXcbHgTs3XECXa49gnMBnJ-n2lxnRiP8kLt3Sys3K9Q6I41X75nUVIAUbaebLfbucs2YEg2iG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TMEekCad--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/7pp8l9bBot2fzptYp8ahtSRd_dEE_Gh-Xgmm7IZ-eBuroGLydqb0RwPLL2AaT1bkXcbHgTs3XECXa49gnMBnJ-n2lxnRiP8kLt3Sys3K9Q6I41X75nUVIAUbaebLfbucs2YEg2iG" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The _id field was created automatically for us by Fauna and that would be used in creating a user to todo relation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mutation CREATEUSER2($input: CreateUserInput!) {
  createUser(input: $input) {
    _id
    username
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this as the variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "input": {
        "username": "User2",
        "password": "User2"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Click on the run button to process the mutation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J8Kr2rd5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/MQhAVq58IWdNXcdypaarovz_YUarYdp6P7OhXzVNO8yA1ML2bgpHZpEVEB5PCsVF-2MGk29VQDA8LPKeQ0j6ggirA6_p4MeHEAuRCvlLR80INs8MdOv88hRJQyP-IeMkCtgZyZgv" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J8Kr2rd5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/MQhAVq58IWdNXcdypaarovz_YUarYdp6P7OhXzVNO8yA1ML2bgpHZpEVEB5PCsVF-2MGk29VQDA8LPKeQ0j6ggirA6_p4MeHEAuRCvlLR80INs8MdOv88hRJQyP-IeMkCtgZyZgv" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, let us create todos for “User1”. Open a new tab and copy the code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mutation CREATETODO1($data: TodoInput!) {
  createTodo(data: $data) {
    text
    completed
    user {
      username
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable should be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "data": {
        "text": "User1 todo",
        "completed": false,
        "user": {
            "connect": "281305036730925569"
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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”&lt;/p&gt;

&lt;p&gt;Click on the run button to process the mutation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dxVMzq1i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/oA9GE5IQ5Wy_rTLrqx5qVqxn7Ani9amibLLVaFQf3wNy2VGGRd_EATpqDonRs58Sk2I4t70wyZnhHF8OdC4N_czCJv6-AoalA38v_XIOBfmBhdqsylopZ_pCUY3yQB1tDkiO237N" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dxVMzq1i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/oA9GE5IQ5Wy_rTLrqx5qVqxn7Ani9amibLLVaFQf3wNy2VGGRd_EATpqDonRs58Sk2I4t70wyZnhHF8OdC4N_czCJv6-AoalA38v_XIOBfmBhdqsylopZ_pCUY3yQB1tDkiO237N" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open a new tab and copy the code below to generate a todo for “User2”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mutation CREATETODO2($data: TodoInput!) {
  createTodo(data: $data) {
    text
    completed
    user {
      username
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the code below as the variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "data": {
        "text": "User1 todo",
        "completed": false,
        "user": {
            "connect": "281305112648876551"
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also here I used the id generated for “User2” to link this todo to “User2”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i-jFvxza--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/7WGLsa9t5zjvAO7bvvwLGvtgwgjaG3HHSEfXLeld53xvqWmGaE9QYbWAvKTrISWeT_9kbJpOwqHe8zBZnOgwS99SJWN_sWO7XZar3ytZcNuqeRln_qVD2qO_8zeWvpM0Dr3EecXB" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i-jFvxza--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/7WGLsa9t5zjvAO7bvvwLGvtgwgjaG3HHSEfXLeld53xvqWmGaE9QYbWAvKTrISWeT_9kbJpOwqHe8zBZnOgwS99SJWN_sWO7XZar3ytZcNuqeRln_qVD2qO_8zeWvpM0Dr3EecXB" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next let us run a query to list all todos. Open a new tab and paste the code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query ListTodos {
  todos {
    data {
      text
      completed
      user {
        username
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the query and you should have the result below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ySsKfo_y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/f1AcNKMrn8rBOakRFTlgXSa9CjXVwrPoZNVSL8WCXH2XnO5EUzHIofxmevrC10RZvnhpW98i9BcFeZw5LYbXV28qIjaYfv8JxsFxL3nbF9-R9Igc-VOSyjXr0kr-Yu99G_gfVQwv" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ySsKfo_y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/f1AcNKMrn8rBOakRFTlgXSa9CjXVwrPoZNVSL8WCXH2XnO5EUzHIofxmevrC10RZvnhpW98i9BcFeZw5LYbXV28qIjaYfv8JxsFxL3nbF9-R9Igc-VOSyjXr0kr-Yu99G_gfVQwv" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So at this point you might be asking yourself why we are able to query the data of users.&lt;/p&gt;

&lt;p&gt;The reason behind this is due to the fact that by default the GraphQL playground makes use of server keys to make requests.&lt;/p&gt;

&lt;p&gt;You can see that in the HTTP headers tab section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qED8XCd2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/BRHtBS2NA6r2qzZU6lCox7LgCv7nQKN0dnWTX0uEjI4sDgGIof5tL11znixaF9ldA1ul_PffuP5xr8Q1d5uTF5XtrLy0eOT4f4imMAQv-FsDKAjykd5_NwrdBNoAafz59TLgsjMk" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qED8XCd2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/BRHtBS2NA6r2qzZU6lCox7LgCv7nQKN0dnWTX0uEjI4sDgGIof5tL11znixaF9ldA1ul_PffuP5xr8Q1d5uTF5XtrLy0eOT4f4imMAQv-FsDKAjykd5_NwrdBNoAafz59TLgsjMk" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://docs.fauna.com/fauna/current/api/graphql/endpoints"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Using ABAC to define access constraints
&lt;/h2&gt;

&lt;p&gt;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 &lt;a href="https://docs.fauna.com/fauna/current/security/abac"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://docs.fauna.com/fauna/current/api/fql/functions/login?lang=javascript"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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())
          )
        ),
      },
    },
  ],
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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”).&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So I set the read action to be true for the Collection(“User”) and Index(“todos”) resource.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    resource: Collection("User"),
    actions: { 
        read: true
    },
},
{
     resource: Index("todos"),
     actions: { 
         read: true
    },
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{

    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())

             )

    ),

    },

    },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;To know more about various privileges and membership click &lt;a href="https://docs.fauna.com/fauna/current/security/roles"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QfVWN8Gy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/CpRsA6YZPAbpGVEZprFPxiilHUPm2N7d93wp2uPM3Gdh3EYVtKS3eWcVVr9wT1QnTcIlexghPahE4_klFAuzzug9KJR8nmOn8DzX4T_RwXuKfmeBseIVnp93-OZYFczOvlc4zNTC" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QfVWN8Gy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/CpRsA6YZPAbpGVEZprFPxiilHUPm2N7d93wp2uPM3Gdh3EYVtKS3eWcVVr9wT1QnTcIlexghPahE4_klFAuzzug9KJR8nmOn8DzX4T_RwXuKfmeBseIVnp93-OZYFczOvlc4zNTC" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Testing the set privileges
&lt;/h2&gt;

&lt;p&gt;To see if the privileges are working properly we would need to run the login mutation for “User1”&lt;/p&gt;

&lt;p&gt;So open the GraphQL playground and create a new tab. Paste this mutation code inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mutation LOGINUSER1($input: LoginUserInput!) {

   loginUser(input: $input)

}

Use this variable below to get the login token for User1

{

   "input": {

      "username": "User1",

      "password": "User1"

   }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the mutation and you should see the login token displayed for the User1&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6B3QmXxl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/dcb-8O5_AkFFxhvOHBQIgwuxgQPznqp8SjzXIwjaYa2CZc0p-_uewijPyojGirRKtu-O73DSO6-gMuI1FR31y6yVzQfAe_b4iXNR92rU2Y0fBmpbDsbEfZtSsRuufpo2I4PHWBGd" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6B3QmXxl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/dcb-8O5_AkFFxhvOHBQIgwuxgQPznqp8SjzXIwjaYa2CZc0p-_uewijPyojGirRKtu-O73DSO6-gMuI1FR31y6yVzQfAe_b4iXNR92rU2Y0fBmpbDsbEfZtSsRuufpo2I4PHWBGd" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the login user token. It would be used as the Bearer token in the Authorization header.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Kkq8xFnH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/8rqNiXwuuUxyney0_0ijTKvkNp7p5JpSaNrc5bkfCDkrjeNoQyQc7klEE7yohP9TioB_64YJlAjYaRuD7KOSaoBIrg_ky5qzVXvMsT66M6QIYt-yS7hYR_QT2B38t3iTejs3MfbS" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Kkq8xFnH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/8rqNiXwuuUxyney0_0ijTKvkNp7p5JpSaNrc5bkfCDkrjeNoQyQc7klEE7yohP9TioB_64YJlAjYaRuD7KOSaoBIrg_ky5qzVXvMsT66M6QIYt-yS7hYR_QT2B38t3iTejs3MfbS" alt=""&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MFV-k_P3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/dFYLh-tnxD084SyhnHCu_VLm6g2veYT6gpvs5NSfLLCMrslWQ1HOpgKNpHRedId1QhSfjyIdIb32fiMc0ODE_-A8u1lgkYMStuNyRQRADtHkz22p_qwbHsDYLJcuuuX-IQf9aSmh" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MFV-k_P3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/dFYLh-tnxD084SyhnHCu_VLm6g2veYT6gpvs5NSfLLCMrslWQ1HOpgKNpHRedId1QhSfjyIdIb32fiMc0ODE_-A8u1lgkYMStuNyRQRADtHkz22p_qwbHsDYLJcuuuX-IQf9aSmh" alt=""&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you are set to run queries using the identity of User1&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query ListTodos {
  todos {
    data {
      text
      completed
      user {
        username
      }
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qnVdHeQA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/BT1VHTh126O5nlpsVkmHrTToUA8ehPDmLKqjuFYQzWLbmERx4qpvL5o2zKF6x0aHMkTmlYNSqOwUtloWRWBi1p7-CTQkmayxjWKbMF_67pjlDpu1kvMNsDnDb4Xiq16LIOowtT_5" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qnVdHeQA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/BT1VHTh126O5nlpsVkmHrTToUA8ehPDmLKqjuFYQzWLbmERx4qpvL5o2zKF6x0aHMkTmlYNSqOwUtloWRWBi1p7-CTQkmayxjWKbMF_67pjlDpu1kvMNsDnDb4Xiq16LIOowtT_5" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now we have the attribute-based access control setup.&lt;/p&gt;

&lt;p&gt;But recall that the initial plan of this tutorial is to handle the login and createuser mutation from Azure functions.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Obtaining server tokens
&lt;/h2&gt;

&lt;p&gt;To run the createuser and login mutation from Azure functions we would need to make use of a server token to grant us access&lt;/p&gt;

&lt;p&gt;Click on the Security tab and click on the “New Key” button”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ub66dHys--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/MeqNWawiQj9Hlw31ic4uavNinVvBB6grx_8I4Vm34pOXrT0Pmk_y3YPcbvqm6k1u-wUdHdOdMLZuTzYRWEtqqTIeLKRtUQV8Yl0A79onro7Z2FLpvjxFptgWMIhBytJnwofB4agJ" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ub66dHys--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/MeqNWawiQj9Hlw31ic4uavNinVvBB6grx_8I4Vm34pOXrT0Pmk_y3YPcbvqm6k1u-wUdHdOdMLZuTzYRWEtqqTIeLKRtUQV8Yl0A79onro7Z2FLpvjxFptgWMIhBytJnwofB4agJ" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the key name field you can type in any name of your choice, then click on the save button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--typOpCRJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/oUWAi7-YEVPrsBAxfelaxX6rNk-6-t1dlT-FUm8pKAukoHDQ4NOvBM7fXFuvshWQJ1WRKGmw9TD0eVYpabUTKT-Lp_s4GEoAIgboMeyShVvcAu-LkpuE6lIEMSYUfrrASeVNuRqn" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--typOpCRJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/oUWAi7-YEVPrsBAxfelaxX6rNk-6-t1dlT-FUm8pKAukoHDQ4NOvBM7fXFuvshWQJ1WRKGmw9TD0eVYpabUTKT-Lp_s4GEoAIgboMeyShVvcAu-LkpuE6lIEMSYUfrrASeVNuRqn" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You would see your created tokens. Save that token somewhere as it would be used in Azure functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HC3j7_uA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/kHNMo-u9i2bUP_up5UIPBqlmMrvzxIP1ZyowL6sjGUHZuudY1WIwZinnwGo8pjSgnzLRldomvwVIS7YGrWIfzWGfB6rsfNAswtE-CSPJm2EE_h5VDciHwRYAqqC16_5EFg81vJ3W" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HC3j7_uA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/kHNMo-u9i2bUP_up5UIPBqlmMrvzxIP1ZyowL6sjGUHZuudY1WIwZinnwGo8pjSgnzLRldomvwVIS7YGrWIfzWGfB6rsfNAswtE-CSPJm2EE_h5VDciHwRYAqqC16_5EFg81vJ3W" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Setting up Azure functions
&lt;/h2&gt;

&lt;p&gt;For the sake of grouping resources, use this &lt;a href="https://portal.azure.com/#blade/HubsExtension/BrowseResourceGroups"&gt;link&lt;/a&gt; to navigate to the resource group page on the Azure portal. Click on the add button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aYjhi461--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/Keiyk_b__WrVZNXVqo24gEQ7szkI2ddoT1NeC68s5cPB4Bo5aLqJYBPtj-z1hXnmNFXX6hD41uJN9-b9DXghYN0fmG_uKmGM0ib1Z2bx5hif83z6e68W1VkKSgYxwAE1JgTcyxWc" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aYjhi461--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/Keiyk_b__WrVZNXVqo24gEQ7szkI2ddoT1NeC68s5cPB4Bo5aLqJYBPtj-z1hXnmNFXX6hD41uJN9-b9DXghYN0fmG_uKmGM0ib1Z2bx5hif83z6e68W1VkKSgYxwAE1JgTcyxWc" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xAGORXzB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/vzvmQ2azLt_UPdZxd3t2SORFzEyOc-_SJ5eaFv-rtx8G-OtTwC5y0-oAajW0q5UO9FG8-OBTRazR--brK-0wkAvrRopp-qEjF6aQuy8E-wR_eBp1cifHzLsDcThNEQKXkrByWiTh" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xAGORXzB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/vzvmQ2azLt_UPdZxd3t2SORFzEyOc-_SJ5eaFv-rtx8G-OtTwC5y0-oAajW0q5UO9FG8-OBTRazR--brK-0wkAvrRopp-qEjF6aQuy8E-wR_eBp1cifHzLsDcThNEQKXkrByWiTh" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AMdQ8kuo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/ccicQM9xusQkSZkEWr-cbMTpIZu2_XmXQmSRzIu3cYgKI0gh2MisNDqUtdPyR48_eupwpyWGS9lhrrid-AaM4D85AFH59IfMb5GGtHatmwOBzC-8WxOTNepvsb7nZv3pTxOWuRuf" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AMdQ8kuo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/ccicQM9xusQkSZkEWr-cbMTpIZu2_XmXQmSRzIu3cYgKI0gh2MisNDqUtdPyR48_eupwpyWGS9lhrrid-AaM4D85AFH59IfMb5GGtHatmwOBzC-8WxOTNepvsb7nZv3pTxOWuRuf" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the “faunaresource” group name and click on the Add button on the right pane.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FkneFfeg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/f7oLx0NrSsqL3077vkMYg4CI8s_NMyhuiBjzftKZJuWN6dUweOWMvX8itNSYf2_Yv6Be7YXPeKBSzvGFwqZkXig2gABBHWgICxS9WO0QPEYz3xmK00F6kF7G5aHBl7fAfojl-kee" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FkneFfeg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/f7oLx0NrSsqL3077vkMYg4CI8s_NMyhuiBjzftKZJuWN6dUweOWMvX8itNSYf2_Yv6Be7YXPeKBSzvGFwqZkXig2gABBHWgICxS9WO0QPEYz3xmK00F6kF7G5aHBl7fAfojl-kee" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A7UkVXOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/UG4BYr63MwiaS_GHrP2cJPJm3NGTu6IeAvrCpRFtU5VkAih6tO-7e7m4RVzj39ep3CGDFHMiolvzZy3tx3f0ISLFIJRzspiSkLg0Kquw9Uwnih_0f5i436RbDJcCPn72uMrfBK6Q" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A7UkVXOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/UG4BYr63MwiaS_GHrP2cJPJm3NGTu6IeAvrCpRFtU5VkAih6tO-7e7m4RVzj39ep3CGDFHMiolvzZy3tx3f0ISLFIJRzspiSkLg0Kquw9Uwnih_0f5i436RbDJcCPn72uMrfBK6Q" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You would be redirected to a new page that reads “Deployment in progress”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ovSVUVm7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/JnbG3ddUe6mUsARmL3QrAOs4mDFr-XeZqFmfG3r0M1iFMMNVBhfX_L5NcvgIqS_ZBRw8UiQrgznUQzeg_egLZ3ODTWJcjYpJ4BGUBWU9OOVWEGDxb4g0T4xDZnpuAkHaBnzCk2Xo" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ovSVUVm7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/JnbG3ddUe6mUsARmL3QrAOs4mDFr-XeZqFmfG3r0M1iFMMNVBhfX_L5NcvgIqS_ZBRw8UiQrgznUQzeg_egLZ3ODTWJcjYpJ4BGUBWU9OOVWEGDxb4g0T4xDZnpuAkHaBnzCk2Xo" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait a little bit until the “Go to Resource” button displays&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UQvVtWlm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/MYeNl5neBxotXdHiIzZueNTSpoJdHaBJpkXL3KJHNdkxQq7hmIteXVX0D2uLNiRAt9k6snhMQedWI10Fd82wwabejeeNPXFfkqSgIGC4ACORXOPVBnXW5K9eA4usIxUT7HlcBc6V" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UQvVtWlm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/MYeNl5neBxotXdHiIzZueNTSpoJdHaBJpkXL3KJHNdkxQq7hmIteXVX0D2uLNiRAt9k6snhMQedWI10Fd82wwabejeeNPXFfkqSgIGC4ACORXOPVBnXW5K9eA4usIxUT7HlcBc6V" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You would be redirected to the created function’s dashboard. Click on the functions menu on the left pane.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OzJvoT6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/oym4ChbFia11Ys0YiX51FmLvpXOimyThPML4tGxDp-j8i1i7oxWLiS-znO1lSOlPTOZNbApvGubeuN_ZyT9UwWezYMpbXmmQb8CQMfirIAjzFwG7VEPelp8Y3hHSdBgVWhBMumv9" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OzJvoT6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/oym4ChbFia11Ys0YiX51FmLvpXOimyThPML4tGxDp-j8i1i7oxWLiS-znO1lSOlPTOZNbApvGubeuN_ZyT9UwWezYMpbXmmQb8CQMfirIAjzFwG7VEPelp8Y3hHSdBgVWhBMumv9" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--baHuoLaO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/RwARc2NXXh3p3iqDgGvGuY3Xa6RniBhpYzgKEG34vBDXFOcv2xxBsU4YjQIwuJwkSR9pdBVlbkAoYav-_w_BczKDMYBZjyup6uaiamVMCnOKojkaLWcWiWNLkoBxJMeHIds009Dc" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--baHuoLaO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/RwARc2NXXh3p3iqDgGvGuY3Xa6RniBhpYzgKEG34vBDXFOcv2xxBsU4YjQIwuJwkSR9pdBVlbkAoYav-_w_BczKDMYBZjyup6uaiamVMCnOKojkaLWcWiWNLkoBxJMeHIds009Dc" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TGSyv9GJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/fHuCVSeTBUiQfyHqXR-JK8aE0KXtdCQPavLRcokA9Ev2GCBZ8VlHsxfB8CP8AiL0dsaMGNpnrXEW95oGZ76sGzJ88vdJ_2T--s-pLgQ7wdkHVdQexHEEYS1JWQRcDgzXY1EClAQ_" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TGSyv9GJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/fHuCVSeTBUiQfyHqXR-JK8aE0KXtdCQPavLRcokA9Ev2GCBZ8VlHsxfB8CP8AiL0dsaMGNpnrXEW95oGZ76sGzJ88vdJ_2T--s-pLgQ7wdkHVdQexHEEYS1JWQRcDgzXY1EClAQ_" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go back to the previous overview dashboard and repeat the function creation process for our second mutation. I named the function “loginuser”.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0NAAssVM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/unWE73vMreG2SJElzM4nZeD0BJv8fFDiCpmCkrnDvPHOQVULdy0Ffh9g1xPpbnXyQoCJ38PRSlMcX9hW2kInOk7SZGItjYk6HF10A2sRRtpf_MWxYkT5kMHchthr7zKHwcYTzOmh" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0NAAssVM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/unWE73vMreG2SJElzM4nZeD0BJv8fFDiCpmCkrnDvPHOQVULdy0Ffh9g1xPpbnXyQoCJ38PRSlMcX9hW2kInOk7SZGItjYk6HF10A2sRRtpf_MWxYkT5kMHchthr7zKHwcYTzOmh" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qq0ldp0s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/a7K5ayGix6uMkd18pCI0m2D2YmJtqDpXvvhjsEFpXqGtG8cAycmHUc54nV7TgHc1VQD45sxpBf4D-CAMzkMFsIG8821WhQgGQzXAF05CRSbMRgnql96BS8u1Ma1RHexbHW7BJmSX" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qq0ldp0s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/a7K5ayGix6uMkd18pCI0m2D2YmJtqDpXvvhjsEFpXqGtG8cAycmHUc54nV7TgHc1VQD45sxpBf4D-CAMzkMFsIG8821WhQgGQzXAF05CRSbMRgnql96BS8u1Ma1RHexbHW7BJmSX" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install these dependencies: cross-fetch and @apollo/client&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Run the code below in the console&lt;/p&gt;

&lt;p&gt;npm install cross-fetch @apollo/client graphql&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2KkT9TGa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/1YL4E6BtwRNEZ5YglAm03gn-0D8swugFxn0b5Vmk1oURHs2AZMTIGFE1WzU0FuAEk0yTx6k4LtSQvOGzBA5FopZzmsGULBxF70PnqVoZi_Edk70fB_GQrVtbYYVNf8O6o2Bd_J9r" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2KkT9TGa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/1YL4E6BtwRNEZ5YglAm03gn-0D8swugFxn0b5Vmk1oURHs2AZMTIGFE1WzU0FuAEk0yTx6k4LtSQvOGzBA5FopZzmsGULBxF70PnqVoZi_Edk70fB_GQrVtbYYVNf8O6o2Bd_J9r" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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,
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now there are some things that you would notice about the code above.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The HttpLink constructor allows the configuration of a custom fetch API and setting of headers.&lt;/p&gt;

&lt;p&gt;I made use of the server token created from Fauna’s dashboard to set the authorization header. So use your own server code there.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Next, let us set up the function that would retrieve the login token from Fauna and send it to the client.&lt;/p&gt;

&lt;p&gt;Open the “loginuser” folder. Click on the index.js file and paste the code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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,
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Also, note that the code saves automatically as you write.&lt;/p&gt;

&lt;p&gt;Now we need to test the function and see if it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: Test the function
&lt;/h2&gt;

&lt;p&gt;Let us test the login function. Go to the “faunagraphql” azure function, click on the functions menu and select the “loginuser” function.&lt;/p&gt;

&lt;p&gt;In the new page select “Code + Test” menu and wait for the editor to load.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n8_NqAGf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/2fGpBpAdRp_bNV59UVISZ71RDa0WdJVOa6e9YKB4AQVnSycwsVMFuxwaPYpNOObizRphdl1G0qUqUtmEZ-fTywjrcCE3vPcITpR39nlDIGSe7ZwbFPEqVZXI3uUWONqb_DY2yeXN" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n8_NqAGf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/2fGpBpAdRp_bNV59UVISZ71RDa0WdJVOa6e9YKB4AQVnSycwsVMFuxwaPYpNOObizRphdl1G0qUqUtmEZ-fTywjrcCE3vPcITpR39nlDIGSe7ZwbFPEqVZXI3uUWONqb_DY2yeXN" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7OdX_Lfk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/Y26P6fiqbffVbh8qKYr0kyaQR-k0THA-MCmsUzxZtIHgWwWpM5e4VWT5Gz6WO9ESkLmwSHJb6gd96XpEDegC2oZHMXsn0MnvsOszLQTFOsHdkaf2QHNgiSy8nhTwDsHmpvzfgKwF" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7OdX_Lfk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh5.googleusercontent.com/Y26P6fiqbffVbh8qKYr0kyaQR-k0THA-MCmsUzxZtIHgWwWpM5e4VWT5Gz6WO9ESkLmwSHJb6gd96XpEDegC2oZHMXsn0MnvsOszLQTFOsHdkaf2QHNgiSy8nhTwDsHmpvzfgKwF" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{

   "username":"User1",

   "password":"User1"

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wzuWmcgo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/eGHE90AsI7asYzOyIDwJjiciDZbvobCDmU5zlf8p7yjl3-q72PpxYpAQBRdzM6Z3w2UBf8W-A3j7hAd0pNLShk0R2-GfjaQb_NzA-32zoNryIbf1XMr7A3_qdbTNLqqdmkOMbHKv" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wzuWmcgo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/eGHE90AsI7asYzOyIDwJjiciDZbvobCDmU5zlf8p7yjl3-q72PpxYpAQBRdzM6Z3w2UBf8W-A3j7hAd0pNLShk0R2-GfjaQb_NzA-32zoNryIbf1XMr7A3_qdbTNLqqdmkOMbHKv" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the run button and wait for a response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S8675e_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/0QQ8ycUPxaqDHCjatzlgm1lpIR8JJKHseDqyc0DJDYfkfV5Xh1cpJ80pGNWXUnfRIMSjphKBkBq6nneebWLQF6tNFW3KQrgOhzPFgZ82mByP672S3YRIlCTNJuNa1-OcwDlRjo7M" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S8675e_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/0QQ8ycUPxaqDHCjatzlgm1lpIR8JJKHseDqyc0DJDYfkfV5Xh1cpJ80pGNWXUnfRIMSjphKBkBq6nneebWLQF6tNFW3KQrgOhzPFgZ82mByP672S3YRIlCTNJuNa1-OcwDlRjo7M" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the loginUser token and paste in fauna’s GraphQL playground as the authorization token.&lt;/p&gt;

&lt;p&gt;Try and run this query&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query ListTodos {
  todos {
    data {
      text
      completed
      user {
        username
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you should see that it only displays the todo of “User1”.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You would see the URL to your Azure function which you can use in your client.&lt;/p&gt;

&lt;p&gt;By the time you would be reading this post, the created tokens I used won’t be functional.&lt;/p&gt;

&lt;p&gt;Thanks for reading through and learning a little bit about the awesomeness of Fauna.&lt;/p&gt;

</description>
      <category>fauna</category>
      <category>azure</category>
      <category>serverless</category>
    </item>
  </channel>
</rss>
