Example #1
0
    def visit_Document(self, node):
        # we need to index all of the fragments before we process operations
        self._context.fragments = {
            f.name: f
            for f in node.definitions
            if isinstance(f, gqlast.FragmentDefinition)
        }

        gqlresult = gql_proc(
            self._context.gqlcore._gql_schema,
            self._context.query,
            variables={
                name[1:]: val.value
                for name, val in self._context.variables.items()
            },
            operation_name=self._context.operation_name,
        )

        if gqlresult.errors:
            for err in gqlresult.errors:
                raise g_errors.GraphQLCoreError(
                    err.message,
                    line=err.locations[0].line,
                    col=err.locations[0].column,
                )

        translated = dict(d for d in self.visit(node.definitions)
                          if d is not None)
        eql = next(v for v in translated.values())

        for el in eql[0].result.elements:
            # swap in the json bits
            if (isinstance(el.compexpr, qlast.FunctionCall)
                    and el.compexpr.func == 'to_json'):
                name = el.expr.steps[0].ptr.name
                el.compexpr.args[0].arg = qlast.StringConstant.from_python(
                    json.dumps(gqlresult.data[name], indent=4))

        return translated
Example #2
0
    def visit_Document(self, node):
        # we need to index all of the fragments before we process operations
        self._context.fragments = {
            f.name: f for f in node.definitions
            if isinstance(f, gqlast.FragmentDefinition)
        }

        gqlresult = gql_proc(
            self._context.gqlcore,
            self._context.query,
            variable_values={
                name[1:]: val for name, val in self._context.variables.items()
            },
            operation_name=self._context.operation_name,
        )

        if gqlresult.errors:
            for err in gqlresult.errors:
                raise GraphQLCoreError(
                    err.message,
                    line=err.locations[0].line,
                    col=err.locations[0].column,
                )

        translated = dict(
            d for d in self.visit(node.definitions) if d is not None)
        eql = next(v for v in translated.values())

        for el in eql[0].result.elements:
            # swap in the json bits
            if (isinstance(el.compexpr, qlast.TypeCast) and
                    el.compexpr.type.maintype.name == 'json'):
                name = el.expr.steps[0].ptr.name
                el.compexpr.expr.value = json.dumps(
                    gqlresult.data[name], indent=4)

        return translated