Advertisement
Guest User

Untitled

a guest
Mar 1st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. from django.views.generic.base import TemplateView
  2.  
  3. from .query import schema
  4.  
  5.  
  6. class GraphQLContextMixin(object):
  7. # GraphQL query
  8. query = ''
  9. # The schema to execute query
  10. schema = None
  11. # Variables to be passed to the query
  12. variables = {}
  13.  
  14. def get_query(self):
  15. """
  16. Returns the GraphQL query to process.
  17. """
  18. return self.query
  19.  
  20. def get_schema(self):
  21. return self.schema
  22.  
  23. def get_context_data(self, **kwargs):
  24. """
  25. Executes the GraphQL query and set the context data to the response.
  26. """
  27. return kwargs
  28.  
  29. def get_variables(self, request):
  30. """
  31. Returns a dict of variables passed to the GraphQL query.
  32. """
  33. return self.variables
  34.  
  35. def get(self, request, *args, **kwargs):
  36. """
  37. Executes the GraphQL query and add result to the context data before rendering template.
  38. """
  39. if not self.get_query():
  40. return self.render_to_response(self.get_context_data())
  41.  
  42. response = self.get_schema().execute(
  43. self.get_query(),
  44. args=self.get_variables(request))
  45.  
  46. return self.render_to_response(
  47. self.get_context_data(errors=response.errors,
  48. data=response.data))
  49.  
  50.  
  51. class MutationView(GraphQLContextMixin):
  52. # Mutation query
  53. mutation = ''
  54.  
  55. def post(self, request, *args, **kwargs):
  56. """
  57. Executes the mutation and render the template with resulting data in the context.
  58. """
  59. if not self.get_mutation():
  60. return self.render_to_response(self.get_context_data())
  61.  
  62. response = self.get_schema().execute(
  63. self.get_mutation(),
  64. args=self.get_user_data(request))
  65.  
  66. return self.render_to_response(
  67. self.get_context_data(errors=response.errors,
  68. data=response.data))
  69.  
  70. def get_user_data(self, request):
  71. """
  72. Returns the user data passed as variables to the mutation query.
  73. """
  74. return request.POST
  75.  
  76. def get_mutation(self):
  77. """
  78. Returns the mutation query.
  79. """
  80. return self.mutation
  81.  
  82.  
  83. class LoginView(MutationView, TemplateView):
  84. mutation = '''
  85. mutation Login($username: String, $password: String) {
  86. createJWT(username: $username, password: $password) {
  87. jwt
  88. }
  89. }
  90. '''
  91.  
  92. template_name = 'accounts/login.html'
  93.  
  94. schema = schema
  95.  
  96.  
  97. class ListAllOrganizations(GraphQLContextMixin, TemplateView):
  98. query = '''
  99. query GetAllOrganizations {
  100. allOrganizations {
  101. name
  102. }
  103. }
  104. '''
  105.  
  106. template_name = 'organizations/list_all.html'
  107.  
  108. schema = schema
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement