cancel
Showing results for 
Search instead for 
Did you mean: 

Can I send an variable to a custom Cypher query using the GraphQL library?

geronimo4j
Graph Buddy

Hi I'm using the GraphQL library and have a custom query that looks like this:

 posts: [Post] @cypher(statement: """
        MATCH (this)-[:HAS_MOVIE]->(m:Movie)<-[:TAGGED]-(review:Review)
        RETURN DISTINCT review
        LIMIT 10
        ORDER BY review.date DESC
    """)```

Just looks for any time a Playlist (i.e. `this`) has a movie, and then fetches all reviews for that playlist.

Because it's a custom query I can't just do posts with options `limit: 10` or feed it an offset, but I'm wondering if I can work around it and feed the custom query a variable from GraphQL? So even if it's not the built-in pagination it's still receiving a variable from the GraphQL query and leveraging that in the cypher query?

Thank you!
2 REPLIES 2

William_Lyon
Graph Fellow

Hi Dan -

Any field arguments that you define in the typedefs for a Cypher directive field will be passed to the Cypher query defined in the directive as Cypher parameters. So for example this would allow you to specify the number of reviews to return at query time (by specifying a value for limit as a field argument), in this case with a default value of 3.

(Using a default value for field arguments that will be passed as Cypher parameters is useful to ensure that parameter is always defined and you don't need to thnk about conditional logic for it in Cypher.)

 posts(limit: Int! = 3): [Post] @cypher(statement: """
        MATCH (this)-[:HAS_MOVIE]->(m:Movie)<-[:TAGGED]-(review:Review)
        RETURN DISTINCT review
        LIMIT $limit
        ORDER BY review.date DESC
    """)

geronimo4j
Graph Buddy

How cool is that!! Thank you!