cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Custom mutation not working

ccn
Node Link

Hi there,

I've having trouble running a custom mutation. First a high level overview of what the mutation does, given a definition, which has a title, some content, a section it belongs to, and finally a list of definitions that it uses, create all the required relationships.

Now in terms of cypher:

MATCH (s) WHERE id(s) = 6
CREATE (s) <- [:DEFINITION_OF] - (x :Definition {title:"new node", content: "hi there"})  
WITH x, [24, 9] as ids 
UNWIND ids as i  
MATCH (d) WHERE id(d) = i
CREATE (x) <- [:USED_BY] - (d) 
RETURN x

Which results in this in the database:

My next step was to add this cyper query to my schema file, and so in the schema file we have this:

type Mutation {
    createDefinition(sec_id: ID!, title: String!, content: String, definitionsUsed: [ID!]) : Definition @cypher(statement: """
    MATCH (s) WHERE id(s) = $sec_id 
    CREATE (s) <- [:DEFINITION_OF] - (x :Definition {title: $title, content: $content})  
    WITH x, $definitionsUsed as ids 
    UNWIND ids as i  
    MATCH (d) WHERE id(d) = i
    CREATE (x) <- [:USED_BY] - (d) 
    RETURN x
    """)
}

I decided to do a sample call from the graphql playground to see if it was working, though when testing it I didn't seem to get a result:

And in the console:

16:10:40 api | 2020-11-05T21:10:40.628Z neo4j-graphql-js CALL apoc.cypher.doIt("MATCH (s) WHERE id(s) = $sec_id 
16:10:40 api | CREATE (s) <- [:DEFINITION_OF] - (x :Definition {title: $title, content: $content})  
16:10:40 api | WITH x, $definitionsUsed as ids 
16:10:40 api | UNWIND ids as i  
16:10:40 api | MATCH (d) WHERE id(d) = i
16:10:40 api | CREATE (x) <- [:USED_BY] - (d) 
16:10:40 api | RETURN x", {sec_id:$sec_id, title:$title, content:$content, definitionsUsed:$definitionsUsed, first:$first, offset:$offset}) YIELD value
16:10:40 api |     WITH apoc.map.values(value, [keys(value)[0]])[0] AS `definition`
16:10:40 api |     RETURN `definition` { .title } AS `definition`
16:10:40 api | 2020-11-05T21:10:40.629Z neo4j-graphql-js {
16:10:40 api |   "sec_id": "6",
16:10:40 api |   "title": "example2",
16:10:40 api |   "content": "something",
16:10:40 api |   "definitionsUsed": [
16:10:40 api |     "7"
16:10:40 api |   ],
16:10:40 api |   "first": -1,
16:10:40 api |   "offset": 0
16:10:40 api | }

And in the database, there is no change either. I'm not sure as to why this doesn't work, so any help is appreciated!

1 ACCEPTED SOLUTION

ccn
Node Link

After lots of debugging, I realized my issue here, it's that the ID type was coming in as a string and so nothing matched when I tried to filer by ID (had to use the toInteger function), though I'll still leave this post here in case anyone has any comments on my method of constructing new nodes/ if there is a better way.

View solution in original post

1 REPLY 1

ccn
Node Link

After lots of debugging, I realized my issue here, it's that the ID type was coming in as a string and so nothing matched when I tried to filer by ID (had to use the toInteger function), though I'll still leave this post here in case anyone has any comments on my method of constructing new nodes/ if there is a better way.