Query To Get Nodes

example
mutation

(sivakarna) #1

Hi ,

Hope you can understand my requirement by seeing json example which i have posted bellow.

Graph

{
  "personName": "",
  "community": [
    {
      "communityName": "",
      "country": [
        {
          "countryName": "",
          "state": [
            {
              "stateName": "",
              "area": [
                {
                  "areaName": "",
                  "visited": [
                    {
                      "locationName": ""
                    }
                  ],
                  "mediator": [
                    {
                      "mediatorName": "",
                      "visited": [
                        {
                          "locationName": ""
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Required OutPut

{
  "areas": [
    {
      "areaName": "",
      "visited": [
        {
          "locationId": "123"
        },
        {
          "locationId": "1234"
        }
      ]
    }
  ]
}

(Or)

{
  "areas": [
    {
      "areaName": "",
      "visited": [
        {
          "locationId": "123"
        }
      ],
      "visitedthroughmediator": [
        {
          "locationId": "1234"
        }
      ]
    }
  ]
}

(Michel Conrado) #2

Not sure, but there you go.

  {
    var(func: eq(personName, "some name")) {
    personName
    community {
      communityName
      state {
        stateName
      AREA as area {
          areaName
          A as visited
          mediator {
            mediatorName
            B as visited
          }
        }
      }
    }
    }
  
    query1(func: uid(AREA)){
      visited @filter(uid(A)){
        locationId: uid
      }
      visitedthroughmediator : mediator {
        visited @filter(uid(B)) {
          locationId: uid
        }
      }  
    }
    
 }

(sivakarna) #3

@MichelDiz Thanks for reply,

 visitedthroughmediator : mediator {
        visited @filter(uid(B)) {
          locationId: uid
        }
      }

The above logic will return output like below which we not required

{
   "visitedthroughmediator": [
          {
            "visited": [
              {
                "locationId": "0x222e6"
              }
            ]
          }
        ]
}

we required like

{
   "visitedthroughmediator": [
          
              {
                "locationId": "0x222e6"
              }
          
        ]
}

so the final output we required as below

{
  "areas": [
    {
      "areaName": "",
      "visited": [
        {
          "locationId": "123"
        }
      ],
      "visitedthroughmediator": [
        {
          "locationId": "1234"
        },
       {
          "locationId": "12345"
        }
      ]
    }
  ]
}

(Michel Conrado) #4

The thing is that Graphql +- don’t work like that. The structure of the response will always be the same as the query structure. The most you can do is use normalize. https://docs.dgraph.io/query-language/#normalize-directive

Or try something like this:

{
    var(func: eq(personName, "some name")) {
    personName
    community {
      communityName
      state {
        stateName
      AREA as area {
          areaName
          A as visited
          mediator {
            mediatorName
            B as visited
          }
        }
      }
    }
    }
visited(func: uid(A)){
        locationId: uid
}
 visitedthroughmediator(func: uid(B)){
          locationId: uid
    }
}