Created
November 16, 2017 19:13
-
-
Save andela-sjames/3f864dad09cea0f9f6237897c4102ea9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Script defined to create helper functions for graphql schema.""" | |
from graphql_relay.node.node import from_global_id | |
def get_object(object_name, relayId, otherwise=None): | |
try: | |
return object_name.objects.get(pk=from_global_id(relayId)[1]) | |
except: | |
return otherwise | |
def update_create_instance(instance, args, exception=['id']): | |
if instance: | |
[setattr(instance, key, value) for key, value in args.items() if key not in exception]you | |
# caution if you literally cloned this project, then be sure to have | |
# elasticsearch running as every saved instance must go through | |
# elasticsearch according to the way this project is configured. | |
instance.save() | |
return instance | |
def get_errors(e): | |
# transform django errors to redux errors | |
# django: {"key1": [value1], {"key2": [value2]}} | |
# redux: ["key1", "value1", "key2", "value2"] | |
fields = e.message_dict.keys() | |
messages = ['; '.join(m) for m in e.message_dict.values()] | |
errors = [i for pair in zip(fields, messages) for i in pair] | |
return errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment