How to make a graphql schema with @cypher directive

Hello everyone I believe I have a simple problem but I can’t seem to solve it. I am using Graphql with Neo4j. I want to simply count relationships and put that capability in my graphql schema using the “CALL graphql.idl()” procedure and the “@cypher”directive,my code is as follows


CALL graphql.idl(
"
type User{
userSocialFollowsCount:@cypher(statement:”MATCH (u:User)-[:FOLLOWS_ON_SOCIALS]->(o:User) WITH u,count(o) as rels WHERE rels > 1 RETURN rels”)
}
"
)

``

Iv also tried this example from the internet 

CALL graphql.idl("

type User{

degree: Int @cypher(statement: "RETURN SIZE((this)-->())")

}

"
)



I am getting a syntax error in the Neo4j browser but I don’t understand where exactly the problem is.. I want to update my schema to be able to count relationships using the procedure.

I having installed the FULL APOC manually to the Graphene Database & they have confirmed it is installed okay ... I checked the version & it is compatible with the instance 


What am I doing wrong? How do you use the @cypher directive in a graphql schema with the “CALL graphql.idl()” procedure to update the schema? I want to do it automatically using the procedure but I seem to be missing something.. thanks 🙏

I think the answer to this question will give you a base to solve yours. Give it a shot:

also: Even those this is an older article it's got some good info:

@MuddyBootsCode

Thanks for the reference well explained and simplified but I am getting the following exception

"

Neo.ClientError.Procedure.ProcedureCallFailed

Failed to invoke procedure graphql.idl: Caused by: java.lang.RuntimeException: Error parsing IDL expected {'[', 'fragment', 'query', 'mutation', 'subscription', 'schema', 'scalar', 'type', 'interface', 'implements', 'enum', 'union', 'input', 'extend', 'directive', NAME} got '@' line 20 column 23
"

Ok can you please show me the query you're using in your schema please?

@MuddyBootsCode


graphql.idl(“      
type User {       
userFollows:@cypher(statement:”MATCH (n)-[r:User_Follows]->() RETURN COUNT(r)”)
}
“)

This is how I implemented it in my own:

wellCount: Int @cypher (
statement: "MATCH (this)<-[r:DRILLED_ON]-(:Well) with this, count(r) as rel_cnt return rel_cnt"
)

@MuddyBootsCode

I have re-adapted it to:

graphql.idl(“      
type User {       
followsCount:Int @cypher (
statement: "MATCH (this)<-[r:FOLLOWS_ON_SOCIALS]-(:User) with this, count(r) as rel_cnt return rel_cnt"
)
}
“)

I am getting the following error

Neo.ClientError.Statement.SyntaxError

Invalid input 'M': expected whitespace, '.', node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', '~', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR, ',' or ')' (line 20, column 13 (offset: 574)) "statement: "MATCH (this)<-[r:FOLLOWS_ON_SOCIALS]-(:User) with this, count(r) as rel_cnt return rel_cnt""

I’m not super sure how your schema is constructed but it looks like it’s not quite right. Is it in a schema.graphql file?

@MuddyBootsCode

when I run CALL graphql.schema() I get a schema and the schema generation command CALL graphql.idl() runs when I omit the field with the "@cypher" directive on the relevant Node type

You might try it the way that the project is set up on the GRANDstack example and have it in a schema.graphql file. It seems that the graphql.idl isn't playing well with Neo4j.

1 Like

@MuddyBootsCode

Thanks

btw How do you edit the graphql.schema file directly?

Here's a link to a project that @lyonwj is working on currently that has a great video series along with it. willow-grandstack/api/src at master · johnymontana/willow-grandstack · GitHub In it you can see how the schema.graphql file is set up and used.

Sorry I mean use the schema.graphql file not the graphql-schema.js

thanks but I am using the plugin on desktop and I think thats a Java/kotlin kind of set up... its just not working the graphql plugin on desktop with cypher :pensive:

I have even downgraded and re-installed APOC .. still not working

I have found the solution from the Github issue I was using double quotes throughout instead of single quotes to inclose the schema command :grin:

Glad you got it figured out. Though the original question was about how to handle counting relationships. :wink: