Commit 1ced7301 authored by Eelco van der Wel's avatar Eelco van der Wel :speech_balloon:
Browse files

add test

parent a744e592
Showing with 334 additions and 2 deletions
+334 -2
# Dgraph Test
%% Cell type:code id:3f5fe9e5 tags:
``` python
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
from pprint import pprint
import datetime
import requests
```
%% Cell type:markdown id:1660935b tags:
### Setup
`
docker run -it -p 5080:5080 -p 6080:6080 -p 8080:8080 -p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph --name dgraph dgraph/standalone:v21.03.0
`
----
#### Add schema
`
curl -X POST localhost:8080/admin/schema --data-binary '@schema.graphql'
`
### Drop all and schema
`
curl -X POST localhost:8080/alter -d '{"drop_all": true}'
`
%% Cell type:code id:626c8c7e tags:
``` python
# Add schema
!curl -X POST localhost:8080/admin/schema --data-binary '@schema.graphql'
```
%% Output
{"data":{"code":"Success","message":"Done"}}
%% Cell type:code id:1e5133f0 tags:
``` python
gql_url = "http://localhost:8080/graphql"
transport = RequestsHTTPTransport(url=gql_url)
client = Client(transport=transport, fetch_schema_from_transport=True)
```
%% Cell type:code id:023b399a tags:
``` python
add_accounts = gql("""
mutation addAccount($authors: [AddAccountInput!]!){
addAccount(input: $authors) {
account {
id
}
}
}
""")
add_messages = gql("""
mutation addMessage($messages: [AddMessageInput!]!){
addMessage(input: $messages) {
message {
id
}
}
}
""")
```
%% Cell type:code id:9db0951f tags:
``` python
accounts = [{
"handle": f"name_{i}",
"service": "whatsapp",
"isMe": False
} for i in range(3)
]
params = {"authors": accounts}
pprint(params)
```
%% Output
{'authors': [{'handle': 'name_0', 'isMe': False, 'service': 'whatsapp'},
{'handle': 'name_1', 'isMe': False, 'service': 'whatsapp'},
{'handle': 'name_2', 'isMe': False, 'service': 'whatsapp'}]}
%% Cell type:code id:cdfc463a tags:
``` python
result = client.execute(add_accounts, variable_values=params)
result
```
%% Output
{'addAccount': {'account': [{'id': '0x39'}, {'id': '0x3a'}, {'id': '0x3b'}]}}
%% Cell type:code id:95dbf024 tags:
``` python
messages = [{
"content": lorem.sentence(),
"dateSent": datetime.datetime(1980, 1, 1, 23, 0, 0, 0).isoformat(),
"service": "whatsapp",
"author": {"id": a["id"]}
} for a in result["addAccount"]["account"]]
pprint(messages)
```
%% Output
[{'author': {'id': '0x2f'},
'content': 'Ipsum dolorem ipsum magnam etincidunt etincidunt.',
'dateSent': '1980-01-01T23:00:00',
'service': 'whatsapp'},
{'author': {'id': '0x30'},
'content': 'Aliquam neque quisquam velit labore amet etincidunt.',
'dateSent': '1980-01-01T23:00:00',
'service': 'whatsapp'},
{'author': {'id': '0x31'},
'content': 'Dolore dolore ut aliquam etincidunt non.',
'dateSent': '1980-01-01T23:00:00',
'service': 'whatsapp'}]
%% Cell type:code id:fb176a73 tags:
``` python
result = client.execute(
add_messages,
variable_values={
"messages": messages
})
result
```
%% Output
{'addMessage': {'message': [{'id': '0x32'}, {'id': '0x33'}, {'id': '0x34'}]}}
%% Cell type:code id:6576a8d8 tags:
``` python
query = gql("""
query {
queryMessage {
id
content
author (filter: {
handle: {alloftext: "name_1"}
}) {
id
handle
}
}
}
""")
result = client.execute(query)
pprint(result)
```
%% Output
{'queryMessage': [{'author': None,
'content': 'Ipsum dolorem ipsum magnam etincidunt '
'etincidunt.',
'id': '0x32'},
{'author': {'handle': 'name_1', 'id': '0x30'},
'content': 'Aliquam neque quisquam velit labore amet '
'etincidunt.',
'id': '0x33'},
{'author': None,
'content': 'Dolore dolore ut aliquam etincidunt non.',
'id': '0x34'}]}
%% Cell type:code id:ea00ac58 tags:
``` python
query = gql("""
query {
queryMessage (filter:
{account: {handle: {alloftext: "name_1"}}}
) {
id
content
author {
id
handle
}
}
}
""")
result = client.execute(query)
pprint(result)
```
%% Output
---------------------------------------------------------------------------
GraphQLError Traceback (most recent call last)
/tmp/ipykernel_14455/610262275.py in <module>
14 """)
15
---> 16 result = client.execute(query)
17 pprint(result)
~/miniconda3/lib/python3.9/site-packages/gql/client.py in execute(self, document, variable_values, operation_name, serialize_variables, parse_result, get_execution_result, **kwargs)
368
369 else: # Sync transports
--> 370 return self.execute_sync(
371 document,
372 variable_values=variable_values,
~/miniconda3/lib/python3.9/site-packages/gql/client.py in execute_sync(self, document, variable_values, operation_name, serialize_variables, parse_result, get_execution_result, **kwargs)
186 """:meta private:"""
187 with self as session:
--> 188 return session.execute(
189 document,
190 variable_values=variable_values,
~/miniconda3/lib/python3.9/site-packages/gql/client.py in execute(self, document, variable_values, operation_name, serialize_variables, parse_result, get_execution_result, **kwargs)
770
771 # Validate and execute on the transport
--> 772 result = self._execute(
773 document,
774 variable_values=variable_values,
~/miniconda3/lib/python3.9/site-packages/gql/client.py in _execute(self, document, variable_values, operation_name, serialize_variables, parse_result, **kwargs)
665 # Validate document
666 if self.client.schema:
--> 667 self.client.validate(document)
668
669 # Parse variable values for custom scalars if requested
~/miniconda3/lib/python3.9/site-packages/gql/client.py in validate(self, document)
130 validation_errors = validate(self.schema, document)
131 if validation_errors:
--> 132 raise validation_errors[0]
133
134 @overload
GraphQLError: Field 'account' is not defined by type 'MessageFilter'.
GraphQL request:4:8
3 | queryMessage (filter:
4 | {account: {handle: {alloftext: "name_1"}}}
| ^
5 | ) {
type Account {
id: ID!
handle: String! @search(by: [term, fulltext])
service: String! @search(by: [term, fulltext])
isMe: Boolean!
}
type Message {
id: ID!
content: String! @search(by: [term, fulltext])
dateSent: DateTime!
service: String! @search(by: [term, fulltext])
author: Account
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment