Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Pass custom params to @cypher directive #187

Closed
EduardTrutsyk opened this issue Feb 3, 2019 · 5 comments
Closed

Pass custom params to @cypher directive #187

EduardTrutsyk opened this issue Feb 3, 2019 · 5 comments

Comments

@EduardTrutsyk
Copy link

EduardTrutsyk commented Feb 3, 2019

Is it possible to pass custom params(for example currentUserId from the session) to @cypher directive?

I have req.user in context, and sometimes I need to write cypher query base on the current user id.

type Movie {
    title: String
    year: Int
    imdbRating: Float
    liked: Boolean @cypher(statement: """
      WITH {this} as this MATCH (this)<-[r:LIKE]-(:User{userId: $currentUserId}) RETURN COUNT(r)>0
    """)
}
@johnymontana
Copy link
Contributor

Ah yes, this is something we wanted to add for exactly these kind of cases. How should this be configured? I'm not sure it would it make sense to add the entire context object, but maybe just add the entire context.request or context.req?

@EduardTrutsyk
Copy link
Author

Great! Thanks @johnymontana

From my point of view, to make it flexible and independent, would be nice to add everything from context.cypherParams (or something like that).

context: ({ req }) => ({
      req,
      driver,
      cypherParams: {
          currentUserId: req.user.id
      }
})

@johnymontana
Copy link
Contributor

@EduardTrutsyk this is now implemented in v2.4.0. Could you give it a try?

@EduardTrutsyk
Copy link
Author

Everything works fine, good job guys! We can close #187

@btroop
Copy link

btroop commented Mar 23, 2019

Not sure if I'm missing something, but just wanted to call out that the only way I can get it to work for a cypher statement attached to a user-defined query is like this:

type Query {
    favoriteMovies: [Movie] @cypher(statement: """
      MATCH (m:Movies)<-[r:IS_FAVORITE]-(:User{userId: $cypherParams.currentUserId}) RETURN m
    """)
}

I tried it without the cypherParams prefix of the object and it threw errors looking for the $currentUserId parameter

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants