Ejemplo n.º 1
0
    def get_response(self, request, data):
        """Extract the information from the request, execute the GraphQL query and form the response.

        Args:
            request (HttpRequest): Request Object from Django
            data (dict): Parsed content of the body of the request.

        Returns:
            response (dict), status_code (int): Payload of the response to send and the status code.
        """
        query, variables, operation_name, id = GraphQLView.get_graphql_params(request, data)

        execution_result = self.execute_graphql_request(request, data, query, variables, operation_name)

        status_code = 200
        if execution_result:
            response = {}

            if execution_result.errors:
                response["errors"] = [GraphQLView.format_error(e) for e in execution_result.errors]

            if execution_result.invalid:
                status_code = 400
            else:
                response["data"] = execution_result.data

            result = response
        else:
            result = None

        return result, status_code
Ejemplo n.º 2
0
 def get_graphql_params(request, data):
     """
     Workaround for createUploadLink
     :param request:
     :param data:
     :return:
     """
     if 'operations' in request.POST:
         data = json.loads(request.POST.get('operations'))
     return GrapheneGraphQLView.get_graphql_params(request, data)
Ejemplo n.º 3
0
    def get_graphql_params(request, data):
        query, variables, operation_name, id_ = _GraphQLView.get_graphql_params(
            request,
            data,
        )

        content_type = _GraphQLView.get_content_type(request)
        if content_type == "multipart/form-data":
            operations = json.loads(data.get("operations", "{}"))
            files_map = json.loads(data.get("map", "{}"))
            for k, v in files_map.items():
                for f in v:
                    _obj_set(operations, f, k)
            query = operations.get("query")
            variables = operations.get("variables")

        return query, variables, operation_name, id_
Ejemplo n.º 4
0
    def wrapped_view(request, *args, **kwargs):
        try:
            data = json.loads(request.body.decode("utf-8"))
            query, variables, operation_name, id = GraphQLView.get_graphql_params(
                request, data)

            assert operation_name in CACHE_WHITELIST

            hashed_query = sha1(str(query).encode("utf-8")).hexdigest()
            cache_key = (hashed_query, variables, operation_name, id,
                         IS_LOCALHOST)

            if cache_key in cache:
                response = cache.get(cache_key)
            else:
                response = view_func(request, *args, **kwargs)
                cache.set(cache_key, response, CACHE_TIMEOUT)
        except:
            response = view_func(request, *args, **kwargs)

        return response