Code Monkey home page Code Monkey logo

dry-graphql's Introduction

Dry::GraphQL

Build Status

dry-graphql makes dry-types and dry-struct play nicely with GraphQL. It adds a graphql_type method which will automatically generate a graphql-ruby. This takes the manual work out of maintaining multiple type definitions. Specify your dry-type, and re-use it for GraphQL.

Usage

Here is an example of using it with dry-struct:

class User < Dry::Struct
  module Types
    include Dry.Types()
  end

  attribute :name, Types::Strict::String.optional
  attribute :age, Types::Coercible::Integer
  attribute :middle_name, Types::Coercible::String
end

class UserType < User.graphql_type(only: [:name, :age])
  # you can add other fields here if you want
end

class Query < GraphQL::Schema::Object
  field :user, UserType, null: false
end

class Schema < GraphQL::Schema
  query Query
end

puts schema.to_definition # =>
# type Query {
#   user: User!
# }
#
# type User {
#   age: Int!
#   name: String
# }
end

Usage with rom-sql

To add a graphql_type method to a ROM::Relation:

require 'dry/graphql/rom'

class Users < ROM::Relation[:sql]
  extend Dry::GraphQL::ROM
end

Installation

Add this line to your application's Gemfile:

gem 'dry-graphql'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dry-graphql

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/adhawk/dry-graphql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Dry::GraphQL project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

dry-graphql's People

Contributors

ianks avatar psparrow avatar tao-galasse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dry-graphql's Issues

Warnings coming from dry-graphql lib

Hello ! I've been having these warnings in my project:

/home/florian/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-graphql-2.2.0/lib/dry/graphql.rb:28: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/home/florian/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-graphql-2.2.0/lib/dry/graphql/schema_builder.rb:22: warning: The called method `initialize' is defined here
/home/florian/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-graphql-2.2.0/lib/dry/graphql/schema_builder.rb:31: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/home/florian/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-graphql-2.2.0/lib/dry/graphql/schema_builder.rb:22: warning: The called method `initialize' is defined here

But I don't know how to fix them since it seems that they come from the gem.
Hope you can help me out, thanks a lot !

DuplicateTypeNamesError if graphql_name is not given

Hello, I use dry-graphql for it's purpose, turning my dry structs to graphql type. Here is one of my types:

module Types
  module Events
    class TitleEmittedType < ::Events::TitleEmitted.graphql_type(skip: SKIPPED_ATTRIBUTES)
      graphql_name 'TitleEmitted'

      description 'Event for emitted title'
    end
  end
end

With it's struct:

module Events
  class TitleEmitted < MovementEvent
    attribute :id, Types::UUID
    attribute :label, Types::String
    attribute :quantity, Types::Integer
    attribute? :bucket_id, Types::Integer.optional
    attribute? :bucket_metadata, Types::BucketMetadata.optional
    attribute :register_id, Types::Integer
    attribute :right_id, Types::Integer
    attribute? :special_right_ids, Types::Array.of(Types::Integer).default([].freeze)
    attribute? :voting_rights, Types::Integer.optional
    attribute? :financial_rights, Types::Integer.optional
    attribute :nature, Types::String
    attribute? :source_title_id, Types::UUID.optional
    attribute? :source_bucket_id, Types::Integer.optional
    attribute? :source_label, Types::String.optional
    attribute :state, Domain::Types::TitleState
    attribute :sub_titles, Types::Array.of(Types::EventTitleInput)

    def destination_bucket_ids
      super + sub_titles.pluck(:bucket_id)
    end

    def destination_title_ids
      super + sub_titles.pluck(:title_id)
    end
  end
end

This config worked great until I updated rubocop-graphql, it gave me this:

GraphQL/GraphqlName: graphql_name should be specified only for overrides.
    class TitleEmittedType < ::Events::TitleEmitted.graphql_type(skip: SKIPPED_ATTRIBUTES)

So I removed the graphql_name and now I have this errors when generating the schema:

GraphQL::Schema::DuplicateTypeNamesError:
  Multiple definitions for `Events__titleemitted`. Previously found DryGraphQLGeneratedTypeForEvents::TitleEmitted (Class), then found DryGraphQLGeneratedTypeForEvents::TitleEmitted (Class) at Mutation.createGroup.group.registers.edges.node.movements.edges.node.event.possible_types.userMetadata

I think this might be related: rmosolgo/graphql-ruby#3949

Hope you can help !

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.