Giter VIP home page Giter VIP logo

hasura-actions-examples's Introduction

Hasura Actions Examples

This repo features examples of Actions to perform custom business logic with Hasura.

Each example comes with it's own schema with relevant migration files to get started.

hasura-actions-examples's People

Contributors

dependabot[bot] avatar geekboysupreme avatar newtonjose avatar praveenweb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

hasura-actions-examples's Issues

Send email with Amazon SES

An example on how to send email with Amazon SES

The current example uses SMTP to send emails. (Can be integrated with any provider that supports SMTP - like SendGrid, SparkPost etc).

We need a new route to show an example of how to send emails using Amazon SES using their SDK.

auth action in python flask

The current signup action is written in Node.js Express. The same example needs to be ported to python flask.

  • An endpoint /signup which receives name, username and password as part of the input argument in mutation

  • Return the id and token generated using a JWT library.

Hostname host.docker.internal does not resolve in Linux

The docker-compose up command fails on linux based systems as the hostname host.docker.internal is not resolved in Linux systems, throws could not translate host name "host.docker.internal" to address: Name or service not known error.

Workaround:

use docker's internal gateway address 172.17.0.1 instead of host.docker.internal while using the default docker network.

tried on Ubuntu 20.04.1 LTS

signup "message": "the key 'message' was not present

const fetch = require("node-fetch");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");

const HASURA_OPERATION = `
mutation ($username: String!, $password: String!) {
  insert_user_one(object: {
    username: $username,
    password: $password,
  }) {
    id
  }
}
`;

// execute the parent mutation in Hasura
const execute = async (variables, reqHeaders) => {
  const fetchResponse = await fetch("http://localhost:8080/v1/graphql", {
    method: "POST",
    headers: reqHeaders || {},
    body: JSON.stringify({
      query: HASURA_OPERATION,
      variables,
    }),
  });
  return await fetchResponse.json();
};

// Request Handler
const handler = async (req, res) => {
  // get request input
  const { username, password } = req.body.input;

  // run some business logic
  const salt = bcrypt.genSaltSync();
  let hashedPassword = await bcrypt.hash(password, salt);

  // execute the Hasura operation
  const { data, errors } = await execute(
    { username, password: hashedPassword },
    req.headers
  );

  // if Hasura operation errors, then throw error
  if (errors) {
    return res.status(400).json({
      message: errors.message,
    });
  }

  const tokenContents = {
    sub: data.insert_user_one.id.toString(),
    name: username,
    iat: Date.now() / 1000,
    "https://hasura.io/jwt/claims": {
      "x-hasura-allowed-roles": ["user", "anonymous"],
      "x-hasura-user-id": data.insert_user_one.id.toString(),
      "x-hasura-default-role": "user",
      "x-hasura-role": "user",
    },
    exp: Math.floor(Date.now() / 1000) + 24 * 60 * 60,
  };

  const token = jwt.sign(tokenContents, process.env.AUTH_PUBLIC_KEY);

  // success
  return res.json({
    ...data.insert_user_one,
    token: token,
  });
};

module.exports = handler;

Screen Shot 2020-05-19 at 22 40 20

Create a Custom Signup Mutation with Hasura Actions Error v1.2.0 when following https://www.youtube.com/watch?v=oqbxEp4FIjE&t=186s

Create a Custom Signup Mutation with Hasura Actions in Heroku deploy v1.2.0 from following:

https://www.youtube.com/watch?v=oqbxEp4FIjE&t=186s

mutation {
  signup(name: "Seb", home: "UK", email: "[email protected]", password: "test123") {
    id
  }
}

getting:

{
  "errors": [
    {
      "extensions": {
        "internal": {
          "error": {
            "type": "http_exception",
            "message": "ConnectionFailure Network.Socket.getAddrInfo (called with preferred socket type/protocol: AddrInfo {addrFlags = [AI_ADDRCONFIG], addrFamily = AF_UNSPEC, addrSocketType = Stream, addrProtocol = 0, addrAddress = <assumed to be undefined>, addrCanonName = <assumed to be undefined>}, host name: Just \"host.docker.internal\", service name: Just \"3000\"): does not exist (Name or service not known)"
          },
          "response": null,
          "request": {
            "body": {
              "session_variables": {
                "x-hasura-role": "admin"
              },
              "input": {
                "email": "[email protected]",
                "home": "UK",
                "name": "seb",
                "password": "test"
              },
              "action": {
                "name": "signup"
              }
            },
            "url": "http://host.docker.internal:3000/signup",
            "headers": []
          }
        },
        "path": "$",
        "code": "unexpected"
      },
      "message": "http exception when calling webhook"
    }
  ]
}

This solves it ?

Cloned this project then in auth directory

docker-compose -d
in localhost:8080

mutation {
  signup(name: "Seb", username: "[email protected]", password: "test123"){
    id
    name
  }
}

also:
"message": "ConnectionFailure Network.Socket.connect: <socket: 33>: does not exist (Connection refused)"

auth action login nodejs-express add

The current login action is written in Node.js Express.
-- An endpoint /login which receives username and password as part of the input argument in mutation
-- Return the id and token generated using a JWT library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.