File: __init__.py

package info (click to toggle)
python-graphene 3.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,935; makefile: 214; sh: 18
file content (39 lines) | stat: -rw-r--r-- 1,286 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from graphql.error import GraphQLError

from graphene.types.schema import Schema


def default_format_error(error):
    if isinstance(error, GraphQLError):
        return error.formatted
    return {"message": str(error)}


def format_execution_result(execution_result, format_error):
    if execution_result:
        response = {}
        if execution_result.errors:
            response["errors"] = [format_error(e) for e in execution_result.errors]
        response["data"] = execution_result.data
        return response


class Client:
    def __init__(self, schema, format_error=None, **execute_options):
        assert isinstance(schema, Schema)
        self.schema = schema
        self.execute_options = execute_options
        self.format_error = format_error or default_format_error

    def format_result(self, result):
        return format_execution_result(result, self.format_error)

    def execute(self, *args, **kwargs):
        executed = self.schema.execute(*args, **dict(self.execute_options, **kwargs))
        return self.format_result(executed)

    async def execute_async(self, *args, **kwargs):
        executed = await self.schema.execute_async(
            *args, **dict(self.execute_options, **kwargs)
        )
        return self.format_result(executed)