cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Returning a list of labels and counts with GraphQL

Not sure whether this is possible, I tried and couldn't get it to work. I'd like to run a query to return a list of label names and the associated node counts based on this cypher query:
MATCH (n)
RETURN labels(n), COUNT(labels(n))

I thought I might be able to create a new type such as

type Item{
Name: String!
Count: Int!
}

type Query{
itemList: [Item] @cypher(statement: "MATCH (n) RETURN labels(n) as Name,COUNT(n) as Count")
}

However it returns an error. Am I trying something that is not possible at all or am I taking the wrong approach.

Thanks for any pointers.

5 REPLIES 5

MuddyBootsCode
Graph Steward

Why are you trying to get all node labels back? A little more information about your use case would probably help.

I'd like to generate a bar chart that visually shows which labels, i.e. node types, have the most number of items. Furthermore I'd like to extend this into a generic query with a WHERE clause such as this:
MATCH (n) WHERE n.Name = 'Some Value' RETURN labels(n), COUNT(n)
This would tell me where I do have a hit in my set of labels. Running the cypher queries works fine, however, I was wondering whether I can use this approach in GraphQL. I thought the approach with a @cypher directive might work, but it doesn't. So I am obviously missing something.
Does this help?

MuddyBootsCode
Graph Steward

You might be able to accomplish this with an interface type. Check out the grand stack docs here https://grandstack.io/docs/graphql-interface-union-types/

I've done something similar with this before. This an example from my code:

const HOME_QUERY = gql`
  query {
    LandAlert {
      id
      title
      text
      days
      date
      tract {
        id
        name
      }
    }
    ROWAlert {
      id
      title
      text
      days
      date
      ROW {
        id
        projectName
      }
    }
    WellAlert {
      id
      title
      text
      days
      date
      well {
        id
        name
      }
    }
    LeaseAlert {
      id
      title
      text
      days
      date
      lease {
        id
        leaseName
      }
    }
    Reminder {
      id
      title
      text
      days
      date
    }
}

You would have to add your logic in to get a count of each type but as long as your know what you're after you can query it like that.

Thanks, Michael. I tried the interface option, but that doesn't seem to work for my use case. However, it gave me an idea for a work around that I could potentially work with. It would require a lot of work, but it might allow me to do what I had in mind.
My request doesn't seem to me such a strange idea, hence I was hoping there would be a more direct solution. Especially since it is possible to achieve it by running a cypher query on the database.

In any case, thanks again for the suggestion.

Yeah sometimes the GraphQL layer makes it rougher than it should be to implement things. The only other thing I could think of would to be to return a map or JSON type object and work with that, which might be easier.