我正在使用graphene.django,但是我在连接graphiql时遇到了一些问题
所有文件:
project schema.py:
import graphene
import art_app.customers.schema as customer
class Query(customer.Query, graphene.ObjectType):
pass
class Mutation(customer.Mutation, graphene.ObjectType):
pass
schema = graphene.Schema(query=Query, mutation=Mutation)
app querys.py:
import graphene
from graphene import relay, ObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .types.nodes import CustomerUserNode
class CustomerUserQuery(ObjectType):
customer_user = relay.Node.Field(CustomerUserNode)
all_customer_users = DjangoFilterConnectionField(CustomerUserNode)
class Query(
CustomerUserQuery, graphene.ObjectType,
):
pass
app突变
create.py:
import graphene
from graphene import relay
from ...models import CustomerUserModel
from ..types.inputs import CustomerUserCreateInput
from ..types.nodes import CustomerUserNode
class CreateCustomerUser(relay.ClientIDMutation):
class Input:
new_customer_details = graphene.Field(CustomerUserCreateInput)
created_customer_details = graphene.Field(CustomerUserNode)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
created_customer_details = CustomerUserModel.objects.create(**input.get("new_customer_details"))
file = info.context.FILES['image']
created_customer_details.image = file
return CreateCustomer(created_customer_details=created_customer_details)
delete.py:
import graphene
from graphene import relay
from ...models import CustomerUserModel
from ..types.inputs import CustomerUserCreateInput
from ..types.nodes import CustomerUserNode
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
from graphql import GraphQLError
class DeleteCustomerUser(relay.ClientIDMutation):
class Input:
id = graphene.ID(required=True)
deleted_customer_details = graphene.Field(CustomerUserNode)
@classmethod
def mutate_and_get_payload(cls, root, info, id):
deleted_customer_details = get_object_or_404(CustomerUserModel, id=id)
if deleted_customer_details.is_active:
deleted_customer_details.is_active = False
deleted_customer_details.save()
else:
raise GraphQLError("This user is not active!")
return DeleteCustomerUser(deleted_customer_details=deleted_customer_details)
nodes.py:
from graphene import relay
from graphene_django import DjangoObjectType
from ...models import CustomerUserModel
class CustomerUserNode(DjangoObjectType):
class Meta:
model = CustomerUserModel
filter_fields = []
interfaces = (relay.Node, )
输入.py
import graphene
class CustomerUserCreateInput(graphene.InputObjectType):
first_name = graphene.String(required=True)
last_name = graphene.String(required=True)
email = graphene.String(required=True)
shipping_address = graphene.String()
invoice_address = graphene.String()
phone_number = graphene.String()
other_notes = graphene.String()
settings.py:
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'graphene_django',
'art_app',
'django_cleanup.apps.CleanupConfig',
'djmoney',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
]
GRAPHENE = {
'SCHEMA': 'art_project.schema.schema'
}
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
...
文件:
[packages]
django = "*"
django-cors-headers = "*"
graphene-django = "*"
graphene = ">=2.0"
pillow = "*"
django-cleanup = "*"
django-filter = "==2.1.0"
django-moneyfield = "*"
django-money = "*"
pytest = "*"
[requires]
python_version = "3.6"
我一直在寻找问题,但我没有发现
你能帮我解决什么问题吗
目前没有回答
相关问题 更多 >
编程相关推荐