Saving JSON response

I want to save resume information as json into dgraph database based on my defined schema which is very similar to the structure of the json, but without the need to give the same structure in the mutation or create a map from schema to json in mutation. Is there a way to do the above

  {
            "ContactInformation": {
                "CandidateName": {
                    "FormattedName": "Dev Test",
                    "GivenName": "Dev",
                    "FamilyName": "Dev"
                },
                "Telephones": [
                    {
                        "Raw": "404-663-1274",
                        "Normalized": "+1 404-663-1274",
                        "InternationalCountryCode": "1",
                        "AreaCityCode": "404",
                        "SubscriberNumber": "45-1274"
                    }
                ],
                "EmailAddresses": [
                    "dev@mail.com"
                ],
                "WebAddresses": [
                    {
                        "Address": "https://www.linkedin.com/in/dev_test",
                        "Type": "LinkedIn"
                    }
                ]
            }
        }

This is tagged GraphQL, so using only the GraphQL API, the answer is no.

GraphQL is a strictly typed API. It only allows in exactly what is predefined in the schema. It can accept less (unless required inputs) but it cannot accept more.

This however is possible with DQL, but if not correctly typed it will not be accessible by the GraphQL API endpoint generated from your pre-defined GraphQL schema.

Behind the GraphQL API endpoint is the Dgraph database. Dgraph is a loosely typed database and you can add data in through the bulk loader, live loader, or mutate endpoint that does not match any predefined schema exactly. JSON data can be added through the mutate endpoint, bulk loader and live loader (thanks for the correction @MichelDiz)

In answering your question, it is possible to “save” JSON responses directly to Dgraph, but not with the GraphQL API, and these saved JSON responses will not be exposed to your GraphQL API to later query, and mutate unless they are first typed correctly.

2 Likes

Correcting this, Dgraph accepts JSON in both loaders.

➜  ~ dgraph live -h | grep json
  -f, --files string                 Location of *.rdf(.gz) or *.json(.gz) file(s) to load
      --format string                Specify file format (rdf or json) instead of getting it from filename

Also, it exports it in JSON. https://dgraph.io/docs/deploy/admin/

1 Like