Graphql-ruby: New-style GraphQL::Function

Created on 11 May 2018  路  4Comments  路  Source: rmosolgo/graphql-ruby

Hello, first of all, thanks for a great gem! I certainly glad that in version 1.8 it automatically camelizes all the keys, this is definitely s step forward.

Right now I'm trying to use the new approach as much as possible with 0.18.0.pre11. The problem is that the docs are still outdated and I can't find answers to my questions yet, so I'm looking at the sources to get the idea at least somehow.

So far I understood that Mutation class should look like:

class Mutation < GraphQL::Schema::Object
  field :do_stuff, mutation: Mutations::DoStuff
  field :do_other_stuff, mutation: Mutations::DoOtherStuff
end

Which is described in docs actually. And I do each mutation:

class ApplicationMutation < GraphQL::Schema::Mutation
  field :success, Boolean, null: false
  field :errors, [Types::Shared::Error], null: true
end

class DoStuff < ApplicationMutation
  argument :some_arg, ID, required: true

  field :result, Types::Appointment, null: true

  def resolve(**args)
    record = Model.new(**args)
    if record.save
      {
        success: true,
        result: record
      }
    else
      {
        errors: record.errors
      }
    end
  end
end

This is kinda clear, I just set up a common payload structure for all the mutations and implementing the interface. Please tell me, do you see any problems with this approach? Am I doing this as it is intended?

The same story is with queries, especially with collection queries. I found out that the cleanest way to implement queries is to use functions. So I do this:

class Query < GraphQL::Schema::Object
  field :some_records, function: Queries::SomeRecords.new
end

And collection query also have a common structure:

class Collection < GraphQL::Function
  def self.collection(type)
    type do
      name "#{type.graphql_name}Collection"

      field :collection, types[type]
      field :count, types.Int
    end
  end
end

class SomeRecords < Collection
  description 'Returns some records'

  collection Types::SomeRecord

  def call(_obj, _args, context)
    collection = SomeRecords.all

    OpenStruct.new(
      collection: collection,
      count: collection.size
    )
  end
end

The only unfortunate this is that functions have still be defined in the old style type-wise. Or maybe I miss something? Also, am I doing it correctly again?

Thanks for your answers in advance!

Most helpful comment

Hey, I'm working on a replacement for GraphQL::Function here: https://github.com/rmosolgo/graphql-ruby/pull/1472 feel free to have a look there if you're interested, I'll be sure to push some docs too, before I merge.

That's the last thing on my list before releasing 1.8.0, which will also fix all the doc links (it seems like RubyDoc.info doesn't like my .pre versions :S)

All 4 comments

Oh wow, just found out that I need to do: field :collection, type.to_non_null_type.to_list_type.to_non_null_type Band doesn't work for the types defined as < GraphQL::Schema::Object. While field :count, !types.Int works at the same context.

Hey, I'm working on a replacement for GraphQL::Function here: https://github.com/rmosolgo/graphql-ruby/pull/1472 feel free to have a look there if you're interested, I'll be sure to push some docs too, before I merge.

That's the last thing on my list before releasing 1.8.0, which will also fix all the doc links (it seems like RubyDoc.info doesn't like my .pre versions :S)

Great news, thanks, looking forward to using it!

One more question, why enum values are not uppercased by default? I've thought this is a standard. And would be nice to define it as value :foobar, 'Description' and get FOOBAR enum value in the sc hema.

Released in 1.8.0

Was this page helpful?
0 / 5 - 0 ratings