0
votes

We have db collection which is little complicated. Many of our keys are JSON objects where fields aren't fixed and change based on input given by user on UI. How should we write mongoose and GraphQL Schema for such complex type ?

{
    "_id" : ObjectId("5ababb359b3f180012762684"),
    "item_type" : "Sample",
    "title" : "This is sample title",
    "sub_title" : "Sample sub title",
    "byline" : "5c6ed39d6ed6def938b71562",
    "lede" : "Sample description",
    "promoted" : "",
    "slug" : [ 
        "myurl"
    ],
    "categories" : [ 
        "Technology"
    ],
    "components" : [ 
        {
            "type" : "Slide",
            "props" : {
                "description" : {
                    "type" : "",
                    "props" : {
                        "value" : "Sample value"
                    }
                },
                "subHeader" : {
                    "type" : "",
                    "props" : {
                        "value" : ""
                    }
                },
                "ButtonWorld" : {
                    "type" : "a-button",
                    "props" : {
                        "buttonType" : "product",
                        "urlType" : "Internal Link",
                        "isServices" : false,
                        "title" : "Hello World",
                        "authors" : [ 
                            {
                                "__dataID__" : "Qm9va0F1dGhvcjo1YWJhYjI0YjllNDIxNDAwMTAxMGNkZmY=",
                                "_id" : null,
                                "First_Name" : "John",
                                "Last_Name" : "Doe",
                                "Display_Name" : "John Doe",
                                "Slug" : "john-doe",
                                "Role" : 1
                            }
                        ],
                        "isbns" : [ 
                            "9781497603424"
                        ],
                        "image" : "978-cover.jpg",
                        "price" : "8.99",
                        "bisacs" : [],
                        "customCategories" : [],
                },
                "salePrice" : {
                    "type" : "",
                    "props" : {
                        "value" : ""
                    }
                }
            }
        }, 
   "tags" : [ 
        {
            "id" : "5abab58042e2c90011875801",
            "name" : "Tag Test 1"
        }, 
        {
            "id" : "5abab5831242260011c248f9",
            "name" : "Tag Test 2"
        }, 
        {
            "id" : "592450e0b1be5055278eb5c6",
            "name" : "horror",
        }, 
        {
            "id" : "59244a96b1be5055278e9b0e",
            "name" : "Special Report",
            "_id" : "59244a96b1be5055278e9b0e"
        }
    ],
    "created_at" : ISODate("2018-03-27T21:44:21.412Z"),
    "created_by" : ObjectId("591345bda429e90011c1797e")
}

I believe Mongoose have Mixed type but how do i represent such complex type in Apollo GraphQL Server and Mongoose Schema. Also, currently my resolver is just models.product.find(). So if i have such complex type, need to understand what update needs to make to my resolver.

It will be great if i get complete solution for GraphQL Apollo schema, mongoose schema and resolver for my data.

1
This question has been asked before. See here, here and here. Use a custom scalar or a JSON scalar.Daniel Rearden
Since there's already a number of similar questions, I think we should close this question. @jvm you could delete this one and, if you like, upvote any of the answers on the other questions that you find helpfulDaniel Rearden

1 Answers

0
votes

Finally found solution for problem.

You can declare new type and reference it in typeDef for GraphQL Schema.

In mongoose model, you can reference it as {type: Array}