尝试通过graphql连接react admin和django问题是什么?

问题描述

反应设置来自https://www.npmjs.com/package/ra-data-graphql-simple

  _addGeoPoint(LatLng myloc) {
    GeoFirePoint myLocation =
        geo.point(latitude: myloc.latitude,longitude: myloc.longitude);
    firestore
        .collection('locations')
        .add({'busName': 'WP9885','routeNo':'155','position': myLocation.data});
  }

Django设置:

//在项目/app/schema.py中

// in App.js
import * as React from 'react';
import { Component } from 'react';
import buildGraphQLProvider from 'ra-data-graphql-simple';
import { Admin,Resource,ListGuesser } from 'react-admin';
import { CountryList } from './countries';


class App extends Component {
    constructor() {
        super();
        this.state = { dataProvider: null };
    }
    componentDidMount() {
        buildGraphQLProvider({ clientOptions: { uri: 'http://localhost:8000/graphql/' }})
            .then(dataProvider => this.setState({ dataProvider }));
    }

    render() {
        const { dataProvider } = this.state;

        if (!dataProvider) {
            return <div>Loading</div>;
        }

        return (
            <Admin dataProvider={dataProvider}>
                <Resource name="countries" list={ListGuesser} />
            </Admin>
        );
    }
}

export default App;

// Djago设置

import graphene
from graphene_django.types import DjangoObjectType
from .models import Organization,Project,Country


class CountryType(DjangoObjectType):
    class Meta:
        model = Country
        fields = ("id","title")


class CreateCountry(graphene.Mutation):
    id = graphene.Int()
    title = graphene.String()

    class Arguments:
        title = graphene.String()

    def mutate(self,info,title):
        country = Country(title=title)
        country.save()

        return CreateCountry(
            id=country.id,title=country.title,)


class InforgMutation(graphene.ObjectType):
    create_country = CreateCountry.Field()


class InforgQuery:
    countries = graphene.List(CountryType)
    country = graphene.Field(CountryType,pk=graphene.Int())

    def resolve_countries(self,**kwargs):
        print(info)
        return Country.objects.all()
    
    def resolve_country(self,**kwargs):
        if 'pk' in kwargs:
            return Country.objects.get(pk=kwargs['pk'])
        return None

//在项目中/main_schema.py

GRAPHENE = {
    'SCHEMA': 'config.schema.schema',}

//后端日志

import graphene
from app.schema import InforgQuery,InforgMutation


class Query(InforgQuery,graphene.ObjectType):
    pass


schema = graphene.Schema(query=Query,mutation=InforgMutation)

//反应日志

[28/Aug/2020 08:14:51] "POST /graphql/ HTTP/1.1" 200 31678
^[[BPOST /graphql/
{'HTTP_HOST': 'localhost:8000','HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0','HTTP_ACCEPT': '*/*','HTTP_ACCEPT_LANGUAGE': 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3','HTTP_ACCEPT_ENCODING': 'gzip,deflate','HTTP_REFERER': 'http://localhost:3000/','HTTP_ORIGIN': 'http://localhost:3000','HTTP_CONNECTION': 'keep-alive'}
b'{"operationName":"IntrospectionQuery","variables":{},"query":"query IntrospectionQuery {\\n  __schema {\\n    queryType {\\n      name\\n      __typename\\n    }\\n    mutationType {\\n      name\\n      __typename\\n    }\\n    subscriptionType {\\n      name\\n      __typename\\n    }\\n    types {\\n      ...FullType\\n      __typename\\n    }\\n    directives {\\n      name\\n      description\\n      locations\\n      args {\\n        ...InputValue\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n}\\n\\nfragment FullType on __Type {\\n  kind\\n  name\\n  description\\n  fields(includeDeprecated: true) {\\n    name\\n    description\\n    args {\\n      ...InputValue\\n      __typename\\n    }\\n    type {\\n      ...TypeRef\\n      __typename\\n    }\\n    isDeprecated\\n    deprecationReason\\n    __typename\\n  }\\n  inputFields {\\n    ...InputValue\\n    __typename\\n  }\\n  interfaces {\\n    ...TypeRef\\n    __typename\\n  }\\n  enumValues(includeDeprecated: true) {\\n    name\\n    description\\n    isDeprecated\\n    deprecationReason\\n    __typename\\n  }\\n  possibleTypes {\\n    ...TypeRef\\n    __typename\\n  }\\n  __typename\\n}\\n\\nfragment InputValue on __InputValue {\\n  name\\n  description\\n  type {\\n    ...TypeRef\\n    __typename\\n  }\\n  defaultValue\\n  __typename\\n}\\n\\nfragment TypeRef on __Type {\\n  kind\\n  name\\n  ofType {\\n    kind\\n    name\\n    ofType {\\n      kind\\n      name\\n      ofType {\\n        kind\\n        name\\n        ofType {\\n          kind\\n          name\\n          ofType {\\n            kind\\n            name\\n            ofType {\\n              kind\\n              name\\n              ofType {\\n                kind\\n                name\\n                __typename\\n              }\\n              __typename\\n            }\\n            __typename\\n          }\\n          __typename\\n        }\\n        __typename\\n      }\\n      __typename\\n    }\\n    __typename\\n  }\\n  __typename\\n}\\n"}'
POST /graphql/ - 200
{'content-type': ('Content-Type','application/json'),'vary': ('Vary','Cookie')}
b'{"data":{"__schema":{"queryType":{"name":"Query","__typename":"__Type"},"mutationType":{"name":"InforgMutation","subscriptionType":null,"types":[{"kind":"OBJECT","name":"Query","description":null,"fields":[{"name":"countries","args":[],"type":{"kind":"LIST","name":null,"ofType":{"kind":"OBJECT","name":"CountryType","ofType":null,"isDeprecated":false,"deprecationReason":null,"__typename":"__Field"},{"name":"country","args":[{"name":"pk","type":{"kind":"SCALAR","name":"Int","defaultValue":null,"__typename":"__InputValue"}],"type":{"kind":"OBJECT",{"name":"projects","name":"ProjectType",{"name":"project","d

据我了解,问题出在响应Django请求的架构中。如何标准化前端和后端通信?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)