In my Django app with Graphene for GraphQL, how do I intercept the Graphene GraphQL messages so I can sanitize the content? I tried creating a custom graphql view and a custom_execute function and using that in the urlpatterns endpoint definition:
class CustomGraphQLView(GraphQLView):
def execute_graphql_request(self, request, data, query, variables, operation_name, show_graphiql=False):
document = parse(query)
result = custom_execute(self.schema, document, root_value=request, variable_values=variables, operation_name=operation_name)
return result
def custom_execute(schema, document, root_value=None, variable_values=None, operation_name=None, context_value=None):
# Execute the query
try:
result = execute(
schema=schema,
document=document,
root_value=root_value,
context_value=context_value,
variable_values=variable_values,
operation_name=operation_name
)
if isinstance(result, ExecutionResult):
if result.errors:
modified_errors = [GraphQLError("Custom error handling message") for error in result.errors]
result.errors = modified_errors
return result
except Exception as e:
return ExecutionResult(data=None, errors=[GraphQLError('test'), GraphQLError(str(e))])
urlpatterns = [
path('graphql', CustomGraphQLView.as_view(graphiql=not IS_PROD)),
]
Inside the execute
function in my custom_execute
function, I get the error: "Expected <Schema instance> to be a GraphQL schema."
I have verified that the schema object is of type <class ‘graphene.types.schema.Schema’>. How do I fix this error. Is this even the recommended way to customize the error messages generated by graphql?