Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import unittest
import graphene
from graphene import ObjectType, Argument, String, Int, Float
from graphene_custom_directives import CustomDirectiveMeta, CustomDirectivesMiddleware
__author__ = 'ekampf'
class QueryRoot(ObjectType):
string_value = String(value=Argument(String))
int_value = Int(value=Argument(Int))
float_value = Float(value=Argument(Float))
@graphene.resolve_only_args
def resolve_string_value(self, value=None):
return value
@graphene.resolve_only_args
def resolve_int_value(self, value=None):
return value
@graphene.resolve_only_args
def resolve_float_value(self, value=None):
return value
schema = graphene.Schema(query=QueryRoot, directives=CustomDirectiveMeta.get_all_directives())
"""Test GraphQL mutation.
Send message to the user or all users.
"""
class Output(graphene.ObjectType):
"""Mutation result."""
message = graphene.String()
userId = UserId()
class Arguments:
"""That is how mutation arguments are defined."""
message = graphene.String(required=True)
userId = graphene.Argument(UserId, required=False)
def mutate(self, info, message, userId=None): # pylint: disable=unused-argument
"""Send message to the user or all users."""
assert self is None, "Root `self` expected to be `None`!"
# Notify subscribers.
OnChatMessageSent.notify(message=message, userId=userId)
return SendChatMessage.Output(message=message, userId=userId)
class KickOutUser(graphene.Mutation):
"""Test GraphQL mutation.
Stop all subscriptions associated with the user.
"""
if operation != "delete":
input_type = registry.get_type_for_model(model, for_input=operation)
if not input_type:
# factory_kwargs.update({'skip_registry': True})
input_type = factory_type(
"input", DjangoInputObjectType, operation, **factory_kwargs
)
global_arguments[operation].update(
{input_field_name: Argument(input_type, required=True)}
)
else:
global_arguments[operation].update(
{
"id": Argument(
ID,
required=True,
description="Django object unique identification field",
)
}
)
global_arguments[operation].update(arguments)
_meta = SerializerMutationOptions(cls)
_meta.output = cls
_meta.arguments = global_arguments
_meta.fields = django_fields
_meta.output_type = output_type
_meta.model = model
_meta.serializer_class = serializer_class
_meta.input_field_name = input_field_name
MenuDelete,
MenuItemCreate,
MenuItemDelete,
MenuItemMove,
MenuItemUpdate,
MenuUpdate,
)
from .resolvers import resolve_menu, resolve_menu_items, resolve_menus
from .sorters import MenuItemSortingInput, MenuSortingInput
from .types import Menu, MenuItem
class MenuQueries(graphene.ObjectType):
menu = graphene.Field(
Menu,
id=graphene.Argument(graphene.ID, description="ID of the menu."),
name=graphene.Argument(graphene.String, description="The menu's name."),
description="Look up a navigation menu by ID or name.",
)
menus = FilterInputConnectionField(
Menu,
query=graphene.String(description=DESCRIPTIONS["menu"]),
sort_by=MenuSortingInput(description="Sort menus."),
filter=MenuFilterInput(description="Filtering options for menus."),
description="List of the storefront's menus.",
)
menu_item = graphene.Field(
MenuItem,
id=graphene.Argument(
graphene.ID, description="ID of the menu item.", required=True
),
description="Look up a menu item by ID.",
filter=StaffUserInput(description="Filtering options for staff users."),
sort_by=UserSortingInput(description="Sort staff users."),
description="List of the shop's staff users.",
query=graphene.String(description=DESCRIPTIONS["user"]),
)
service_accounts = FilterInputConnectionField(
ServiceAccount,
filter=ServiceAccountFilterInput(
description="Filtering options for service accounts."
),
sort_by=ServiceAccountSortingInput(description="Sort service accounts."),
description="List of the service accounts.",
)
service_account = graphene.Field(
ServiceAccount,
id=graphene.Argument(
graphene.ID, description="ID of the service account.", required=True
),
description="Look up a service account by ID.",
)
user = graphene.Field(
User,
id=graphene.Argument(graphene.ID, description="ID of the user.", required=True),
description="Look up a user by ID.",
)
def resolve_address_validation_rules(
self, info, country_code, country_area=None, city=None, city_area=None
):
return resolve_address_validation_rules(
info,
\n the current node is returned")
class Order(Enum):
ASC = 'asc'
DESC = 'desc'
class Query(graphene.ObjectType):
"""
Query for node path
"""
node_path_by_name = graphene.List(
StructureNode,
node_name=graphene.String(required=True),
order=graphene.Argument(graphene.Enum.from_enum(Order)),
description="Returns the path from root to the node within structure"
)
@Auth.user_roles('Admin', 'Default User', 'Super Admin')
def resolve_node_path_by_name(self, info, **kwargs):
node_name = kwargs['node_name']
order = kwargs.get('order')
query = StructureNode.get_query(info)
order = asc if order == 'asc' else desc
node = query.filter(
StructureModel.name.ilike(node_name.strip())).first()
if not node:
raise GraphQLError('node not found')
return node.path_to_root(order=order)
_meta.model = mutation_class._meta.model
_meta.stream = stream
_meta.serializer_class = copy.deepcopy(mutation_class._meta.serializer_class)
_meta.mutation_class = mutation_class
serializer_fields = [(to_snake_case(field.strip('_')).upper(), to_snake_case(field))
for field in _meta.serializer_class.Meta.fields]
model_fields_enum = Enum('{}Fields'.format(mutation_class._meta.model.__name__), serializer_fields,
description='Desired {} fields in subscription\'s notification.'
.format(mutation_class._meta.model.__name__))
arguments = {
'channel_id': Argument(String, required=True, description='Websocket\'s channel connection identification'),
'action': Argument(ActionSubscriptionEnum, required=True,
description='Subscribe or unsubscribe action : (create, update or delete)'),
'operation': Argument(OperationSubscriptionEnum, required=True, description='Operation to do'),
'id': Argument(ID, description='ID field value that has the object to which you wish to subscribe'),
'data': List(model_fields_enum, required=False)
}
_meta.arguments = arguments
super(Subscription, cls).__init_subclass_with_meta__(_meta=_meta, description=description, **options)
def get_all_links_input_fields():
return {
# this creates an input field using the LinkOrderBy custom enum
'order_by': graphene.Argument(LinkOrderBy)
}
def __init__(self, type, *args, **kwargs):
kwargs.update({
FILTERS_FIELD: Argument(GenericScalar),
ORDER_BY_FIELD: Argument(List(String)),
PAGE_FIELD: Argument(Int),
PAGINATE_BY_FIELD: Argument(Int),
})
super(PeeweeConnectionField, self).__init__(type, *args, **kwargs)
players = gh.List(gh.NonNull(gh.Int), required=True, description='参与玩家UID')
winners = gh.List(gh.NonNull(gh.Int), required=True, description='胜利玩家UID')
started_at = gh.DateTime(auto_now_add=True, required=True, description='开始时间')
duration = gh.Int(required=True, description='持续时间')
class GameRewardInput(gh.InputObjectType):
player_id = gh.Int(required=True, description='玩家ID')
type = gh.String(required=True, description='积分类型')
amount = gh.Int(required=True, description='数量')
class GameOps(gh.ObjectType):
archive = gh.Field(
Game,
game=gh.Argument(GameInput, required=True, description='游戏元数据'),
archive=gh.String(required=True, description='游戏 Replay 数据(Base64)'),
description='保存游戏存档',
)
add_reward = gh.Field(
Game,
game_id=gh.Int(required=True, description='游戏ID'),
rewards=gh.List(gh.NonNull(GameRewardInput), required=True, description='积分列表'),
description='增加游戏积分',
)