-
At present, I have an Two options that come to mind though I am not sure how to implement either:
I am open to either of these approaches or even a third one that I haven't come up with but am not sure how to best implement these approaches idiomatically in Ariadne. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
When auth-only is the default for your API, then your security should be opt-out. How to do this? I would write custom def protect_schema(schema):
protect_schema_type_fields(schema.type_map["Query"])
protect_schema_type_fields(schema.type_map["Mutation"])
def protect_schema_type_fields(type_fields):
for field_name, field_type in type_fields.items():
if not field_type.ast_node:
field_type.resolve = protect_schema_resolver(field_type.resolve or default_resolver)
continue
directives = [
directive.name.value
for directive in field_type.ast_node.directive
]
if "public" not in directives:
field_type.resolve = protect_schema_resolver(field_type.resolve or default_resolver)
def protect_schema_resolver(resolver):
@wraps(resolver)
def protected_resolver(obj, info, **kwargs):
if not info.context["user"]:
raise GraphQLError("This field requires authentication.")
return resolver(obj, info, **kwargs)
return protected_resolver And then the use would be: schema = make_executable_schema(...)
protect_schema(schema) |
Beta Was this translation helpful? Give feedback.
-
Yeah. I've did that code 100% from memory so ofcourse I've got something wrong here 😅 |
Beta Was this translation helpful? Give feedback.
When auth-only is the default for your API, then your security should be opt-out.
How to do this?
I would write custom
protect_schema
utility that would takeschema
frommake_executable_schema
and would wrap resolvers onQuery
andMutation
type withrequires_only
decorator, but only for fields that don't have@public
directive, eg: