将 prisma graphql 查询与 ariadne 集成
Integrate prisma graphql queries with ariadne
我已经编写了一些 graphql 模式并使用 prisma 部署了它。 Prisma 使用 type Query
、type Mutation
、type Subscription
生成了一些 .graphql
文件。来自 docker 的 prisma 服务器 运行 正在联系 MySQL 数据库。现在我想使用 Ariadne 编写一些 API 函数并使用 Prisma 查询联系数据库。我怎样才能做到这一点?
提供给 prisma 的 GraphQL 模式
datamodel.prisma
type User {
id: ID! @id
name: String!
}
生成的graphql文件示例
prisma.graphql
type Query {
user(where: UserWhereUniqueInput!): User
users(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [User]!
usersConnection(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): UserConnection!
node(id: ID!): Node
}
API 的代码片段使用 ariadne 尝试连接到数据库
我正在尝试执行 users
查询,即从数据库中获取所有用户。
api.py
from ariadne import gql, load_schema_from_path, QueryType, make_executable_schema
from ariadne.asgi import GraphQL
schema_files_path = "/root/manisha/prisma/generated/prisma.graphql"
schema = load_schema_from_path(schema_files_path)
query = QueryType()
@query.field("users")
def resolve_users(_, info):
...
schema = make_executable_schema(schema, query)
app = GraphQL(schema, debug=True)
运行服务器使用uvicorn
uvicorn api:app --reload --port 7000
我可以使用以下查询获取 prisma playground 中的所有用户。
{
users{
name
id
}
}
Screenshot of prisma playground for getting all users from database
对 ariadne resolve_users
解析器进行同样的尝试无效。
给我以下错误:
ERROR: Expected Iterable, but did not find one for field Query.users.
GraphQL request (2:3)
1: {
2: users {
^
3: id
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 675, in complete_value_catching_error
return_type, field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 750, in complete_value
result,
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 766, in complete_value
cast(GraphQLList, return_type), field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 807, in complete_list_value
"Expected Iterable, but did not find one for field"
TypeError: Expected Iterable, but did not find one for field Query.users.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 351, in execute_operation
)(type_, root_value, path, fields)
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 441, in execute_fields
parent_type, source_value, field_nodes, field_path
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 612, in resolve_field
field_def.type, field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 688, in complete_value_catching_error
self.handle_field_error(error, field_nodes, path, return_type)
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 703, in handle_field_error
raise error
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 675, in complete_value_catching_error
return_type, field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 750, in complete_value
result,
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 766, in complete_value
cast(GraphQLList, return_type), field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 807, in complete_list_value
"Expected Iterable, but did not find one for field"
graphql.error.graphql_error.GraphQLError: Expected Iterable, but did not find one for field Query.users.
Screenshot of error from ariadne
由于 prisma 尚不支持 python 的客户端,下面的代码有助于作为从 Ariadne 联系 prisma 服务器的解决方法。
from ariadne import gql, load_schema_from_path, QueryType, make_executable_schema
from ariadne.asgi import GraphQL
import requests
prisma_url = "your-prisma-endpoint"
schema_files_path = "/root/manisha/prisma/generated/prisma.graphql"
schema = load_schema_from_path(schema_files_path)
query = QueryType()
def make_call_to_prisma(info):
data = info.context["request"]._body
resp = requests.post(
url=prisma_url, headers={"content-type": "application/json"}, data=data
)
return resp
@query.field("users")
def resolve_users(_, info, where=None):
result = make_call_to_prisma(info)
print(result.json())
return result.json()["data"]["users"]
schema = make_executable_schema(schema, query)
app = GraphQL(schema, debug=True)
我已经编写了一些 graphql 模式并使用 prisma 部署了它。 Prisma 使用 type Query
、type Mutation
、type Subscription
生成了一些 .graphql
文件。来自 docker 的 prisma 服务器 运行 正在联系 MySQL 数据库。现在我想使用 Ariadne 编写一些 API 函数并使用 Prisma 查询联系数据库。我怎样才能做到这一点?
提供给 prisma 的 GraphQL 模式
datamodel.prisma
type User {
id: ID! @id
name: String!
}
生成的graphql文件示例
prisma.graphql
type Query {
user(where: UserWhereUniqueInput!): User
users(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [User]!
usersConnection(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): UserConnection!
node(id: ID!): Node
}
API 的代码片段使用 ariadne 尝试连接到数据库
我正在尝试执行 users
查询,即从数据库中获取所有用户。
api.py
from ariadne import gql, load_schema_from_path, QueryType, make_executable_schema
from ariadne.asgi import GraphQL
schema_files_path = "/root/manisha/prisma/generated/prisma.graphql"
schema = load_schema_from_path(schema_files_path)
query = QueryType()
@query.field("users")
def resolve_users(_, info):
...
schema = make_executable_schema(schema, query)
app = GraphQL(schema, debug=True)
运行服务器使用uvicorn
uvicorn api:app --reload --port 7000
我可以使用以下查询获取 prisma playground 中的所有用户。
{
users{
name
id
}
}
Screenshot of prisma playground for getting all users from database
对 ariadne resolve_users
解析器进行同样的尝试无效。
给我以下错误:
ERROR: Expected Iterable, but did not find one for field Query.users.
GraphQL request (2:3)
1: {
2: users {
^
3: id
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 675, in complete_value_catching_error
return_type, field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 750, in complete_value
result,
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 766, in complete_value
cast(GraphQLList, return_type), field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 807, in complete_list_value
"Expected Iterable, but did not find one for field"
TypeError: Expected Iterable, but did not find one for field Query.users.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 351, in execute_operation
)(type_, root_value, path, fields)
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 441, in execute_fields
parent_type, source_value, field_nodes, field_path
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 612, in resolve_field
field_def.type, field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 688, in complete_value_catching_error
self.handle_field_error(error, field_nodes, path, return_type)
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 703, in handle_field_error
raise error
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 675, in complete_value_catching_error
return_type, field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 750, in complete_value
result,
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 766, in complete_value
cast(GraphQLList, return_type), field_nodes, info, path, result
File "/usr/local/lib/python3.6/dist-packages/graphql/execution/execute.py", line 807, in complete_list_value
"Expected Iterable, but did not find one for field"
graphql.error.graphql_error.GraphQLError: Expected Iterable, but did not find one for field Query.users.
Screenshot of error from ariadne
由于 prisma 尚不支持 python 的客户端,下面的代码有助于作为从 Ariadne 联系 prisma 服务器的解决方法。
from ariadne import gql, load_schema_from_path, QueryType, make_executable_schema
from ariadne.asgi import GraphQL
import requests
prisma_url = "your-prisma-endpoint"
schema_files_path = "/root/manisha/prisma/generated/prisma.graphql"
schema = load_schema_from_path(schema_files_path)
query = QueryType()
def make_call_to_prisma(info):
data = info.context["request"]._body
resp = requests.post(
url=prisma_url, headers={"content-type": "application/json"}, data=data
)
return resp
@query.field("users")
def resolve_users(_, info, where=None):
result = make_call_to_prisma(info)
print(result.json())
return result.json()["data"]["users"]
schema = make_executable_schema(schema, query)
app = GraphQL(schema, debug=True)