0.0
No release in over a year
A thin wrapper to generate mock GraphQL API responses via OpenAI gpt-3.5-turbo in Ruby apps.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.8, >= 2.8.1
~> 2.0, >= 2.0.11
~> 6.1.5.1
~> 13.0.3
~> 6.0.0
~> 1.48, >= 1.48.1
~> 0.20.0

Runtime

 Project Readme

MockGraphqlAI

openai-gpt-3.5-turbo graphql-dev rubocop-dev rspec-dev rails-dev

Residing on Server Side, there's always a need to be one step ahead to provide APIs for client side teams to consume. To enable seamless development and parallel processing. Here comes a thin wrapper to generate mock GraphQL API responses via OpenAI gpt-3.5-turbo in Ruby apps.

When Real APIs are ready, tweak the configuration. Its minimal ❤️

Getting Started

Pre-requisites

It is built over OpenAI's ruby library i.e., ruby-openai. Make sure to follow through here. You can check the graphql-ruby documentation here as well

Install

Add this to your Gemfile

gem 'mock_graphql_ai', '~> 1.1'

Run bundle install

Invoke Generator

In order to add mock files you have to run install generator

rails g mock_graphql_ai:install

It'll do the following changes:

  • Add a module named mockable in app/controllers/concerns
  • Add a controller named graphql_mocks_controller to bypass graphql-ruby original controller
  • Add a model named MockGraphqlResponse to persist responses inside db for repititions. Saving the OpenAI overhead
  • Add the respective migration for model with attributes
  • Add a mock route inside routes.rb
  get '/imitate_graphql', to: 'graphql_mocks#imitate', as: :imitate_graphql

Note: You can extend the model layer but remember to keep the original configuration intact, super essential for gem to work properly for now

Enable Mockers 🔥

Include mockable module inside graphql_controller and add a before filter to bypass the #execute

class GraphqlController < ApplicationController
  include Mockable
  
  before_action :bypass_graphql, only: [:execute] if Rails.env.development?
  
  def execute
    # Your normal code goes here ...
  end
end

If you notice the code inside graphql_mocks_controller there is a simple API call to service that generates the responses and persist them for future use

class GraphqlMocksController < ApplicationController
  def imitate
    result = MockGraphqlAi::Resource.new(
      query: params[:query],
      token: Rails.application.credentials.dig(:openai, :access_token) # Your ENV var
    ).respond!

    render json: result
  end 
end

Requirements

This gem requires Ruby 2.6+ Also it requires graphql to override its ability to generate responses. Rails 6.x or plus recommended

License

Check out LICENSE file in project's root

Want to Contribute?

Check out CONTRIBUTING.md and the docs directory.

To generate a PULL Request:

Check out PULL_REQUEST_TEMPLATE.md