如何将 DRF 序列化器与 Graphene 一起使用

Posted

技术标签:

【中文标题】如何将 DRF 序列化器与 Graphene 一起使用【英文标题】:How to use DRF serializers with Graphene 【发布时间】:2019-08-23 02:35:12 【问题描述】:

我正在关注this tutorial 使用 GrapheneDjango,一切都很顺利,直到我到达 Integration with Django Rest Framework section。

本节说您可以通过创建序列化程序克隆来重用 DRF 序列化程序和 Graphene,但它没有说明如何处理此类克隆以重用DRF 带有 Graphene 的序列化程序。

这些是我的序列化程序和克隆:

from rest_framework import serializers
from graphene_django.rest_framework.mutation import SerializerMutation
from GeneralApp.models import Airport
from ReservationsManagerApp.serializers import ReservationSerializer
from ReservationsManagerApp.models import ReservationComponent, ReservationHotel, ReservationRoundtrip, ReservationTransfer, ReservationTour, ReservationService, Hotel

class ReservationMutation(SerializerMutation):
    class Meta:
        serializer_class = ReservationSerializer

class ReservationComponentGraphSerializer(serializers.ModelSerializer):
    component = serializers.SerializerMethodField()

    class Meta:
        model = ReservationComponent
        fields = ('id', 'reservation', 'dertour_bk', 'day', 'content_type', 'object_id', 'comment', 'is_invoiced', 'component')

    def get_component(self, instance):
        components_models = 
            'reservationhotel': ReservationHotel,
            'reservationroundtrip': ReservationRoundtrip,
            'reservationtransfer': ReservationTransfer,
            'reservationtour': ReservationTour,
            'reservationservice': ReservationService,
        

        component = components_models[instance.content_type.model].objects.get(id=instance.object_id)

        return self.get_component_string(instance.content_type.model, component)

    def get_component_string(self, component_model, component):
        components_get_string = 
            'reservationhotel': self.get_hotel_string,
            'reservationroundtrip': self.get_roundtrip_string,
            'reservationtransfer': self.get_transfer_string,
            'reservationtour': self.get_tour_string,
            'reservationservice': self.get_service_string,
        

        return components_get_string[component_model](component):

    def get_hotel_string(self, component):
        return component.hotel.name

    def get_roundtrip_string(self, component):
        return component.roundtrip.name

    def get_transfer_string(self, component):
        origin_str = self.get_place_str('origin', component)
        destination_str = self.get_place_str('destination', component)

        return " => ".format(origin_str, destination_str)

    def get_place_str(self, case, component):
        places_models = 
            'airport': Airport,
            'hotel': Hotel,
        

        if case == 'origin':
            return places_models[component.origin_content_type.model].objects.get(id=component.origin_object_id).name
        else:
            return places_models[component.destination_content_type.model].objects.get(id=component.destination_object_id).name

    def get_tour_string(self, component):
        return component.tour.name

    def get_service_string(self, component):
        return component.service.name

class ReservationComponentMutation(SerializerMutation):
    class Meta:
        serializer_class = ReservationComponentGraphSerializer

这是我的schemas.py

import graphene
from graphene_django.types import DjangoObjectType
from ReservationsManagerApp.models import   Reservation, ReservationComponent
from InvoicesManagerApp.models import Invoice, InvoiceEntry, InvoiceEntryComponent
from PaymentsManagerApp.models import Payment, PaymentReservationComponent

class ReservationType(DjangoObjectType):
    class Meta:
        model = Reservation

class ReservationComponentType(DjangoObjectType):
    class Meta:
        model = ReservationComponent

class InvoiceType(DjangoObjectType):
    class Meta:
        model = Invoice

class InvoiceEntryType(DjangoObjectType):
    class Meta:
        model = InvoiceEntry

class InvoiceEntryComponentType(DjangoObjectType):
    class Meta:
        model = InvoiceEntryComponent

class PaymentType(DjangoObjectType):
    class Meta:
        model = Payment

class PaymentReservationComponentType(DjangoObjectType):
    class Meta:
        model = PaymentReservationComponent

class  Query(object):
    all_reservations = graphene.List(ReservationType)
    all_reservation_components = graphene.List(ReservationComponentType)
    all_invoices = graphene.List(InvoiceType)
    all_invoice_components = graphene.List(InvoiceEntryType)
    all_invoice_entries_components = graphene.List(InvoiceEntryComponentType)
    all_payment = graphene.List(PaymentType)
    all_payment_reservation_components = graphene.List(PaymentReservationComponentType)

    def resolve_all_reservations(self, info, **kwargs):
        return Reservation.objects.all()

    def resolve_all_reservation_components(self, info, **kwargs):
        return ReservationComponent.objects.select_related('reservation').all()

    def resolve_all_invoice_entries_components(self, info, **kwargs):
        return InvoiceEntryComponent.objects.select_related('reservation_component').all()

    def resolve_all_payment_reservation_components(self, info, **kwargs):
        return PaymentReservationComponent.objects.select_related('reservation_component').all()

我不知道我是否遗漏了一些明显的东西,但我不明白我应该如何将这些序列化程序突变与石墨烯一起使用。我想一定是通过某种方式配置Query 类,但我在文档中找不到参考。

【问题讨论】:

我相信 DRF 序列化器只能用于突变,不能用于查询 【参考方案1】:

从你的例子:

import graphene
from your.schemas import Query as YourQuery
from your.serializers import ReservationComponentMutation

# notice its an objecttype, and i've also added some debug
class Mutation(graphene.ObjectType):
    debug = graphene.Field(DjangoDebug, name="_debug")

    create_reservation = ReservationComponentMutation.Field()


class Query(YourQuery, graphene.ObjectType):
    pass


class Mutation(Mutation, graphene.ObjectType):
    pass


root_schema = graphene.Schema(query=Query, mutation=Mutation)

还有网址:

urlpatterns = (
    url(r"^graphql", GraphQLView.as_view(schema=root_schema graphiql=True), name="graphql"),
)

# you can wrap csrf_exempt(GraphQLView.as_view(...)) for testing
# or you can setup the frontend `apollo-client` to use csrf_tokens
# also see the related links below

例如对于apollo-clientnot apollo-boost window.csrf_token 在模板渲染中是% csrf_token %

import  ApolloProvider  from "react-apollo";
import ApolloClient from "apollo-client";
import  HttpLink  from "apollo-link-http";
import  InMemoryCache as Cache  from "apollo-cache-inmemory";
import  ApolloLink  from "apollo-link";
import fetch from "unfetch";

const uri = UrlUtils.makeUrl(Urls.graphQl);
const AuthLink = (operation, next) => 
  const token = window.csrf_token; 

  operation.setContext(context => (
    ...context,
    headers: 
      ...context.headers,
      "X-CSRFToken": token
    
  ));

  return next(operation);
;

const link = ApolloLink.from([
  AuthLink,
  new HttpLink(
    uri,
    credentials: "same-origin",
    fetch // override fetch implementation for polyfills
  )
]);

const apollo = new ApolloClient(
  link,
  cache: new Cache().restore()
);

你应该能够:

query TestQuery() 
    resolveAllReservations () 
         id
    

或:

mutate TestMutate($input: ReservationComponentMutationInput!) 
     createReservation(input: $input) 
         id
         errors 
           field
           messages
         
     
    _debug 
      sql 
        rawSql
      
    

相关:

https://***.com/a/39026832/2026508

【讨论】:

【参考方案2】:

我看不出有什么理由必须按照该教程中的说明进行操作。通过以下方式连接 drf 和 graphql 要容易得多。这样一来,您就不必担心任何模糊的类,而只需依赖于 drf 和石墨烯的主要方面。

正常构造drf序列化器,如下图连接到graphql。

假设我们有模型主题。让我们为它创建 CRUD api。

from graphene.types.scalars import Scalar

class ObjectField(Scalar): # to serialize error message from serializer
    @staticmethod
    def serialize(dt):
        return dt 


class SubjectType(DjangoObjectType):
    class Meta:
        model=Subject


# For mutation, use serializers

#creating subject
class CreateSubject(graphene.Mutation):
    subject=graphene.Field(SubjectType)
    message=ObjectField()
    status=graphene.Int()

    class Arguments:
        name=graphene.String(required=True)
        description=graphene.String(required=True)
   
    @classmethod
    def mutate(cls,root,info,**kwargs):
        serializer=SubjectSerializer(data=kwargs)
        if serializer.is_valid():
            obj=serializer.save()
            msg='success'
        else:
            msg=serializer.errors
            obj=None
            print(msg)
        return cls(subject=obj,message=msg,status=200)


'''Updating subject'''
class UpdateSubject(graphene.Mutation):
    subject=graphene.Field(SubjectType)
    status=graphene.Int()
    message=ObjectField()

    class Arguments:
        id=graphene.ID(required=True)
        name=graphene.String()
        description=graphene.String()

    @classmethod
    def mutate(cls,root,info,id,**kwargs):
        sub=Subject.objects.get(id=id)
        serializer=SubjectSerializer(sub,data=kwargs,partial=True)
        if serializer.is_valid():
            obj=serializer.save()
            msg='success'
        else:
            msg=serializer.errors
            obj=None
            print(msg)
        return cls(subject=obj,message=msg,status=200)


'''Delete Subject'''
class DeleteSubject(graphene.Mutation):
    message=ObjectField()
    status=graphene.Int()

    class Arguments:
        id=graphene.ID(required=True)

    @classmethod
    def mutate(cls,root,info,id,**kwargs):
        c=Subject.objects.get(id=id)
        c.delete()
        return cls(message='success',status=200)


class Mutation(graphene.ObjectType):
    create_subject=CreateSubject.Field()
    update_subject=UpdateSubject.Field()
    delete_subject=DeleteSubject.Field()

# Query is normal.

class Query(graphene.ObjectType):
    subject=graphene.Field(SubjectType,id=graphene.Int(), slug=graphene.String())
    
    subjects=graphene.List(SubjectType)

    def resolve_subject(self, info, id=None, slug=None):
        if id:
            return Subject.objects.get(id=id)
        if slug:
            return  Subject.objects.get(slug=slug)

    def resolve_subjects(self,info,**kwargs):
        return Subject.objects.all()

    

您可以尝试为自己制作类似框架的小东西,以避免出现冗余代码。

【讨论】:

以上是关于如何将 DRF 序列化器与 Graphene 一起使用的主要内容,如果未能解决你的问题,请参考以下文章

如何使DRF串行器与大写字母兼容

GraphQLError 未知类型“XYZMutationInput”

如何将 ListView 构建器与提供程序一起使用?

您将如何将订阅管理器与流星的模板订阅一起使用?

如何将多图像选择器与 Flutter Image Compress 一起使用?

如何将这两个类装饰器与 functools.update_wrapper 一起使用?