Coder Social home page Coder Social logo

hasura-openapi-to-actions-converter's Introduction

Hasura OpenAPI -> Action Generator via Action Transforms

Introduction

This repo contains a working implementation of a basic OpenAPI-Spec-to-Hasura-Action generator.

It uses the newly-released "Action Transforms" feature to generate mappings from the GraphQL object fields to the corresponding OpenAPI request fields.

Example

Take the following basic FastAPI server, which exposes a /login endpoint:

from dataclasses import dataclass
from fastapi import FastAPI

@dataclass
class AuthInput:
    email: str
    password: str

@dataclass
class AuthToken:
    token: str

app = FastAPI()

@app.post("/login", response_model=AuthToken)
async def login(input: AuthInput):
    return {"token": "<fake JWT token>"}

The OpenAPI definition generated for this is (as YAML to save space):

---
openapi: 3.0.2
paths:
  "/login":
    post:
      summary: Login
      operationId: login_login_post
      requestBody:
        content:
          application/json:
            schema:
              "$ref": "#/components/schemas/AuthInput"
        required: true
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/AuthToken"
components:
  schemas:
    AuthInput:
      title: AuthInput
      required:
      - email
      - password
      type: object
      properties:
        email:
          title: Email
          type: string
        password:
          title: Password
          type: string
    AuthToken:
      title: AuthToken
      required:
      - token
      type: object
      properties:
        token:
          title: Token
          type: string

This tool generates the following Hasura metadata definition, which create the proper GraphQL input/output types to match the OpenAPI spec, as well as the Kriti lang request_transform which maps the values from the payload automatically:

{
  actions: [
    {
      name: 'login',
      definition: {
        handler: 'http://localhost:8000/api/login',
        output_type: 'AuthToken!',
        arguments: [ { name: 'params', type: 'AuthInput!' } ],
        request_transform: {
          body: '{ "email": {{$body.input.params.email}}, "password": {{$body.input.params.password}} }',
          content_type: 'application/json',
          template_engine: 'Kriti'
        },
        type: 'mutation',
        kind: 'synchronous'
      }
    }
  ],
  custom_types: {
    objects: [
      {
        name: 'AuthToken',
        fields: [ { name: 'token', type: 'String!' } ]
      }
    ],
    input_objects: [
      {
        name: 'AuthInput',
        fields: [
          { name: 'email', type: 'String!' },
          { name: 'password', type: 'String!' }
        ]
      }
    ]
  }
}

Limitations

Currently, it only supports requests which take payloads entirely from a body object (requestBody)

It is not possible to support URL params until Hasura gets the ability to template values into a request URL in addition to a request body. See this issue for details:

hasura/graphql-engine#8147

There are likely many edgecases and broken scenarios as well. Array values aren't yet supported, and this doesn't generate enum types yet either.

This was just me being curious how much effort it would take to get something like this working. It turns out the answer was ~2 hours.

hasura-openapi-to-actions-converter's People

Contributors

gavinray97 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.