graphql-crystal / graphql

GraphQL server library for Crystal
https://graphql-crystal.github.io/graphql/index.html
MIT License
134 stars 13 forks source link

Setting default values to an Array on an input object #39

Open jwoertink opened 2 years ago

jwoertink commented 2 years ago

I have this input object that I wanted to have an optional array with a default value, but I get this error:

@[GraphQL::InputObject]
class GoalInput
  include GraphQL::InputObjectType

  getter title : String
  getter milestones : Array(String)

  @[GraphQL::Field]
  def initialize(
    @title : String,
    @milestones : Array(String) = [] of String,
  )
  end
end
There was a problem expanding macro 'values'

Code in lib/graphql/src/graphql/language/nodes.cr:149:7

 149 | values({name: String, type: Type, default_value: FValue, directives: Array(Directive), description: String?})
       ^
Called macro defined in lib/graphql/src/graphql/language/ast.cr:4:7

 4 | macro values(args)

Which expanded to:

 >  8 |           @name = name
 >  9 | @type = type
 > 10 | @default_value = default_value
                         ^------------
Error: instance variable '@default_value' of GraphQL::Language::InputValueDefinition must be GraphQL::Language::FValue, not Array(String)

Removing the default value off the array no longer produces the error.

jgillich commented 2 years ago

Hmm, FValue is defined as:

https://github.com/graphql-crystal/graphql/blob/9ed2a5d220384a0aa119cdf6d693bae691e8522d/src/graphql/language/nodes.cr#L89

So we should be able to do this: https://play.crystal-lang.org/#/r/dyy2

:confused: