Securing GraphQL
Use the @secureGraphQL decorator to protect access to your GraphQL integrations.
This decorator enables you to easily control access to your GraphQL endpoints, ensuring that only authorized users can access your data and resources.
When you use the @secureGraphQL decorator, the decorated function accepts a parameter of type GraphqlContext (a dict in Python). This
provides the full context of the GraphQL query, including the actual query, variables, operation name, and more.
The security function returns a boolean: true permits the request and false denies it.
- TypeScript
- Python
Backend code
import { secureGraphQL, SquidService, GraphqlContext } from '@squidcloud/backend';
export class ExampleService extends SquidService {
@secureGraphQL('usersGraphQl')
secureUsersGraphQl(context: GraphqlContext): boolean {
// TODO - Implement your security logic here
}
}
Backend code
from squidcloud_backend import SquidService, secure_graphql
class ExampleService(SquidService):
@secure_graphql('usersGraphQl')
def secure_users_graphql(self, context: dict) -> bool:
# TODO - Implement your security logic here
return False
In TypeScript, the context is a typed GraphqlContext object; in Python, the security function receives the same
context as a dict with keys such as query, variables, and operationName.