How to return an object in json to be a hash and not a string hash


#1

Below is the api response:

{
    "total": 59198,
    "products": [
        {
            "id": "23423",
            "name": {
                "German": "s.Oliver BLACK LABE - Damen, Weiß, Größe 44"
            },
            "brand": "s.Oliver BLACK LABEL",
            "image_url": "https://some_url.com",
            "category": "Shirts & Tops"
        },...
   ]
}

and i’m using dry-struct to convert the response to ruby objects. For that below is the class:

class SomeService < Dry::Struct
    include Dry::Types.module

    attribute 'id',              String
    attribute 'brand',        String
    attribute 'image_url', String
    attribute 'category',   String
    attribute 'name',        Strict::hash
 end

Notice the attribute name. The name attribute in the response is json and for that i’m using Strict::Hash to convert the json in ruby hash. I’m not sure about it.

Still the response is like this:

{
        "brand": "s.Oliver BLACK LABEL",
        "name": "{\"German\"=>\"s.Oliver BLACK LABEL - Damen, Weiß, Größe 44\"}"
}

Let me know what am i doing wrong ?


#2

I’ve resolved this issue. The problem was not with the dry-struct but the graphql types.


#3

I recommend using your own Types module where you include built-in types, instead of including them in the struct classes, they have same names as some core classes like String, Hash etc. and you really want to have name namespaced.


#4

Yes absolutely. Thanks much