我正在研究开发GraphQL API.我有一个django/elasticsearch/mysql后端,我正在弄清楚GraphQL如何适应这张图片.
我在阅读关于graphene-django项目的文章,但它似乎与Django ORM紧密结合,所以我想知道弹性搜索是否适合这个配方.
我刚刚开始这项研究,所以即使这个问题也没有意义.
关于如何做到这一点的任何线索?
我已经使用Django设置了Graphene服务器。当我通过GraphiQL(Web客户端)运行查询时,一切正常。但是,当我从其他任何地方运行时,都会出现错误:“必须提供查询字符串”。
我做了一些故障排除。GraphiQL使用来将POST数据发送到GraphQL服务器Content-Type: application/json。这是我从Chrome“网络”标签复制的GraphiQL请求的正文:
{"query":"query PartnersQuery {\n partners{\n name\n url\n logo\n }\n}","variables":"null","operationName":"PartnersQuery"}
Run Code Online (Sandbox Code Playgroud)
当我使用将其复制到Postman时Content-Type: application/json,得到以下响应:
{
"errors": [
{
"message": "Must provide query string."
}
]
}
Run Code Online (Sandbox Code Playgroud)
造成此问题的原因是什么?我没有对该架构做任何疯狂的事情。只是遵循了石墨烯文档中的教程。还有什么会导致这样的问题?
假设一个模型翻译了如下字段,我们如何使用 django-graphene 查询这些字段?
from parler.models import TranslatableModel, TranslatedFields
class Article(TranslatableModel):
#regular fields
publishing_date = models.DateTimeField(_('publishing date'),
default=now)
# translated fields
translations = TranslatedFields(
title=models.CharField(_('title'), max_length=234),
slug=models.SlugField(
verbose_name=_('slug'),
max_length=255,
db_index=True,
blank=True,
),
meta_title=models.CharField(
max_length=255, verbose_name=_('meta title'),
blank=True, default=''),
meta_description=models.TextField(
verbose_name=_('meta description'), blank=True, default=''),
meta_keywords=models.TextField(
verbose_name=_('meta keywords'), blank=True, default=''),
)
Run Code Online (Sandbox Code Playgroud)
为了注册“未知”字段,我执行以下操作:
@convert_django_field.register(GeopositionField)
def convert_geofield_to_string(field, registry=None):
return graphene.String(description=field.help_text, required=not field.null)
Run Code Online (Sandbox Code Playgroud)
……但这在这里行不通。有任何想法吗?
我想将状态字段添加到错误响应中,所以要这样做:
{
"errors": [
{
"message": "Authentication credentials were not provided",
"locations": [
{
"line": 2,
"column": 3
}
]
}
],
"data": {
"viewer": null
}
}
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
{
"errors": [
{
"status": 401, # or 400 or 403 or whatever error status suits
"message": "Authentication credentials were not provided",
"locations": [
{
"line": 2,
"column": 3
}
]
}
],
"data": {
"viewer": null
}
}
Run Code Online (Sandbox Code Playgroud)
我发现只能通过在resolver:中引发Exception来更改消息raise Error('custom error message'),但是如何添加字段?
代码示例:
class Query(UsersQuery, graphene.ObjectType):
me = …Run Code Online (Sandbox Code Playgroud) 我正在尝试将带有graphql突变的图像上传到我的服务器,如何用insomnia.rest测试它?Graphql查询的结构化请求不显示任何字段来添加文件。另外,如果失眠是不可能的,我还可以使用其他替代方法来测试这种情况吗?
我有以下情况:我有一个用户,每个用户都有一个库存。我正在努力在 Mutation“CreateUser”中声明用户的库存。以下是用于创建用户的以下更改:
mutation Create{
addUser(UserData:{name:"Shibunika",age:21}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试在此突变中声明用户的库存,我期望类似
mutation Create{
addUser(UserData:{name:"Shibunika",age:21,inventory:{'item1':45,'item2':25}
}s
Run Code Online (Sandbox Code Playgroud)
这些数字是每个项目的数量。我如何在石墨烯中定义这些输入?你会轻轻地给我看一个模式吗?
我正在尝试组合位于 Django 2.1 中不同应用程序中的多个查询模式。使用 graphene-django 2.2(已经尝试过 2.1 有同样的问题)。蟒蛇 3.7。
Query 类只注册第一个变量。例如 shop.schema.Query。
import graphene
import graphql_jwt
from django.conf import settings
import about.schema
import shop.schema
import landingpage.schema
class Query(about.schema.Query, shop.schema.Query, landingpage.schema.Query, graphene.ObjectType):
pass
class Mutation(shop.schema.Mutation, graphene.ObjectType):
token_auth = graphql_jwt.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.Verify.Field()
refresh_token = graphql_jwt.Refresh.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
Run Code Online (Sandbox Code Playgroud)
为什么会这样?python 3.7中的类有什么改变吗?石墨烯教程说这将继承多个......
class Query(cookbook.ingredients.schema.Query, graphene.ObjectType):
# This class will inherit from multiple Queries
# as we begin to add more apps to our project
pass
schema = graphene.Schema(query=Query)
Run Code Online (Sandbox Code Playgroud)
我正在将我的架构导出到 schema.json 以便将它与反应中继一起使用。我确实从登陆页面(3. 变量)中找到了我的对象“集合”查询模式。继电器返回: …
我正在努力让我的删除操作工作。我的创建、读取和更新工作正常,但删除没有任何回报。
class DeleteEmployeeInput(graphene.InputObjectType):
"""Arguments to delete an employee."""
id = graphene.ID(required=True, description="Global Id of the employee.")
class DeleteEmployee(graphene.Mutation):
"""Delete an employee."""
employee = graphene.Field(
lambda: Employee, description="Employee deleted by this mutation.")
class Arguments:
input = DeleteEmployeeInput(required=True)
def mutate(self, info, input):
data = utils.input_to_dictionary(input)
#data['edited'] = datetime.utcnow()
employee = db_session.query(
EmployeeModel).filter_by(id=data['id'])
employee.delete(data['id'])
db_session.commit()
#employee = db_session.query(
#EmployeeModel).filter_by(id=data['id']).first()
#return DeleteEmployee(employee=employee)
Run Code Online (Sandbox Code Playgroud)
删除条目的最佳方法是什么?我假设我必须返回 OK 或 Error。
当我运行我的突变时:
mutation {
deleteEmployee (input: {
id: "RW1wbG95ZWU6MQ=="
})
}
Run Code Online (Sandbox Code Playgroud)
我收到错误 Field \"deleteEmployee\" of type \"DeleteEmployee\" must have …
我在python(Flask + Graphene)中有一个后端服务器,我需要返回一个这样的JSON对象:
{
's1': "Section 1",
's2': "Section 2",
's3': "Section 3",
's4': "Section 4"
}
Run Code Online (Sandbox Code Playgroud)
解析器如下所示:
questionnaire = graphene.types.json.JSONString(
description='JSON result test')
def resolve_questionnaire(self, info: graphql.ResolveInfo):
sections = {
's1': "Section 1",
's2': "Section 2",
's3': "Section 3",
's4': "Section 4"
}
print(json.dumps(sections))
return sections
Run Code Online (Sandbox Code Playgroud)
在控制台中我看到了print(json.dumps(sections))我期望的结果:
user-api_1 | {"s1": "Section 1", "s2": "Section 2", "s3": "Section 3", "s4": "Section 4"}
Run Code Online (Sandbox Code Playgroud)
当我更改为时return sections,return json.dumps(sections)我得到如下结果:

问题是如何在石墨烯解析器中正确返回JSON对象? 我知道有像这里使用的json.replace方法,但我相信我只是以错误的方式生成/传递对象.
我正在尝试设置 Graphene,但在浏览器中打开 http://localhost:8000/graphql/ 时出现以下异常:
TemplateDoesNotExist at /graphql/
graphene/graphiql.html
Request Method: GET
Request URL: http://localhost:8000/graphql/
Django Version: 3.2.10
Run Code Online (Sandbox Code Playgroud)
添加了整个设置、添加到 URL、配置架构、查询和突变。但还是不行。甚至不记得曾经需要为石墨烯配置模板。
graphene-python ×10
django ×6
graphql ×5
python ×2
graphiql ×1
json ×1
python-3.x ×1
react-relay ×1
reactjs ×1