繁体   English   中英

GraphQL + Django:使用原始 PostgreSQL 查询解析查询

[英]GraphQL + Django: resolve queries using raw PostgreSQL query

当使用外部数据库从多个表中获取数据时,将 GraphQL 与 Django 一起使用的最佳方法是什么(即,创建一个 Django 模型来表示数据与我的数据库中的单个表不对应)?

我的方法是暂时放弃使用 Django 模型,因为我认为我还没有完全理解它们。 (我对 Django 和 GraphQL 完全陌生。)我已经建立了一个简单的项目,其中包含一个连接了外部 Postgres DB 的应用程序。 我遵循了Graphene Django 教程中的所有设置,然后当我意识到我创建的模型是几个表的混合物时遇到了障碍。

我有一个查询发送回映射到模型中字段的正确列,但我不知道如何使其成为动态连接,以便当我的 API 被命中时,它查询我的数据库并将行映射到模型我在 Django 中定义的模式。

从那以后,我的方法一直是避免模型并使用 Steven Luscher 的演讲中展示的更简单的方法:在 30 分钟内从零到 GraphQL

TLDR;

目标是能够访问我的 GraphQL 端点,使用 django.db.connection 中的游标对象来获取应解析为 OrderItemTypes 的 GraphQLList 的字典列表(见下文)。

问题是当我通过查询访问以下端点时,每个值都为空值:

localhost:8000/api?query={orderItems{date,uuid,orderId}}

返回:

{ "data":{ "orderItems":[ {"date":null, "uuid":null, "orderId":null }, ... ] } }

项目/主/应用程序/schema.py

import graphene
from django.db import connection


class OrderItemType(graphene.ObjectType):
    date = graphene.core.types.custom_scalars.DateTime()
    order_id = graphene.ID()
    uuid = graphene.String()

class QueryType(graphene.ObjectType):
    name = 'Query'
    order_items = graphene.List(OrderItemType)

    def resolve_order_items(root, args, info):
        data = get_order_items()

        # data prints out properly in my terminal
        print data
        # data does not resolve properly
        return data


def get_db_dicts(sql, args=None):
    cursor = connection.cursor()
    cursor.execute(sql, args)
    columns = [col[0] for col in cursor.description]
    data = [
        dict(zip(columns, row))
        for row in cursor.fetchall() ]

    cursor.close()
    return data

def get_order_items():
    return get_db_dicts("""
        SELECT j.created_dt AS date, j.order_id, j.uuid
        FROM job AS j
        LIMIT 3;
    """)

在我的终端中,我从 QueryType 的解析方法打印,我可以看到数据成功地从我的 Postgres 连接返回。 然而,GraphQL 给了我空值,所以它必须在解析方法中,某些映射被搞砸了。

[ { 'uuid': u'7584aac3-ab39-4a56-9c78-e3bb1e02dfc1', 'order_id': 25624320, 'date': datetime.datetime(2016, 1, 30, 16, 39, 40, 573400, tzinfo=<UTC>) }, ... ]

如何将我的数据正确映射到我在 OrderItemType 中定义的字段?

这里还有一些参考:

项目/主/ schema.py

import graphene

from project.app.schema import QueryType AppQuery

class Query(AppQuery):
    pass

schema = graphene.Schema(
    query=Query, name='Pathfinder Schema'
)

文件树

|-- project
    |-- manage.py
    |-- main
        |-- app
            |-- models.py
            |-- schema.py
        |-- schema.py
        |-- settings.py
        |-- urls.py

GraphQL Python / Graphene 上的默认解析器尝试使用 getattr 解析根对象中的给定 field_name。 因此,例如,名为order_items的字段的默认解析器将类似于:

def resolver(root, args, context, info):
    return getattr(root, 'order_items', None)

知道,在dict执行getattr时,结果将为None (要访问 dict 项目,您必须使用__getitem__ / dict[key] )。

因此,解决您的问题可能就像从dicts更改为将内容存储到namedtuples

import graphene
from django.db import connection
from collections import namedtuple


class OrderItemType(graphene.ObjectType):
    date = graphene.core.types.custom_scalars.DateTime()
    order_id = graphene.ID()
    uuid = graphene.String()

class QueryType(graphene.ObjectType):
    class Meta:
        type_name = 'Query' # This will be name in graphene 1.0

    order_items = graphene.List(OrderItemType)

    def resolve_order_items(root, args, info):
        return get_order_items()    


def get_db_rows(sql, args=None):
    cursor = connection.cursor()
    cursor.execute(sql, args)
    columns = [col[0] for col in cursor.description]
    RowType = namedtuple('Row', columns)
    data = [
        RowType(*row) # Edited by John suggestion fix
        for row in cursor.fetchall() ]

    cursor.close()
    return data

def get_order_items():
    return get_db_rows("""
        SELECT j.created_dt AS date, j.order_id, j.uuid
        FROM job AS j
        LIMIT 3;
    """)

希望这可以帮助!

这是临时解决方法,尽管我希望有一些更清洁的方法来处理 snake_cased 字段名。

项目/主/应用程序/schema.py

from graphene import (
    ObjectType, ID, String, Int, Float, List
)
from graphene.core.types.custom_scalars import DateTime
from django.db import connection

''' Generic resolver to get the field_name from self's _root '''
def rslv(self, args, info):
    return self.get(info.field_name)


class OrderItemType(ObjectType):
    date = DateTime(resolver=rslv)
    order_id = ID()
    uuid = String(resolver=rslv)
    place_id = ID()

    ''' Special resolvers for camel_cased field_names '''
    def resolve_order_id(self, args, info):
    return self.get('order_id')

    def resolve_place_id(self, args, info):
        return self.get('place_id')

class QueryType(ObjectType):
    name = 'Query'
    order_items = List(OrderItemType)

    def resolve_order_items(root, args, info):
        return get_order_items()

也可以更改graphene.object 的默认解析器。

我相信以下内容在重组后会起作用。

from graphene.types.resolver import dict_resolver

class OrderItemType(ObjectType):

    class Meta:
        default_resolver = dict_resolver

    date = DateTime()
    order_id = ID()
    uuid = String()
    place_id = ID()

即使这个方法不能直接适用于上述问题,这个问题也是我在研究如何做到这一点时发现的。

dict_resolver

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM