0

I have written some graphql schema and deployed it using prisma. Prisma generated some .graphql file with type Query, type Mutation, type Subscription. There is a prisma server running from docker which is contacting MySQL database. Now I would like to write some API functions using Ariadne and contact the database using Prisma queries. How can I achieve this?

GraphQL Schema provided to prisma

datamodel.prisma

type User {
  id: ID! @id 
  name: String!
}

Example of generated graphql file

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
}

Code snippet of API using ariadne trying to connect to database

I am trying to execute users query i.e., getting all users from the database.

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)

Running the server using uvicorn uvicorn api:app --reload --port 7000

I am able to get all the users in prisma playground using below query.

{
  users{
    name
    id

  }
}

Screenshot of prisma playground for getting all users from database

Trying the same with ariadne resolve_users resolver is not working.

Giving me below error:

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

patrys
  • 2,729
  • 17
  • 27
Manisha Bayya
  • 137
  • 1
  • 9
  • I think you should return `User` objects in resolver. – Zorig Jul 02 '19 at 11:18
  • Did you fetch the data from your prisma server? I perceived that you are trying to substitute prisma server using python+ariadne right? but using generated type definitions from prisma? – Zorig Jul 02 '19 at 11:26
  • yes @Zorig. I am using Ariadne to contact prisma server. As prisma didn't support python client yet and I didn't get any answer in stack overflow, as a workaround I am using requests module to send request to prisma server's endpoint from my Ariadne resolver. – Manisha Bayya Jul 08 '19 at 06:28
  • Why not using ariadne + some db or plain Django+ariadne ? Instead of using Prisma? For that case using ariadne is the sole purpose. Now using Prisma and using ariadne to is kind of sound strange – Zorig Jul 08 '19 at 07:15
  • Because prisma supports filtering, pagination, and subscriptions out of the box without me writing code for that again. – Manisha Bayya Jul 08 '19 at 13:21
  • Oh, i see, well for me i wrote pagination and filtering all by myself currently. @patrys is there any intention to make ariadne more like bundle tool such as `graphene-django` ? – Zorig Jul 09 '19 at 03:15

1 Answers1

0

As prisma doesn't support client for python yet, below code helps as a workaround for contacting prisma server from Ariadne.

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) 
Manisha Bayya
  • 137
  • 1
  • 9