graphiql-rails

repository·master·Indexed 19 days ago

https://github.com/rmosolgo/graphiql-rails

An engine that mounts the GraphiQL IDE within a Ruby on Rails application to provide a visual interface for interacting with GraphQL endpoints during development. It includes configurable options for query parameters, CSRF tokens, custom headers via Procs, and UI customization such as titles and logos.

Tokens
1.2K
Snippets
6
Records
8
Agent score
66%

What's inside graphiql-rails

  1. Mount the GraphiQL Engine in Rails routes

    master

    To serve the GraphiQL IDE, mount the GraphiQL::Rails::Engine in your config/routes.rb file. It is recommended to wrap this in a development environment check.

    Use the following options:

    • at:: The URL path where the GraphiQL interface will be served.
    • graphql_path:: The URL path of your existing GraphQL endpoint where GraphiQL will send queries.
    # config/routes.rb
    Rails.application.routes.draw do
      # ...
      if Rails.env.development?
        mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/your/endpoint"
      end
    end
  2. Configure GraphiQL-Rails settings

    master

    You can customize the behavior of the GraphiQL engine by configuring the GraphiQL::Rails::Config object. This allows you to set the page title, logo, initial query, and other operational settings.

    Available configuration attributes:

    • title: The title displayed in the browser tab.
    • logo: The URL for the logo displayed in the interface.
    • initial_query: A string containing the GraphQL query to be loaded by default.
    • query_params: Boolean indicating whether to include query parameters in the URL.
    • csrf: Boolean (defaults to true) that determines if the X-CSRF-Token header is automatically added to requests.
    • input_value_deprecation: Boolean to enable/disable input value deprecation warnings.
    • should_persist_headers: Boolean for header persistence behavior.
    • headers: A hash of custom headers to include in GraphQL requests.
  3. Reference: GraphiQL::Rails.config options

    master

    The following configuration keys are available in GraphiQL::Rails.config:

    KeyTypeDefaultDescription
    query_paramsbooleanfalseIf true, the GraphQL query string is persisted in the page's query parameters.
    initial_querystringnilThe query string to render in the query pane on the first visit.
    titlestringnilThe text to be rendered in the page <title> tag.
    logostringnilThe text used for the logo.
    csrfbooleantrueWhether to include X-CSRF-Token in GraphiQL's HTTP requests.
    header_editor_enabledbooleanfalseWhether to render the header editor in the UI.
    headershash (String => Proc){}Procs to fetch header values for requests. Procs receive the view_context as an argument.
    input_value_deprecationbooleanfalseWhether to render deprecated arguments.
    should_persist_headersbooleannilIf true, headers in the editor are persisted. If false, the 'Persist headers' toggle is hidden in settings.
    # Example: Setting a dynamic Authorization header using the view context
    GraphiQL::Rails.config.headers['Authorization'] = -> (context) { "bearer #{context.cookies['_graphql_token']}" }
  4. Add custom headers to GraphQL requests

    master

    The headers configuration attribute accepts a hash where keys are header names and values are Proc objects. These procs receive the view_context as an argument, allowing you to dynamically generate header values (e.g., pulling values from the Rails view context).

    By default, Content-Type: application/json is included. If csrf is enabled (which is the default), X-CSRF-Token is also automatically added using the view_context.form_authenticity_token method.

    # Adding a custom header using a proc
    config.headers["My-Header"] = -> (view_context) { "My-Value" }
  5. Access and configure GraphiQL::Rails configuration

    master

    The GraphiQL::Rails module provides a global config accessor to manage the gem's settings. You can access the current configuration via GraphiQL::Rails.config. This object is initialized automatically when the gem is loaded.

    # Access the configuration object
    config = GraphiQL::Rails.config
    
    # Example of setting a configuration value (assuming Config class methods exist)
    GraphiQL::Rails.config.some_setting = 'value'