Git Product home page Git Product logo

graphql-extras's Introduction

GraphQL::Extras

Build Version

A collection of utilities for building GraphQL APIs.

Installation

Add this line to your application's Gemfile:

gem 'graphql-extras'

And then execute:

$ bundle

Usage

GraphQL::Extras::Controller

The graphql gem will generate a controller for you with a bunch of boilerplate. This module will encapsulate that boilerplate:

class GraphqlController < ApplicationController
  include GraphQL::Extras::Controller

  def execute
    graphql(schema: MySchema, context: { current_user: current_user })
  end
end

GraphQL::Extras::Batch::AssociationLoader

This is a subclass of GraphQL::Batch::Loader that performs eager loading of Active Record associations.

loader = GraphQL::Extras::Batch::AssociationLoader.for(:blog)
loader.load(Post.first)
loader.load_many(Post.all)

GraphQL::Extras::Batch::Resolvers

This includes a set of convenience methods for query batching.

class Post < GraphQL::Schema::Object
  include GraphQL::Extras::Batch::Resolver

  field :blog, BlogType, resolve: association(:blog), null: false
  field :comments, [CommentType], resolve: association(:comments, preload: { comments: :user }), null: false
  field :blog_title, String, null: false

  def blog_title
    association(object, :blog).then(&:title)
  end
end

GraphQL::Extras::Types

In your base classes, you should include the GraphQL::Extras::Types.

class BaseObject < GraphQL::Schema::Object
  include GraphQL::Extras::Types
end

class BaseInputObject < GraphQL::Schema::InputObject
  include GraphQL::Extras::Types
end

Date

This scalar takes a Date and transmits it as a string, using ISO 8601 format.

field :birthday, Date, required: true

DateTime

This scalar takes a DateTime and transmits it as a string, using ISO 8601 format.

field :created_at, DateTime, required: true

Note: This is just an alias for the ISO8601DateTime type that is included in the graphql gem.

Decimal

This scalar takes a BigDecimal and transmits it as a string.

field :weight, BigDecimal, required: true

Upload

This scalar is used for accepting file uploads.

field :image, Upload, required: true

It achieves this by passing in all file upload parameters through context. This will work out of the box if you're using GraphQL::Extras::Controller.

Here's an example using CURL:

$ curl -X POST \
    -F query='mutation { uploadFile(image: "image") }' \
    -F [email protected] \
    localhost:3000/graphql

Take note of the correspondence between the value "image" and the additional HTTP parameter called -F [email protected].

See apollo-link-upload for the client-side implementation.

RSpec integration

Add the following to your rails_helper.rb (or spec_helper.rb).

require "graphql/extras/rspec"

Now, you can run tests like so:

RSpec.describe "hello" do
  let(:context) { { name: "Ray" } }
  let(:schema)  { use_schema(Schema, context: context) }
  let(:queries) { graphql_fixture("hello.graphql") }

  it "allows easily executing queries" do
    result = schema.execute(queries.hello)

    expect(result).to be_successful_query
    expect(result['data']['hello']).to eq("world")
  end
end

The graphql_fixture method assumes that your queries live in spec/fixtures/graphql. You can change this assumption with the following configuration:

RSpec.configure do |config|
  config.graphql_fixture_path = '/path/to/queries'
end

Development

To install dependencies:

$ bundle install

To run the test suite:

$ bundle exec rspec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/graphql-extras.

graphql-extras's People

Contributors

macbury avatar rzane 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.