Я написал схему graphql и развернул ее с помощью prisma. Prisma создала файл .graphql
с type Query
, type Mutation
, type Subscription
. Есть сервер призмы, работающий от докера, который связывается с базой данных MySQL. Теперь я хотел бы написать некоторые функции API с использованием Ariadne и связаться с базой данных с помощью запросов 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, используя запрос ниже.
{
users{
name
id
}
}
Скриншот площадки Prisma для получения всех пользователей из базы данных
Попытка сделать то же самое с 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.
Скриншот ошибки из ariadne