concept @cypher in category graphql

appears as: @cypher
Fullstack GraphQL Applications With GRANDstack

This is an excerpt from Manning's book Fullstack GraphQL Applications With GRANDstack.

  • neo4j-graphql.js A package to make it easier to use GraphQL and Neo4j together. neo4j-graphql-js translates GraphQL queries to a single Cypher query, eliminating the need to write queries in GraphQL resolvers and for batching queries. It also exposes the Cypher query language through GraphQL via the @cypher schema directive.
  • We can also use the @cypher schema directive to resolve object and array fields. Let’s add a recommended business field to the Business type. We’ll use a simple Cypher query to find common businesses that other users reviewed. For example, if a user likes "Market on Front", we could recommend other businesses that users who reviewed "Market on Front" also reviewed.

    Listing 4.28. Cypher
    MATCH (b:Business {name: "Market on Front"})<-[:REVIEWS]-(:Review)<-[:WROTE]-(:User)-[:WROTE]->(:Review)-[:REVIEWS]->(rec:Business)
    WITH rec, COUNT(*) AS score
    RETURN rec ORDER BY score DESC

    We can make use of this Cypher query in our GraphQL schema by including it in a @cypher directive on the recommended field in our Business type definition.

    Listing 4.29. index.js - GraphQL type definitions
    type Business {
        businessId: ID!
        averageStars: Float! @cypher(statement:"MATCH (this)<-[:REVIEWS]-(r:Review) RETURN avg(r.stars)")
        recommended(first: Int = 1): [Business] @cypher(statement: """
          MATCH (this)<-[:REVIEWS]-(:Review)<-[:WROTE]-(:User)-[:WROTE]->(:Review)-[:REVIEWS]->(rec:Business)
          WITH rec, COUNT(*) AS score
          RETURN rec ORDER BY score DESC LIMIT $first
        """)
        name: String!
        city: String!
        state: String!
        address: String!
        location: Point!
        reviews: [Review] @relation(name: "REVIEWS", direction: "IN")
        categories: [Category] @relation(name: "IN_CATEGORY", direction: "OUT")
    }

    We also define a first field argument, which is passed to the Cypher query included in the @cypher directive and acts as a limit on the number of recommended businesses returned.