Ruby On Rails Middleware
Inigo requires an installation of Inigo::Middleware
into your Rails application. A demo Rails application using Inigo is available at https://github.com/inigolabs/workshops/tree/main/rails-demo.
Create an Inigo Service and Get an Inigo Token
- Set up a service and a token. If you still need one, follow Getting Started and Deployment.
You may use the Inigo CLI as such:
inigo create service >>your_service_name<<
inigo create token >>your_service_name<<
Gem Installation of the Inigo Middleware
Note: The Inigo Ruby on Rails Middleware requires Ruby 3.1 or greater.
gem install inigorb
Integration of the Inigo Middleware
Use middleware in your graphql controller or add it globally.
class GraphqlController < ApplicationController
use Inigo::Middleware
end
Middleware has to be initialized. You can pass your schema definition from graphql lib (graphql
gem is used in the following example) or omit this argument.
If you provide INIGO_SCHEMA_PATH
environment variable middleware will use it.
Configure the Schema class (If Necessary)
In your GraphQL::Schema
class (i.e. app/graphql/inigo_ror_schema.rb
), make sure the mutation
and query
are configured as such:
class InigoRorSchema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)
end
Create an Inigo Initializer
Create a new initializer at /config/initializers/inigo.rb
and add the sample code below. Change InigoRorSchema
to be the actual name of your Ruby GraphQL schema object.
Rails.application.config.after_initialize do
if defined?(Rails::Server)
require 'inigorb'
Inigo::Middleware.initialize_middleware(InigoRorSchema.to_definition)
end
end
Run Your Rails App with the Inigo Middleware
After making the code changes, using your Inigo token, you can run your Rails app as such:
export INIGO_SERVICE_TOKEN="ey..."
bin/rails server
To test, open your GraphiQL at http://localhost:3000/graphiql and run a GraphQL query.
It's best to name your GraphQL query so it can be labeled properly in Inigo Analytics. For example, query users
:
query users {
users {
name
}
}
Execute some GraphQL queries in GraphiQL and they will soon be visible on https://app.inigo.io under the relevant Inigo Service.
Router Setup Example (If Necessary)
If /graphiql
and /graphql
are not automatically set up for your Rails application in config/routes.rb
, it can be done as such:
Rails.application.routes.draw do
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end
post "/graphql", to: "graphql#execute"
end
Note: if your graphql server route is different from
/graphql
you have to provide the path inINIGO_PATH
environment variable.
Operation Store (graphql-pro gem)
To be able to use schema operation store with Inigo middleware in Ruby operation store has to be provided as a second parameter during middleware initialization
Rails.application.config.after_initialize do
if defined?(Rails::Server)
require 'inigorb'
Inigo::Middleware.initialize_middleware(InigoRorSchema.to_definition, InigoRorSchema.operation_store)
end
end
Also be aware that Inigo middleware will take care of resolving the query from the operationId which means your graphql controller has to accept query
parameter to avoid double backend calls
class GraphqlController < ApplicationController
use Inigo::Middleware
def execute
variables = prepare_variables(params[:variables])
query = params[:query] # this line is required to avoid double backend calls per request
operation_name = params[:operationName]
context = {
operation_id: params[:operationId]
}
result = InigoRorSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
end
end
Configuration Options
Name | Type | Required | Description |
---|---|---|---|
INIGO_SERVICE_TOKEN | string | Yes | Service token |
INIGO_PATH | string | No default: /graphql | Path to guard by middleware |
INIGO_SCHEMA_PATH | string | No | Path to GraphQL schema file |
INIGO_DEBUG | bool | No default: False | Enable debug mode |