Graphql-ruby: Default value for enum with hash as value?

Created on 9 May 2019  路  3Comments  路  Source: rmosolgo/graphql-ruby

Hello,
Thanks for much for building this library! I have an enum like:

class AcceptedChallengesSortParamType < Types::BaseEnum
    value 'acceptedAt_ASC', value: { accepted_at: :asc }
    value 'acceptedAt_DESC', value: { accepted_at: :desc }
end

and it works great, except I can't figure out how to specify default_value other than nil. If I try:

argument :sort_by, [AcceptedChallengesSortParamType], required: false, default_value: ['acceptedAt_ASC']

I get:

F, [2019-05-09T10:02:20.694440 #1237] FATAL -- : GraphQL::Schema::InvalidTypeError (Query is invalid: field "acceptedChallenges" argument "sortBy" default value ["acceptedAt_ASC"] is not valid for type [AcceptedChallengesSortParam!] (Can't resolve enum AcceptedChallengesSortParam for "acceptedAt_ASC")):

What is the correct way to specify default_value?

Most helpful comment

To clarify further, since the argument accepts a list type, the default value should be an array, so you probably want:

argument :sort_by, [AcceptedChallengesSortParamType], required: false, default_value: [{accepted_at: :asc}]

It seems awkward because GraphQL-Ruby's assumption is that you'd be using _application values_ as enum value: overrides, not complex data structures. Anyways, let me know if it works for you!

All 3 comments

graphql-ruby version: 1.9.3
rails version: 5.2.2.1

Hi,

The default_value has to match the value in your enum.

value: { accepted_at: :asc } --> default_value: { accepted_at: :asc }

Best,
Balint

To clarify further, since the argument accepts a list type, the default value should be an array, so you probably want:

argument :sort_by, [AcceptedChallengesSortParamType], required: false, default_value: [{accepted_at: :asc}]

It seems awkward because GraphQL-Ruby's assumption is that you'd be using _application values_ as enum value: overrides, not complex data structures. Anyways, let me know if it works for you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pareeohnos picture pareeohnos  路  3Comments

peterphan1996 picture peterphan1996  路  3Comments

rmosolgo picture rmosolgo  路  4Comments

jturkel picture jturkel  路  3Comments

Plummat picture Plummat  路  4Comments