You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
all the solution mentioned in this issue are for mutation, is there a way to send custom output for a query?
I am trying to handle permissions for different queries and have decorators that
will filter premonitions from resolver example code would be like this:
classOutputMixin:
""" A class to all public classes extend to padronize the output """success=graphene.Boolean(default_value=True)
errors=graphene.Field(ExpectedErrorType)
classServerStatusType(DjangoObjectType, OutputMixin):
classMeta:
model=models.ServerStatusRecorddefadmin_only(fn):
defwrapper(root, info*args, **kwargs):
user=info.context.userifuser.is_admin:
returnfn(*args, **kwargs)
returncls(success=False, errors=Messages.NOT_VERIFIED)
returnwrapperclassStatusQuery(graphene.ObjectType):
current_status=graphene.Field(ServerStatusType)
@admin_onlydefresolve_current_status(cls, root, info, **kwargs):
returnmodels.ServerStatusRecord.objects.latest()
While this code works flawlessly with mutations it doesn't work with queries
because the the resolve function isn't accepting the object type as the first argument
the object exists under info.return_type.graphene_type(success=False, errors="fdsaf")
but its seems like a terrible hack and all the fields are required so i cant return just that.
The text was updated successfully, but these errors were encountered:
Is there a specific reason for you to use a custom type for errors instead of referring to the inbuilt exception handling?
When you throw an exception in graphene, these errors are incorporated into your response:
{
"data": {
your response data/query data
},
"errors": [
{
"message": "Exception",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"name of your query/mutation"
]
}
]
}
Your wrappers could look like this:
def admin_only(fn):
def wrapper(root, info *args, **kwargs):
user = info.context.user
if user.is_admin:
return fn(*args, **kwargs)
raise GraphQLError(Messages.NOT_VERIFIED)
return wrapper
The advantage of this approach is, that clients like Apollo support error handlers for this notation by default.
This does also work for queries, but you have to annotate the resolvers correctly.
If for other reasons you still want to stick to your approach, please send an example that includes a query that is also annotated. Maybe I can help you find the reason of failure then.
#513
all the solution mentioned in this issue are for mutation, is there a way to send custom output for a query?
I am trying to handle permissions for different queries and have decorators that
will filter premonitions from resolver example code would be like this:
While this code works flawlessly with mutations it doesn't work with queries
because the the resolve function isn't accepting the object type as the first argument
the object exists under
info.return_type.graphene_type(success=False, errors="fdsaf")
but its seems like a terrible hack and all the fields are required so i cant return just that.
The text was updated successfully, but these errors were encountered: