Skip to content

Using mutation is throwing me key error? #7

Open
@shyamgt

Description

@shyamgt

### CODE :

`import logging
import neo4j
import azure.functions as func
from neo4j_graphql_py import neo4j_graphql
from ariadne.wsgi import GraphQL
from ariadne import QueryType, make_executable_schema, MutationType

type_defs = '''
directive @cypher(statement: String!) on FIELD_DEFINITION
directive @relation(name:String!, direction:String!) on FIELD_DEFINITION

type RiskDomain {
name: String
colour: String
}

type Post {
id: String!
externalId: String
title: String
url: String
source: String
description : String
body: String
keywords: String
language: String
isSafe: Boolean
datePublished: String
imageUrl: String
imageHeight: Int
imageWidth: Int
active: Boolean
riskDomain(username: String): RiskDomain @cypher(statement:"""
MATCH (p:Post {id: this.id})
MATCH (rd:RiskDomain)<-[:FOLLOWS_RD]-(u:User)
WHERE u.name = $username
AND (rd)-[:CREATES]->(p)
RETURN rd
""")
}

type Query {
ListPost(user_name: String, first: Int, offset: Int): [Post] @cypher(
statement: """
CALL {
MATCH (p:Post)<-[:CREATES]-(rd:RiskDomain)<-[:FOLLOWS_RD]-(u:User)
WHERE u.name = $user_name
WITH p, rd
ORDER BY p.datePublished DESC
RETURN p
UNION
MATCH (p:Post)<-[:CREATES_POST]-(u1:User)
WHERE u1.name = $user_name
RETURN p
UNION
MATCH (p:Post)<-[:CREATES_POST]-(u2:User)<-[:FOLLOWS]-(u3:User)
WHERE u3.name = $user_name
RETURN p
}
RETURN p
ORDER BY p.datePublished DESC
"""
)

ListCVE_Posts(user_name: String, index_name: String): [Post] @cypher(
statement: "MATCH (a:Asset)<-[:HOLDS_AS]-(p:User) WHERE p.name = $user_name WITH apoc.text.replace(a.name, ' ', ' AND ') AS name WITH apoc.text.replace(name, name, '(' + name + ')' ) AS name WITH apoc.text.join(collect(name),' OR ') AS output CALL db.index.fulltext.queryNodes($index_name, output) YIELD node AS feed, score WITH feed MATCH (feed)<-[:CREATES]-(rd:CVEFeed)<-[:FOLLOWS_RD]-(u:User) WHERE u.name = $user_name RETURN feed")
}

type Mutation {
LikePost(user_refid: Int, post_id: Int): Post @cypher(
statement:"""
MATCH (u:User)
WHERE ID(u) = $user_refid
MATCH (p:Post)
WHERE ID(p) = $post_id
MERGE (u)-[r:LIKES_POST { userId: $user_refid, postId: $post_id}]->(p) ON CREATE SET r.datetime = datetime()
RETURN p
"""
)
FindPost(post_id: Int): [Post] @cypher(
statement:"""
MATCH(p:Post)
RETURN p
"""
)
}
'''

query = QueryType()
mutation = MutationType()

@query.field('ListCVE_Posts')
@query.field('ListPost')
@mutation.field('LikePost')
@mutation.field('FindPost')
def resolve(obj, info, **kwargs):
print(obj)
print(info)
return neo4j_graphql(obj, info.context, info, True, **kwargs)

schema = make_executable_schema(type_defs, [query, mutation])

driver = None

def context(request):
global driver
if driver is None:
driver = neo4j.GraphDatabase.driver("bolt://127.0.0.1:7687", auth=("neo4j", "test"))

return {'driver': driver, 'request': request}

rootValue = {}
app = GraphQL(schema=schema, root_value=rootValue, context_value=context, debug=True)

from azf_wsgi import AzureFunctionsWsgi

def main(req: func.HttpRequest) -> func.HttpResponse:
print("ok")
return AzureFunctionsWsgi(app).main(req)
`

### ERROR

GraphQL request:2:3
1 | mutation{
2 | FindPost(post_id:22) {
| ^
3 | title
Traceback (most recent call last):
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\graphql\execution\execute.py", line 623, in resolve_field_value_or_error
result = resolve_fn(source, info, **args)
File "C:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL\RiskPulseService_init_.py", line 99, in resolve
return neo4j_graphql(obj, info.context, info, True, **kwargs)
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\neo4j_graphql_py\main.py", line 23, in neo4j_graphql
query = cypher_query(context, resolve_info, **kwargs)
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\neo4j_graphql_py\main.py", line 55, in cypher_query
cyp_dir = cypher_directive(resolve_info.schema.query_type, resolve_info.field_name)
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\neo4j_graphql_py\utils.py", line 117, in fun
directive = field_directive(schema_type, field_name, directive_name)
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\neo4j_graphql_py\utils.py", line 112, in field_directive
return find(schema_type.fields[field_name].ast_node.directives, lambda d: d.name.value == directive_name)
KeyError: 'FindPost'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\graphql\execution\execute.py", line 668, in complete_value_catching_error
return_type, field_nodes, info, path, result
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\graphql\execution\execute.py", line 733, in complete_value
raise result
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\graphql\execution\execute.py", line 623, in resolve_field_value_or_error
result = resolve_fn(source, info, **args)
File "C:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL\RiskPulseService_init_.py", line 99, in resolve
return neo4j_graphql(obj, info.context, info, True, **kwargs)
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\neo4j_graphql_py\main.py", line 23, in neo4j_graphql
query = cypher_query(context, resolve_info, **kwargs)
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\neo4j_graphql_py\main.py", line 55, in cypher_query
cyp_dir = cypher_directive(resolve_info.schema.query_type, resolve_info.field_name)
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\neo4j_graphql_py\utils.py", line 117, in fun
directive = field_directive(schema_type, field_name, directive_name)
File "c:\Users\Shyam\Hailey\RiskPulse\Risk_Pulse_GraphQL.venv\lib\site-packages\neo4j_graphql_py\utils.py", line 112, in field_directive
return find(schema_type.fields[field_name].ast_node.directives, lambda d: d.name.value == directive_name)
graphql.error.graphql_error.GraphQLError: 'FindPost'

GraphQL request:2:3
1 | mutation{
2 | FindPost(post_id:22) {
| ^
3 | title

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions