Graphql-go: Unable to use scalar Time for Inputs

Created on 17 Aug 2018  路  5Comments  路  Source: graph-gophers/graphql-go

It looks like when you try to use scalar time as an input, you get an error message "cant unmarshal (string) as time.Time

screen shot 2018-08-16 at 9 54 03 pm

Most helpful comment

@Melaninneal time.Time is not an available input type, you should not use it. Use graphql.Time instead

All 5 comments

Make sure that string is RFC3339 compliant like "2006-01-02T15:04:05Z07:00" (see source)

screen shot 2018-08-17 at 6 37 41 pm

still the same issue when i use compliant strings

looks like the issue is coming from

this function in github.com/graph-gophers/graphql-go/internal/exec/packer/packer.go

Basically i pass in a string but when i hit if reflect.TypeOf(input).ConvertibleTo(typ) line, it returns false because "strings" and Time are not convertible types

func unmarshalInput(typ reflect.Type, input interface{}) (interface{}, error) {
if reflect.TypeOf(input) == typ {
return input, nil
}

switch typ.Kind() {
case reflect.Int32:
    switch input := input.(type) {
    case int:
        if input < math.MinInt32 || input > math.MaxInt32 {
            return nil, fmt.Errorf("not a 32-bit integer")
        }
        return int32(input), nil
    case float64:
        coerced := int32(input)
        if input < math.MinInt32 || input > math.MaxInt32 || float64(coerced) != input {
            return nil, fmt.Errorf("not a 32-bit integer")
        }
        return coerced, nil
    }

case reflect.Float64:
    switch input := input.(type) {
    case int32:
        return float64(input), nil
    case int:
        return float64(input), nil
    }

case reflect.String:
    if reflect.TypeOf(input).ConvertibleTo(typ) {
        return reflect.ValueOf(input).Convert(typ).Interface(), nil
    }
}

return nil, fmt.Errorf("incompatible type")

}

@Melaninneal time.Time is not an available input type, you should not use it. Use graphql.Time instead

@vetcher is correct. You have to define scalar Time in your schema and use the type defined in this library https://github.com/graph-gophers/graphql-go/blob/master/time.go#L11

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vladimiroff picture vladimiroff  路  6Comments

saward picture saward  路  6Comments

Hagbarth picture Hagbarth  路  6Comments

JitinGuglani picture JitinGuglani  路  6Comments

aquiseb picture aquiseb  路  4Comments