Skip to content

Commit 76c5ba4

Browse files
authored
Merge pull request #27 from qlient-org/develop
Release 0.2.0-beta
2 parents 7e5be93 + 7f8feb6 commit 76c5ba4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1939
-1157
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from qlient import Fields, Field, Directive
2+
3+
my_home_world_selection = Fields(
4+
"id",
5+
"name",
6+
"population"
7+
)
8+
9+
my_person_selection = Fields(
10+
"id", # will be converted to Field("id")
11+
Field("name"),
12+
Field("height", _alias="my_height"),
13+
Field("homeworld", _sub_fields=my_home_world_selection, _directive=Directive("include", **{"if": True}))
14+
)
15+
16+
# Every variable name used in a Field or Directive is automatically generated and therefore unique.
17+
# {
18+
# id
19+
# name
20+
# my_height: height
21+
# homeworld @include(if: $include_123456789_if) {
22+
# id
23+
# name
24+
# population
25+
# }
26+
# }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from qlient import Fields, Field
2+
3+
name_fields = Fields("first_name", "last_name")
4+
age_field = Field("age")
5+
6+
person_fields = name_fields + age_field
7+
8+
# { first_name last_name age }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from qlient import Client
2+
3+
my_client = Client("https://...")
4+
5+
my_query = """
6+
query {
7+
user1: user(id: "1234") {
8+
...userInformation
9+
}
10+
user2: user(id: "5678") {
11+
...userInformation
12+
}
13+
}
14+
15+
fragment userInformation on user {
16+
username
17+
firstname
18+
lastname
19+
}
20+
"""
21+
22+
response = my_client.query(query=my_query)

docs/examples/simple_proxy_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
client = Client("https://my-graphql-api.com/")
44

5-
# query is a QueryService object (Inherits from OperationProxy). It will check if there
5+
# query is a QueryServiceProxy object (Inherits from OperationServiceProxy). It will check if there
66
# is an operation with the name `X` defined in the binding
7-
# and if that is the case it will return a callable Operation object
7+
# and if that is the case it will return a callable OperationProxy object
88
client.query.X()
99

1010
# The operation can also be called via an __getitem__ call.

docs/examples/usage_client_simple.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/examples/using_custom_backends.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,29 @@ class Book:
2020
author: str
2121

2222

23-
def get_books():
24-
return [Book(title='The Great Gatsby', author='F. Scott Fitzgerald', )]
23+
my_books = [
24+
Book(title="The Great Gatsby", author="F. Scott Fitzgerald")
25+
]
2526

2627

2728
@strawberry.type
2829
class Query:
29-
books: List[Book] = strawberry.field(resolver=get_books)
30+
@strawberry.field
31+
def get_books(self) -> List[Book]:
32+
return my_books
33+
34+
35+
@strawberry.type
36+
class Mutation:
37+
@strawberry.mutation
38+
def add_book(self, title: str, author: str) -> Book:
39+
book = Book(title=title, author=author)
40+
my_books.append(book)
41+
return book
3042

3143

3244
# this line creates the strawberry schema
33-
my_book_schema = strawberry.Schema(query=Query)
45+
my_book_schema = strawberry.Schema(query=Query, mutation=Mutation)
3446

3547

3648
# now this is the important part
@@ -67,6 +79,34 @@ def cache_key(self) -> str:
6779

6880
client = Client(StrawberryBackend(my_book_schema))
6981

70-
response: GraphQLResponse = client.query.books(_fields=["title", "author"])
82+
# note that the operation is now named "getBooks"
83+
# instead of the actual method "get_books"
84+
# This is due to the automatic camel case conversation in strawberry
85+
# https://strawberry.rocks/docs/types/schema-configurations
86+
response: GraphQLResponse = client.query.getBooks(_fields=["title", "author"])
87+
88+
print(response.data) # {"getBooks": [{"title": "The Great Gatsby", "author": "F. Scott Fitzgerald"}]}
89+
90+
response: GraphQLResponse = client.mutation.addBook(
91+
title="1984",
92+
author="George Orwell",
93+
_fields=["title", "author"]
94+
)
7195

72-
print(response.data) # {'books': [{'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald'}]}
96+
print(response.data)
97+
# {
98+
# "addBook": {
99+
# "title": "1984",
100+
# "author": "George Orwell"
101+
# }
102+
# }
103+
104+
response: GraphQLResponse = client.query.getBooks(_fields=["title", "author"])
105+
106+
print(response.data)
107+
# {
108+
# "getBooks": [
109+
# {"title": "The Great Gatsby", "author": "F. Scott Fitzgerald"},
110+
# {"title": "1984", "author": "George Orwell"}
111+
# ]
112+
# }

docs/examples/using_field_class.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from qlient import Field, Directive
2+
3+
my_field = Field(
4+
# Mandatory, the name of the field
5+
"repository",
6+
7+
# Schema Field specific inputs
8+
# say we want the last 5 repositories where the name matches abc
9+
last=5,
10+
name="*abc*",
11+
12+
# Qlient Field specific arguments start with a `_`
13+
# These arguments are Optional and can be left empty
14+
# Optional, the alias of the field
15+
_alias="my_repo",
16+
# Optional, a field directive
17+
_directive=Directive("include", **{"if": True}),
18+
)
19+
20+
# This results in a field like so:
21+
22+
# {
23+
# my_repo: repository(last: $repository_1234_last name: $repository_1234_name) @include(if: $include_5678_if)
24+
# }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from qlient import Fields
2+
3+
nested_fields = Fields(
4+
"first_name", # will be converted to Field("first_name")
5+
"last_name", # will be converted to Field("first_name")
6+
7+
# This will be converted to
8+
# Field(
9+
# "hobby",
10+
# _sub_fields=Fields(
11+
# Field("name"),
12+
# Field("club", _sub_fields=Fields(Field("name")))
13+
# )
14+
# )
15+
hobby=Fields(
16+
"name",
17+
club="name"
18+
)
19+
)
20+
21+
# last_name first_name hobby { name club { name } }

docs/usage/client.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ Say for the example above, you'd also like to have information about the rocket.
4242

4343
{% include "../examples/script_legend.md" %}
4444

45-
For more details on the `Fields` class, have a look at the [fields documentation](./fields.md)
45+
For more details on the `Fields` class, have a look at the [fields documentation](./fields.md)
46+
47+
## Sending custom queries
48+
49+
Please have a look at the [proxy documentation](./proxy.md)

docs/usage/fields.md

Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,40 @@
1-
# Fields
1+
# Field and Fields
22

3-
The `Fields` class is a powerful class for making nested or pre-configured field selections.
3+
The `Fields` class is a powerful class for making nested or pre-configured field selections. In combination with
4+
the `Field` class you can create every query you need.
45

5-
## Nested Fields selection
6+
## Using the Field Class
67

7-
```python
8-
from qlient import Fields
8+
The Field class supports all major graphql features ranging from name, alias to directives and field inputs.
99

10-
nested_fields = Fields(
11-
"first_name",
12-
"last_name",
13-
hobby="name"
14-
)
15-
16-
print(nested_fields)
17-
```
18-
19-
```text
20-
last_name first_name hobby { name }
21-
```
22-
23-
{% include "../examples/script_legend.md" %}
24-
25-
The `hobby` selection can be changed from a single item to a `list`:
26-
27-
```python
28-
from qlient import Fields
29-
30-
nested_fields = Fields(
31-
"first_name",
32-
"last_name",
33-
hobby=["name", "description"]
34-
)
35-
36-
print(nested_fields)
37-
```
38-
39-
```text
40-
last_name first_name hobby { name description }
10+
```python
11+
{% include "../examples/using_field_class.py" %}
4112
```
4213

4314
{% include "../examples/script_legend.md" %}
4415

45-
or even another `Fields` instance for even deeper selection.
46-
47-
```python
48-
from qlient import Fields
49-
50-
nested_fields = Fields(
51-
"first_name",
52-
"last_name",
53-
hobby=Fields(
54-
"name",
55-
club="name"
56-
)
57-
)
16+
## Using the Fields Class
5817

59-
print(nested_fields)
60-
```
61-
62-
```text
63-
last_name first_name hobby { name club { name } }
18+
```python
19+
{% include "../examples/using_fields_class.py" %}
6420
```
6521

6622
{% include "../examples/script_legend.md" %}
6723

6824
## Supported Operators
6925

70-
The `Fields` class supports two operators: addition and subtraction.
26+
Both, the `Field` and `Fields` class currently support the following operators:
7127

7228
### Addition
7329

74-
```python
75-
from qlient import Fields
76-
77-
name = Fields("first_name", "last_name")
78-
age = Fields("age")
79-
80-
added = name + age
81-
82-
print(added)
83-
```
84-
85-
```text
86-
first_name last_name age
30+
```python
31+
{% include "../examples/fields_addition_operator.py" %}
8732
```
8833

8934
{% include "../examples/script_legend.md" %}
9035

91-
### Subtraction
92-
93-
```python
94-
from qlient import Fields
95-
96-
full_name = Fields("first_name", "last_name")
97-
last_name = Fields("last_name")
98-
99-
subtracted = full_name - last_name
100-
101-
print(subtracted)
102-
```
103-
104-
```text
105-
first_name
106-
```
36+
## Combination of Fields and Field
10737

108-
{% include "../examples/script_legend.md" %}
38+
```python
39+
{% include "../examples/combination_of_fields_and_field.py" %}
40+
```

0 commit comments

Comments
 (0)