jos97
jos97

Reputation: 405

How to return several results in a only one GraphQL request?

I have a GRANDstack app.

For this app I have a Cypher request in Neo4j like this:

MATCH (c:CLUSTER)
WITH c
ORDER BY c.cluster_score DESC
LIMIT 5
MATCH (c)<-[:HAS_CLUSTER]-(a:ARTICLE)
WITH c,a
ORDER BY a.article_score  DESC
RETURN c, collect(a)[..5] as articles

It returns the best articles in each clusters. I have to show this in my React interface. I would like to implement this in GraphQL but I don't know how to code this because I return 2 things in this request.

How can I write my request in GraphQL ?

Upvotes: 0

Views: 457

Answers (1)

Dan Starns
Dan Starns

Reputation: 3815

You are returning 2 values c and articles however there is no way to represent this in a GraphQL type. Make an intermediary type I will call mine Intermediary;

type CLUSTER {
   id: ID
}

type ARTICLE {
   id: ID
}

type Intermediary {
}

And embedded your CLUSTER and ARTICLE as fields on the type;

type Intermediary {
  cluster: CLUSTER 
  article: ARTICLE
}

Then use this type where your @cypper directive is;

type Query {
  myQuery: Intermediary @cypher(...)
}

When projecting the values out of your cypher query return an object representing your Intermediary type;

MATCH (c:CLUSTER)
WITH c
ORDER BY c.cluster_score DESC
LIMIT 5
MATCH (c)<-[:HAS_CLUSTER]-(a:ARTICLE)
WITH c,a
ORDER BY a.article_score  DESC
WITH c, collect(a)[..5] as articles
RETURN {
    cluster: c,
    articles: articles
}

Upvotes: 1

Related Questions