Compare commits

..

No commits in common. "master" and "v2.1.3" have entirely different histories.

174 changed files with 3661 additions and 7998 deletions

View File

@ -1,34 +0,0 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: "\U0001F41B bug"
assignees: ''
---
**Note: for support questions, please use stackoverflow**. This repository's issues are reserved for feature requests and bug reports.
* **What is the current behavior?**
* **If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem** via
a github repo, https://repl.it or similar.
* **What is the expected behavior?**
* **What is the motivation / use case for changing the behavior?**
* **Please tell us about your environment:**
- Version:
- Platform:
* **Other information** (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow)

View File

@ -1 +0,0 @@
blank_issues_enabled: false

View File

@ -1,20 +0,0 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: "✨ enhancement"
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.

24
.github/stale.yml vendored
View File

@ -1,24 +0,0 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: false
# Number of days of inactivity before a stale issue is closed
daysUntilClose: false
# Issues with these labels will never be considered stale
exemptLabels:
- pinned
- security
- 🐛 bug
- 📖 documentation
- 🙋 help wanted
- ✨ enhancement
- good first issue
- work in progress
# Label to use when marking an issue as stale
staleLabel: wontfix
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: false
# markComment: >
# This issue has been automatically marked as stale because it has not had
# recent activity. It will be closed if no further activity occurs. Thank you
# for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false

View File

@ -1,21 +0,0 @@
name: 📦 Build
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Building package
run: python3 -m build
- name: Check package with Twine
run: twine check dist/*

View File

@ -1,26 +0,0 @@
name: 🚀 Deploy to PyPI
on:
push:
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Build wheel and source tarball
run: |
pip install wheel
python setup.py sdist bdist_wheel
- name: Publish a Python distribution to PyPI
uses: pypa/gh-action-pypi-publish@v1.1.0
with:
user: __token__
password: ${{ secrets.pypi_password }}

View File

@ -1,26 +0,0 @@
name: 💅 Lint
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Run lint
run: tox
env:
TOXENV: pre-commit
- name: Run mypy
run: tox
env:
TOXENV: mypy

View File

@ -1,64 +0,0 @@
name: 📄 Tests
on:
push:
branches:
- master
- '*.x'
paths-ignore:
- 'docs/**'
- '*.md'
- '*.rst'
pull_request:
branches:
- master
- '*.x'
paths-ignore:
- 'docs/**'
- '*.md'
- '*.rst'
jobs:
tests:
# runs the test suite
name: ${{ matrix.name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- {name: '3.13', python: '3.13', os: ubuntu-latest, tox: py313}
- {name: '3.12', python: '3.12', os: ubuntu-latest, tox: py312}
- {name: '3.11', python: '3.11', os: ubuntu-latest, tox: py311}
- {name: '3.10', python: '3.10', os: ubuntu-latest, tox: py310}
- {name: '3.9', python: '3.9', os: ubuntu-latest, tox: py39}
- {name: '3.8', python: '3.8', os: ubuntu-latest, tox: py38}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
- name: update pip
run: |
python -m pip install --upgrade pip
pip install --upgrade setuptools wheel
- name: get pip cache dir
id: pip-cache
run: echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
- name: cache pip dependencies
uses: actions/cache@v3
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: pip|${{ runner.os }}|${{ matrix.python }}|${{ hashFiles('setup.py') }}
- run: pip install tox
- run: tox -e ${{ matrix.tox }}
- name: Upload coverage.xml
if: ${{ matrix.python == '3.10' }}
uses: actions/upload-artifact@v4
with:
name: graphene-coverage
path: coverage.xml
if-no-files-found: error
- name: Upload coverage.xml to codecov
if: ${{ matrix.python == '3.10' }}
uses: codecov/codecov-action@v4

13
.gitignore vendored
View File

@ -10,6 +10,7 @@ __pycache__/
# Distribution / packaging # Distribution / packaging
.Python .Python
env/
build/ build/
develop-eggs/ develop-eggs/
dist/ dist/
@ -44,8 +45,7 @@ htmlcov/
.pytest_cache .pytest_cache
nosetests.xml nosetests.xml
coverage.xml coverage.xml
*.cover *,cover
.pytest_cache/
# Translations # Translations
*.mo *.mo
@ -60,14 +60,6 @@ docs/_build/
# PyBuilder # PyBuilder
target/ target/
# VirtualEnv
.env
.venv
env/
venv/
# Typing
.mypy_cache/
/tests/django.sqlite /tests/django.sqlite
@ -90,4 +82,3 @@ venv/
*.sqlite3 *.sqlite3
.vscode .vscode
.mypy_cache .mypy_cache
.ruff_cache

2
.isort.cfg Normal file
View File

@ -0,0 +1,2 @@
[settings]
known_third_party = aniso8601,graphql,graphql_relay,promise,pytest,pytz,pyutils,setuptools,six,snapshottest,sphinx_graphene_theme

View File

@ -1,29 +1,24 @@
default_language_version:
python: python3.10
repos: repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: git://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0 rev: v1.3.0
hooks: hooks:
- id: check-merge-conflict
- id: check-json - id: check-json
- id: check-yaml - id: check-yaml
- id: debug-statements - id: debug-statements
- id: end-of-file-fixer - id: end-of-file-fixer
exclude: ^docs/.*$ exclude: ^docs/.*$
- id: trailing-whitespace
exclude: README.md
- id: pretty-format-json - id: pretty-format-json
args: args:
- --autofix - --autofix
- id: trailing-whitespace - id: flake8
exclude: README.md
- repo: https://github.com/asottile/pyupgrade - repo: https://github.com/asottile/pyupgrade
rev: v2.37.3 rev: v1.4.0
hooks: hooks:
- id: pyupgrade - id: pyupgrade
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/ambv/black
# Ruff version. rev: 18.6b4
rev: v0.5.0
hooks: hooks:
- id: ruff - id: black
- id: ruff-format language_version: python3.6
args: [ --check ]

33
.travis.yml Normal file
View File

@ -0,0 +1,33 @@
language: python
matrix:
include:
- env: TOXENV=py27
python: 2.7
- env: TOXENV=py34
python: 3.4
- env: TOXENV=py35
python: 3.5
- env: TOXENV=py36
python: 3.6
- env: TOXENV=pypy
python: pypy-5.7.1
- env: TOXENV=pre-commit
python: 3.6
- env: TOXENV=mypy
python: 3.6
install:
- pip install coveralls tox
script: tox
after_success: coveralls
cache:
directories:
- $HOME/.cache/pip
- $HOME/.cache/pre-commit
deploy:
provider: pypi
user: syrusakbary
on:
tags: true
password:
secure: LHOp9DvYR+70vj4YVY8+JRNCKUOfYZREEUY3+4lMUpY7Zy5QwDfgEMXG64ybREH9dFldpUqVXRj53eeU3spfudSfh8NHkgqW7qihez2AhSnRc4dK6ooNfB+kLcSoJ4nUFGxdYImABc4V1hJvflGaUkTwDNYVxJF938bPaO797IvSbuI86llwqkvuK2Vegv9q/fy9sVGaF9VZIs4JgXwR5AyDR7FBArl+S84vWww4vTFD33hoE88VR4QvFY3/71BwRtQrnCMm7AOm31P9u29yi3bpzQpiOR2rHsgrsYdm597QzFKVxYwsmf9uAx2bpbSPy2WibunLePIvOFwm8xcfwnz4/J4ONBc5PSFmUytTWpzEnxb0bfUNLuYloIS24V6OZ8BfAhiYZ1AwySeJCQDM4Vk1V8IF6trTtyx5EW/uV9jsHCZ3LFsAD7UnFRTosIgN3SAK3ZWCEk5oF2IvjecsolEfkRXB3q9EjMkkuXRUeFDH2lWJLgNE27BzY6myvZVzPmfwZUsPBlPD/6w+WLSp97Rjgr9zS3T1d4ddqFM4ZYu04f2i7a/UUQqG+itzzuX5DWLPvzuNt37JB45mB9IsvxPyXZ6SkAcLl48NGyKok1f3vQnvphkfkl4lni29woKhaau8xlsuEDrcwOoeAsVcZXiItg+l+z2SlIwM0A06EvQ=
distributions: "sdist bdist_wheel"

View File

@ -1,28 +0,0 @@
.PHONY: help
help:
@echo "Please use \`make <target>' where <target> is one of"
@grep -E '^\.PHONY: [a-zA-Z_-]+ .*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = "(: |##)"}; {printf "\033[36m%-30s\033[0m %s\n", $$2, $$3}'
.PHONY: install-dev ## Install development dependencies
install-dev:
pip install -e ".[dev]"
.PHONY: test ## Run tests
test:
py.test graphene examples
.PHONY: docs ## Generate docs
docs: install-dev
cd docs && make install && make html
.PHONY: docs-live ## Generate docs with live reloading
docs-live: install-dev
cd docs && make install && make livehtml
.PHONY: format
format:
black graphene examples setup.py
.PHONY: lint
lint:
flake8 graphene examples setup.py

View File

@ -1,47 +1,50 @@
# ![Graphene Logo](http://graphene-python.org/favicon.png) [Graphene](http://graphene-python.org) [![PyPI version](https://badge.fury.io/py/graphene.svg)](https://badge.fury.io/py/graphene) [![Coverage Status](https://coveralls.io/repos/graphql-python/graphene/badge.svg?branch=master&service=github)](https://coveralls.io/github/graphql-python/graphene?branch=master) [![](https://dcbadge.vercel.app/api/server/T6Gp6NFYHe?style=flat)](https://discord.gg/T6Gp6NFYHe) Please read [UPGRADE-v2.0.md](/UPGRADE-v2.0.md) to learn how to upgrade to Graphene `2.0`.
[💬 Join the community on Discord](https://discord.gg/T6Gp6NFYHe) ---
**We are looking for contributors**! Please check the current issues to see how you can help ❤️ # ![Graphene Logo](http://graphene-python.org/favicon.png) [Graphene](http://graphene-python.org) [![Build Status](https://travis-ci.org/graphql-python/graphene.svg?branch=master)](https://travis-ci.org/graphql-python/graphene) [![PyPI version](https://badge.fury.io/py/graphene.svg)](https://badge.fury.io/py/graphene) [![Coverage Status](https://coveralls.io/repos/graphql-python/graphene/badge.svg?branch=master&service=github)](https://coveralls.io/github/graphql-python/graphene?branch=master)
## Introduction
[Graphene](http://graphene-python.org) is an opinionated Python library for building GraphQL schemas/types fast and easily. [Graphene](http://graphene-python.org) is a Python library for building GraphQL schemas/types fast and easily.
- **Easy to use:** Graphene helps you use GraphQL in Python without effort. - **Easy to use:** Graphene helps you use GraphQL in Python without effort.
- **Relay:** Graphene has builtin support for Relay. - **Relay:** Graphene has builtin support for Relay.
- **Data agnostic:** Graphene supports any kind of data source: SQL (Django, SQLAlchemy), Mongo, custom Python objects, etc. - **Data agnostic:** Graphene supports any kind of data source: SQL (Django, SQLAlchemy), NoSQL, custom Python objects, etc.
We believe that by providing a complete API you could plug Graphene anywhere your data lives and make your data available We believe that by providing a complete API you could plug Graphene anywhere your data lives and make your data available
through GraphQL. through GraphQL.
## Integrations ## Integrations
Graphene has multiple integrations with different frameworks: Graphene has multiple integrations with different frameworks:
| integration | Package | | integration | Package |
| ----------------- | --------------------------------------------------------------------------------------- | |---------------|-------------------|
| SQLAlchemy | [graphene-sqlalchemy](https://github.com/graphql-python/graphene-sqlalchemy/) | | Django | [graphene-django](https://github.com/graphql-python/graphene-django/) |
| Mongo | [graphene-mongo](https://github.com/graphql-python/graphene-mongo/) | | SQLAlchemy | [graphene-sqlalchemy](https://github.com/graphql-python/graphene-sqlalchemy/) |
| Apollo Federation | [graphene-federation](https://github.com/graphql-python/graphene-federation/) | | Google App Engine | [graphene-gae](https://github.com/graphql-python/graphene-gae/) |
| Django | [graphene-django](https://github.com/graphql-python/graphene-django/) | | Peewee | *In progress* ([Tracking Issue](https://github.com/graphql-python/graphene/issues/289)) |
Also, Graphene is fully compatible with the GraphQL spec, working seamlessly with all GraphQL clients, such as [Relay](https://github.com/facebook/relay), [Apollo](https://github.com/apollographql/apollo-client) and [gql](https://github.com/graphql-python/gql). Also, Graphene is fully compatible with the GraphQL spec, working seamlessly with all GraphQL clients, such as [Relay](https://github.com/facebook/relay), [Apollo](https://github.com/apollographql/apollo-client) and [gql](https://github.com/graphql-python/gql).
## Installation ## Installation
To install `graphene`, just run this command in your shell For instaling graphene, just run this command in your shell
```bash ```bash
pip install "graphene>=3.1" pip install "graphene>=2.0"
``` ```
## 2.0 Upgrade Guide
Please read [UPGRADE-v2.0.md](/UPGRADE-v2.0.md) to learn how to upgrade.
## Examples ## Examples
Here is one example for you to get started: Here is one example for you to get started:
```python ```python
import graphene
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
hello = graphene.String(description='A typical hello world') hello = graphene.String(description='A typical hello world')
@ -64,13 +67,9 @@ result = schema.execute(query)
If you want to learn even more, you can also check the following [examples](examples/): If you want to learn even more, you can also check the following [examples](examples/):
- **Basic Schema**: [Starwars example](examples/starwars) * **Basic Schema**: [Starwars example](examples/starwars)
- **Relay Schema**: [Starwars Relay example](examples/starwars_relay) * **Relay Schema**: [Starwars Relay example](examples/starwars_relay)
## Documentation
Documentation and links to additional resources are available at
https://docs.graphene-python.org/en/latest/
## Contributing ## Contributing
@ -85,46 +84,43 @@ pip install -e ".[test]"
Well-written tests and maintaining good test coverage is important to this project. While developing, run new and existing tests with: Well-written tests and maintaining good test coverage is important to this project. While developing, run new and existing tests with:
```sh ```sh
pytest graphene/relay/tests/test_node.py # Single file py.test PATH/TO/MY/DIR/test_test.py # Single file
pytest graphene/relay # All tests in directory py.test PATH/TO/MY/DIR/ # All tests in directory
``` ```
Add the `-s` flag if you have introduced breakpoints into the code for debugging. Add the `-s` flag if you have introduced breakpoints into the code for debugging.
Add the `-v` ("verbose") flag to get more detailed test output. For even more detailed output, use `-vv`. Add the `-v` ("verbose") flag to get more detailed test output. For even more detailed output, use `-vv`.
Check out the [pytest documentation](https://docs.pytest.org/en/latest/) for more options and test running controls. Check out the [pytest documentation](https://docs.pytest.org/en/latest/) for more options and test running controls.
Regularly ensure your `pre-commit` hooks are up to date and enabled:
```sh
pre-commit install
```
You can also run the benchmarks with: You can also run the benchmarks with:
```sh ```sh
pytest graphene --benchmark-only py.test graphene --benchmark-only
``` ```
Graphene supports several versions of Python. To make sure that changes do not break compatibility with any of those versions, we use `tox` to create virtualenvs for each Python version and run tests with that version. To run against all Python versions defined in the `tox.ini` config file, just run: Graphene supports several versions of Python. To make sure that changes do not break compatibility with any of those versions, we use `tox` to create virtualenvs for each python version and run tests with that version. To run against all python versions defined in the `tox.ini` config file, just run:
```sh ```sh
tox tox
``` ```
If you wish to run against a specific version defined in the `tox.ini` file: If you wish to run against a specific version defined in the `tox.ini` file:
```sh ```sh
tox -e py39 tox -e py36
``` ```
Tox can only use whatever versions of python are installed on your system. When you create a pull request, Travis will also be running the same tests and report the results, so there is no need for potential contributors to try to install every single version of python on their own system ahead of time. We appreciate opening issues and pull requests to make graphene even more stable & useful!
Tox can only use whatever versions of Python are installed on your system. When you create a pull request, GitHub Actions pipelines will also be running the same tests and report the results, so there is no need for potential contributors to try to install every single version of Python on their own system ahead of time. We appreciate opening issues and pull requests to make graphene even more stable & useful! ### Documentation
### Building Documentation
The documentation is generated using the excellent [Sphinx](http://www.sphinx-doc.org/) and a custom theme. The documentation is generated using the excellent [Sphinx](http://www.sphinx-doc.org/) and a custom theme.
An HTML version of the documentation is produced by running: The documentation dependencies are installed by running:
```sh ```sh
make docs cd docs
pip install -r requirements.txt
```
Then to produce a HTML version of the documentation:
```sh
make html
``` ```

137
README.rst Normal file
View File

@ -0,0 +1,137 @@
Please read `UPGRADE-v2.0.md </UPGRADE-v2.0.md>`__ to learn how to
upgrade to Graphene ``2.0``.
--------------
|Graphene Logo| `Graphene <http://graphene-python.org>`__ |Build Status| |PyPI version| |Coverage Status|
=========================================================================================================
`Graphene <http://graphene-python.org>`__ is a Python library for
building GraphQL schemas/types fast and easily.
- **Easy to use:** Graphene helps you use GraphQL in Python without
effort.
- **Relay:** Graphene has builtin support for Relay.
- **Data agnostic:** Graphene supports any kind of data source: SQL
(Django, SQLAlchemy), NoSQL, custom Python objects, etc. We believe
that by providing a complete API you could plug Graphene anywhere
your data lives and make your data available through GraphQL.
Integrations
------------
Graphene has multiple integrations with different frameworks:
+---------------------+----------------------------------------------------------------------------------------------+
| integration | Package |
+=====================+==============================================================================================+
| Django | `graphene-django <https://github.com/graphql-python/graphene-django/>`__ |
+---------------------+----------------------------------------------------------------------------------------------+
| SQLAlchemy | `graphene-sqlalchemy <https://github.com/graphql-python/graphene-sqlalchemy/>`__ |
+---------------------+----------------------------------------------------------------------------------------------+
| Google App Engine | `graphene-gae <https://github.com/graphql-python/graphene-gae/>`__ |
+---------------------+----------------------------------------------------------------------------------------------+
| Peewee | *In progress* (`Tracking Issue <https://github.com/graphql-python/graphene/issues/289>`__) |
+---------------------+----------------------------------------------------------------------------------------------+
Also, Graphene is fully compatible with the GraphQL spec, working
seamlessly with all GraphQL clients, such as
`Relay <https://github.com/facebook/relay>`__,
`Apollo <https://github.com/apollographql/apollo-client>`__ and
`gql <https://github.com/graphql-python/gql>`__.
Installation
------------
For instaling graphene, just run this command in your shell
.. code:: bash
pip install "graphene>=2.0"
2.0 Upgrade Guide
-----------------
Please read `UPGRADE-v2.0.md </UPGRADE-v2.0.md>`__ to learn how to
upgrade.
Examples
--------
Here is one example for you to get started:
.. code:: python
class Query(graphene.ObjectType):
hello = graphene.String(description='A typical hello world')
def resolve_hello(self, info):
return 'World'
schema = graphene.Schema(query=Query)
Then Querying ``graphene.Schema`` is as simple as:
.. code:: python
query = '''
query SayHello {
hello
}
'''
result = schema.execute(query)
If you want to learn even more, you can also check the following
`examples <examples/>`__:
- **Basic Schema**: `Starwars example <examples/starwars>`__
- **Relay Schema**: `Starwars Relay
example <examples/starwars_relay>`__
Contributing
------------
After cloning this repo, ensure dependencies are installed by running:
.. code:: sh
pip install -e ".[test]"
After developing, the full test suite can be evaluated by running:
.. code:: sh
py.test graphene --cov=graphene --benchmark-skip # Use -v -s for verbose mode
You can also run the benchmarks with:
.. code:: sh
py.test graphene --benchmark-only
Documentation
~~~~~~~~~~~~~
The documentation is generated using the excellent
`Sphinx <http://www.sphinx-doc.org/>`__ and a custom theme.
The documentation dependencies are installed by running:
.. code:: sh
cd docs
pip install -r requirements.txt
Then to produce a HTML version of the documentation:
.. code:: sh
make html
.. |Graphene Logo| image:: http://graphene-python.org/favicon.png
.. |Build Status| image:: https://travis-ci.org/graphql-python/graphene.svg?branch=master
:target: https://travis-ci.org/graphql-python/graphene
.. |PyPI version| image:: https://badge.fury.io/py/graphene.svg
:target: https://badge.fury.io/py/graphene
.. |Coverage Status| image:: https://coveralls.io/repos/graphql-python/graphene/badge.svg?branch=master&service=github
:target: https://coveralls.io/github/graphql-python/graphene?branch=master

View File

@ -1,15 +0,0 @@
# Security Policy
## Supported Versions
Support for security issues is currently provided for Graphene 3.0 and above. Support on earlier versions cannot be guaranteed by the maintainers of this library, but community PRs may be accepted in critical cases.
The preferred mitigation strategy is via an upgrade to Graphene 3.
| Version | Supported |
| ------- | ------------------ |
| 3.x | :white_check_mark: |
| <3.x | :x: |
## Reporting a Vulnerability
Please use responsible disclosure by contacting a core maintainer via Discord or E-Mail.

View File

@ -2,29 +2,30 @@
Big changes from v0.10.x to 1.0. While on the surface a lot of this just looks like shuffling around API, the entire codebase has been rewritten to handle some really great use cases and improved performance. Big changes from v0.10.x to 1.0. While on the surface a lot of this just looks like shuffling around API, the entire codebase has been rewritten to handle some really great use cases and improved performance.
## Backwards Compatibility and Deprecation Warnings ## Backwards Compatibility and Deprecation Warnings
This has been a community project from the start, we need your help making the upgrade as smooth as possible for everybody! This has been a community project from the start, we need your help making the upgrade as smooth as possible for everybody!
We have done our best to provide backwards compatibility with deprecated APIs. We have done our best to provide backwards compatibility with deprecated APIs.
## Deprecations ## Deprecations
- `with_context` is no longer needed. Resolvers now always take the context argument. * `with_context` is no longer needed. Resolvers now always take the context argument.
Before: Before:
```python ```python
def resolve_xxx(root, args, info): def resolve_xxx(self, args, info):
# ... # ...
``` ```
With 1.0: With 1.0:
```python ```python
def resolve_xxx(root, args, context, info): def resolve_xxx(self, args, context, info):
# ... # ...
``` ```
- `ObjectType` and `Interface` no longer accept the `abstract` option in the `Meta`. * `ObjectType` and `Interface` no longer accept the `abstract` option in the `Meta`.
Inheriting fields should be now achieved using `AbstractType` inheritance. Inheriting fields should be now achieved using `AbstractType` inheritance.
Before: Before:
@ -41,7 +42,6 @@ We have done our best to provide backwards compatibility with deprecated APIs.
``` ```
With 1.0: With 1.0:
```python ```python
class MyBaseQuery(graphene.AbstractType): class MyBaseQuery(graphene.AbstractType):
my_field = String() my_field = String()
@ -50,9 +50,9 @@ We have done our best to provide backwards compatibility with deprecated APIs.
pass pass
``` ```
- The `type_name` option in the Meta in types is now `name` * The `type_name` option in the Meta in types is now `name`
- Type references no longer work with strings, but with functions. * Type references no longer work with strings, but with functions.
Before: Before:
@ -70,6 +70,7 @@ We have done our best to provide backwards compatibility with deprecated APIs.
users = graphene.List(lambda: User) users = graphene.List(lambda: User)
``` ```
## Schema ## Schema
Schemas in graphene `1.0` are `Immutable`, that means that once you create a `graphene.Schema` any Schemas in graphene `1.0` are `Immutable`, that means that once you create a `graphene.Schema` any
@ -79,6 +80,7 @@ The `name` argument is removed from the Schema.
The arguments `executor` and `middlewares` are also removed from the `Schema` definition. The arguments `executor` and `middlewares` are also removed from the `Schema` definition.
You can still use them, but by calling explicitly in the `execute` method in `graphql`. You can still use them, but by calling explicitly in the `execute` method in `graphql`.
```python ```python
# Old way # Old way
schema = graphene.Schema(name='My Schema') schema = graphene.Schema(name='My Schema')
@ -92,6 +94,7 @@ schema = graphene.Schema(
) )
``` ```
## Interfaces ## Interfaces
For implementing an Interface in an ObjectType, you have to add it onto `Meta.interfaces`. For implementing an Interface in an ObjectType, you have to add it onto `Meta.interfaces`.
@ -128,7 +131,7 @@ class ReverseString(Mutation):
reversed = String() reversed = String()
def mutate(root, args, context, info): def mutate(self, args, context, info):
reversed = args.get('input')[::-1] reversed = args.get('input')[::-1]
return ReverseString(reversed=reversed) return ReverseString(reversed=reversed)
@ -153,15 +156,16 @@ class Query(ObjectType):
``` ```
Also, if you wanted to create an `ObjectType` that implements `Node`, you have to do it Also, if you wanted to create an `ObjectType` that implements `Node`, you have to do it
explicitly. explicity.
## Django ## Django
The Django integration with Graphene now has an independent package: `graphene-django`. The Django integration with Graphene now has an independent package: `graphene-django`.
For installing, you have to replace the old `graphene[django]` with `graphene-django`. For installing, you have to replace the old `graphene[django]` with `graphene-django`.
- As the package is now independent, you now have to import from `graphene_django`. * As the package is now independent, you now have to import from `graphene_django`.
- **DjangoNode no longer exists**, please use `relay.Node` instead: * **DjangoNode no longer exists**, please use `relay.Node` instead:
```python ```python
from graphene.relay import Node from graphene.relay import Node
@ -177,8 +181,8 @@ For installing, you have to replace the old `graphene[django]` with `graphene-dj
The SQLAlchemy integration with Graphene now has an independent package: `graphene-sqlalchemy`. The SQLAlchemy integration with Graphene now has an independent package: `graphene-sqlalchemy`.
For installing, you have to replace the old `graphene[sqlalchemy]` with `graphene-sqlalchemy`. For installing, you have to replace the old `graphene[sqlalchemy]` with `graphene-sqlalchemy`.
- As the package is now independent, you have to import now from `graphene_sqlalchemy`. * As the package is now independent, you have to import now from `graphene_sqlalchemy`.
- **SQLAlchemyNode no longer exists**, please use `relay.Node` instead: * **SQLAlchemyNode no longer exists**, please use `relay.Node` instead:
```python ```python
from graphene.relay import Node from graphene.relay import Node

View File

@ -7,22 +7,20 @@ It also improves the field resolvers, [simplifying the code](#simpler-resolvers)
developer has to write to use them. developer has to write to use them.
**Deprecations:** **Deprecations:**
* [`AbstractType`](#abstracttype-deprecated)
- [`AbstractType`](#abstracttype-deprecated) * [`resolve_only_args`](#resolve_only_args)
- [`resolve_only_args`](#resolve_only_args) * [`Mutation.Input`](#mutationinput)
- [`Mutation.Input`](#mutationinput)
**Breaking changes:** **Breaking changes:**
* [`Simpler Resolvers`](#simpler-resolvers)
- [`Simpler Resolvers`](#simpler-resolvers) * [`Node Connections`](#node-connections)
- [`Node Connections`](#node-connections)
**New Features!** **New Features!**
* [`InputObjectType`](#inputobjecttype)
* [`Meta as Class arguments`](#meta-ass-class-arguments) (_only available for Python 3_)
- [`InputObjectType`](#inputobjecttype)
- [`Meta as Class arguments`](#meta-as-class-arguments) (_only available for Python 3_)
> The type metaclasses are now deleted as they are no longer necessary. If your code was depending > The type metaclasses are now deleted as they are no longer necessary. If your code was depending
> on this strategy for creating custom attrs, see an [example on how to do it in 2.0](https://github.com/graphql-python/graphene/blob/v2.0.0/graphene/tests/issues/test_425.py). > on this strategy for creating custom attrs, see an [example on how to do it in 2.0](https://github.com/graphql-python/graphene/blob/v2.0.0/graphene/tests/issues/test_425.py).
## Deprecations ## Deprecations
@ -51,7 +49,7 @@ class Pet(CommonFields, Interface):
pass pass
``` ```
### resolve_only_args ### resolve\_only\_args
`resolve_only_args` is now deprecated as the resolver API has been simplified. `resolve_only_args` is now deprecated as the resolver API has been simplified.
@ -62,8 +60,8 @@ class User(ObjectType):
name = String() name = String()
@resolve_only_args @resolve_only_args
def resolve_name(root): def resolve_name(self):
return root.name return self.name
``` ```
With 2.0: With 2.0:
@ -72,8 +70,8 @@ With 2.0:
class User(ObjectType): class User(ObjectType):
name = String() name = String()
def resolve_name(root, info): def resolve_name(self, info):
return root.name return self.name
``` ```
### Mutation.Input ### Mutation.Input
@ -96,6 +94,7 @@ class User(Mutation):
name = String() name = String()
``` ```
## Breaking Changes ## Breaking Changes
### Simpler resolvers ### Simpler resolvers
@ -109,7 +108,7 @@ Before:
```python ```python
my_field = graphene.String(my_arg=graphene.String()) my_field = graphene.String(my_arg=graphene.String())
def resolve_my_field(root, args, context, info): def resolve_my_field(self, args, context, info):
my_arg = args.get('my_arg') my_arg = args.get('my_arg')
return ... return ...
``` ```
@ -119,15 +118,15 @@ With 2.0:
```python ```python
my_field = graphene.String(my_arg=graphene.String()) my_field = graphene.String(my_arg=graphene.String())
def resolve_my_field(root, info, my_arg): def resolve_my_field(self, info, my_arg):
return ... return ...
``` ```
**PS.: Take care with receiving args like `my_arg` as above. This doesn't work for optional (non-required) arguments as standard `Connection`'s arguments (first, last, after, before).** **PS.: Take care with receiving args like `my_arg` as above. This doesn't work for optional (non-required) arguments as stantard `Connection`'s arguments (first, before, after, before).**
You may need something like this: You may need something like this:
```python ```python
def resolve_my_field(root, info, known_field1, known_field2, **args): ## get other args with: args.get('arg_key') def resolve_my_field(self, info, known_field1, known_field2, **args): ## get other args with: args.get('arg_key')
``` ```
And, if you need the context in the resolver, you can use `info.context`: And, if you need the context in the resolver, you can use `info.context`:
@ -135,7 +134,7 @@ And, if you need the context in the resolver, you can use `info.context`:
```python ```python
my_field = graphene.String(my_arg=graphene.String()) my_field = graphene.String(my_arg=graphene.String())
def resolve_my_field(root, info, my_arg): def resolve_my_field(self, info, my_arg):
context = info.context context = info.context
return ... return ...
``` ```
@ -189,7 +188,6 @@ class MyObject(ObjectType):
``` ```
To: To:
```python ```python
class MyObject(ObjectType): class MyObject(ObjectType):
class Meta: class Meta:
@ -205,32 +203,30 @@ class MyObject(ObjectType):
The parameters' order of `get_node_from_global_id` method has changed. You may need to adjust your [Node Root Field](http://docs.graphene-python.org/en/latest/relay/nodes/#node-root-field) and maybe other places that uses this method to obtain an object. The parameters' order of `get_node_from_global_id` method has changed. You may need to adjust your [Node Root Field](http://docs.graphene-python.org/en/latest/relay/nodes/#node-root-field) and maybe other places that uses this method to obtain an object.
Before: Before:
```python ```python
class RootQuery(object): class RootQuery(object):
... ...
node = Field(relay.Node, id=ID(required=True)) node = Field(relay.Node, id=ID(required=True))
def resolve_node(root, args, context, info): def resolve_node(self, args, context, info):
node = relay.Node.get_node_from_global_id(args['id'], context, info) node = relay.Node.get_node_from_global_id(args['id'], context, info)
return node return node
``` ```
Now: Now:
```python ```python
class RootQuery(object): class RootQuery(object):
... ...
node = Field(relay.Node, id=ID(required=True)) node = Field(relay.Node, id=ID(required=True))
def resolve_node(root, info, id): def resolve_node(self, info, id):
node = relay.Node.get_node_from_global_id(info, id) node = relay.Node.get_node_from_global_id(info, id)
return node return node
``` ```
## Mutation.mutate ## Mutation.mutate
Now only receives (`root`, `info`, `**kwargs`) and is not a @classmethod Now only receives (`self`, `info`, `**args`) and is not a @classmethod
Before: Before:
@ -249,7 +245,7 @@ With 2.0:
class SomeMutation(Mutation): class SomeMutation(Mutation):
... ...
def mutate(root, info, **args): def mutate(self, info, **args):
... ...
``` ```
@ -262,14 +258,17 @@ class SomeMutation(Mutation):
last_name = String(required=True) last_name = String(required=True)
... ...
def mutate(root, info, first_name, last_name): def mutate(self, info, first_name, last_name):
... ...
``` ```
## ClientIDMutation.mutate_and_get_payload ## ClientIDMutation.mutate_and_get_payload
Now only receives (`root`, `info`, `**input`) Now only receives (`root`, `info`, `**input`)
### Middlewares ### Middlewares
If you are using Middelwares, you need to some adjustments: If you are using Middelwares, you need to some adjustments:
@ -277,7 +276,7 @@ If you are using Middelwares, you need to some adjustments:
Before: Before:
```python ```python
class MyGrapheneMiddleware(object): class MyGrapheneMiddleware(object):
def resolve(self, next_mw, root, args, context, info): def resolve(self, next_mw, root, args, context, info):
## Middleware code ## Middleware code
@ -288,16 +287,17 @@ class MyGrapheneMiddleware(object):
With 2.0: With 2.0:
```python ```python
class MyGrapheneMiddleware(object): class MyGrapheneMiddleware(object):
def resolve(self, next_mw, root, info, **args): def resolve(self, next_mw, root, info, **args):
context = info.context context = info.context
## Middleware code ## Middleware code
info.context = context info.context = context
       return next_mw(root, info, **args)        return next_mw(root, info, **args)```
``` ```
## New Features ## New Features
### InputObjectType ### InputObjectType
@ -321,7 +321,7 @@ class Query(ObjectType):
user = graphene.Field(User, input=UserInput()) user = graphene.Field(User, input=UserInput())
@resolve_only_args @resolve_only_args
def resolve_user(root, input): def resolve_user(self, input):
user_id = input.get('id') user_id = input.get('id')
if is_valid_input(user_id): if is_valid_input(user_id):
return get_user(user_id) return get_user(user_id)
@ -334,17 +334,18 @@ class UserInput(InputObjectType):
id = ID(required=True) id = ID(required=True)
@property @property
def is_valid(root): def is_valid(self):
return root.id.startswith('userid_') return self.id.startswith('userid_')
class Query(ObjectType): class Query(ObjectType):
user = graphene.Field(User, input=UserInput()) user = graphene.Field(User, input=UserInput())
def resolve_user(root, info, input): def resolve_user(self, info, input):
if input.is_valid: if input.is_valid:
return get_user(input.id) return get_user(input.id)
``` ```
### Meta as Class arguments ### Meta as Class arguments
Now you can use the meta options as class arguments (**ONLY PYTHON 3**). Now you can use the meta options as class arguments (**ONLY PYTHON 3**).
@ -365,6 +366,7 @@ class Dog(ObjectType, interfaces=[Pet]):
name = String() name = String()
``` ```
### Abstract types ### Abstract types
Now you can create abstact types super easily, without the need of subclassing the meta. Now you can create abstact types super easily, without the need of subclassing the meta.
@ -376,8 +378,11 @@ class Base(ObjectType):
id = ID() id = ID()
def resolve_id(root, info): def resolve_id(self, info):
return f"{root.__class__.__name__}_{root.id}" return "{type}_{id}".format(
type=self.__class__.__name__,
id=self.id
)
``` ```
### UUID Scalar ### UUID Scalar

7
bin/autolinter Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
# Install the required scripts with
# pip install autoflake autopep8 isort
autoflake ./examples/ ./graphene/ -r --remove-unused-variables --remove-all-unused-imports --in-place
autopep8 ./examples/ ./graphene/ -r --in-place --experimental --aggressive --max-line-length 120
isort -rc ./examples/ ./graphene/

View File

@ -17,54 +17,75 @@ I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
.PHONY: help .PHONY: help
help: help:
@echo "Please use \`make <target>' where <target> is one of" @echo "Please use \`make <target>' where <target> is one of"
@grep -E '^\.PHONY: [a-zA-Z_-]+ .*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = "(: |##)"}; {printf "\033[36m%-30s\033[0m %s\n", $$2, $$3}' @echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " applehelp to make an Apple Help Book"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " epub3 to make an epub3"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " xml to make Docutils-native XML files"
@echo " pseudoxml to make pseudoxml-XML files for display purposes"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
@echo " coverage to run coverage check of the documentation (if enabled)"
@echo " dummy to check syntax errors of document sources"
.PHONY: install ## to install all documentation related requirements .PHONY: clean
install:
pip install -r requirements.txt
.PHONY: clean ## to remove all built documentation
clean: clean:
rm -rf $(BUILDDIR)/* rm -rf $(BUILDDIR)/*
.PHONY: html ## to make standalone HTML files .PHONY: html
html: html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo @echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html." @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
.PHONY: dirhtml ## to make HTML files named index.html in directories .PHONY: dirhtml
dirhtml: dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo @echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
.PHONY: singlehtml ## to make a single large HTML file .PHONY: singlehtml
singlehtml: singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo @echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
.PHONY: pickle ## to make pickle files .PHONY: pickle
pickle: pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo @echo
@echo "Build finished; now you can process the pickle files." @echo "Build finished; now you can process the pickle files."
.PHONY: json ## to make JSON files .PHONY: json
json: json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo @echo
@echo "Build finished; now you can process the JSON files." @echo "Build finished; now you can process the JSON files."
.PHONY: htmlhelp ## to make HTML files and a HTML help project .PHONY: htmlhelp
htmlhelp: htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo @echo
@echo "Build finished; now you can run HTML Help Workshop with the" \ @echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp." ".hhp project file in $(BUILDDIR)/htmlhelp."
.PHONY: qthelp ## to make HTML files and a qthelp project .PHONY: qthelp
qthelp: qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo @echo
@ -74,7 +95,7 @@ qthelp:
@echo "To view the help file:" @echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Graphene.qhc" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Graphene.qhc"
.PHONY: applehelp ## to make an Apple Help Book .PHONY: applehelp
applehelp: applehelp:
$(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp
@echo @echo
@ -83,7 +104,7 @@ applehelp:
"~/Library/Documentation/Help or install it in your application" \ "~/Library/Documentation/Help or install it in your application" \
"bundle." "bundle."
.PHONY: devhelp ## to make HTML files and a Devhelp project .PHONY: devhelp
devhelp: devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo @echo
@ -93,19 +114,19 @@ devhelp:
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Graphene" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Graphene"
@echo "# devhelp" @echo "# devhelp"
.PHONY: epub ## to make an epub .PHONY: epub
epub: epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo @echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub." @echo "Build finished. The epub file is in $(BUILDDIR)/epub."
.PHONY: epub3 ## to make an epub3 .PHONY: epub3
epub3: epub3:
$(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3 $(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3
@echo @echo
@echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3." @echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3."
.PHONY: latex ## to make LaTeX files, you can set PAPER=a4 or PAPER=letter .PHONY: latex
latex: latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo @echo
@ -113,33 +134,33 @@ latex:
@echo "Run \`make' in that directory to run these through (pdf)latex" \ @echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)." "(use \`make latexpdf' here to do that automatically)."
.PHONY: latexpdf ## to make LaTeX files and run them through pdflatex .PHONY: latexpdf
latexpdf: latexpdf:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..." @echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf $(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
.PHONY: latexpdfja ## to make LaTeX files and run them through platex/dvipdfmx .PHONY: latexpdfja
latexpdfja: latexpdfja:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through platex and dvipdfmx..." @echo "Running LaTeX files through platex and dvipdfmx..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
.PHONY: text ## to make text files .PHONY: text
text: text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo @echo
@echo "Build finished. The text files are in $(BUILDDIR)/text." @echo "Build finished. The text files are in $(BUILDDIR)/text."
.PHONY: man ## to make manual pages .PHONY: man
man: man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo @echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man." @echo "Build finished. The manual pages are in $(BUILDDIR)/man."
.PHONY: texinfo ## to make Texinfo files .PHONY: texinfo
texinfo: texinfo:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo @echo
@ -147,62 +168,62 @@ texinfo:
@echo "Run \`make' in that directory to run these through makeinfo" \ @echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)." "(use \`make info' here to do that automatically)."
.PHONY: info ## to make Texinfo files and run them through makeinfo .PHONY: info
info: info:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..." @echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
.PHONY: gettext ## to make PO message catalogs .PHONY: gettext
gettext: gettext:
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo @echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
.PHONY: changes ## to make an overview of all changed/added/deprecated items .PHONY: changes
changes: changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo @echo
@echo "The overview file is in $(BUILDDIR)/changes." @echo "The overview file is in $(BUILDDIR)/changes."
.PHONY: linkcheck ## to check all external links for integrity .PHONY: linkcheck
linkcheck: linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo @echo
@echo "Link check complete; look for any errors in the above output " \ @echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt." "or in $(BUILDDIR)/linkcheck/output.txt."
.PHONY: doctest ## to run all doctests embedded in the documentation (if enabled) .PHONY: doctest
doctest: doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \ @echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt." "results in $(BUILDDIR)/doctest/output.txt."
.PHONY: coverage ## to run coverage check of the documentation (if enabled) .PHONY: coverage
coverage: coverage:
$(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage
@echo "Testing of coverage in the sources finished, look at the " \ @echo "Testing of coverage in the sources finished, look at the " \
"results in $(BUILDDIR)/coverage/python.txt." "results in $(BUILDDIR)/coverage/python.txt."
.PHONY: xml ## to make Docutils-native XML files .PHONY: xml
xml: xml:
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
@echo @echo
@echo "Build finished. The XML files are in $(BUILDDIR)/xml." @echo "Build finished. The XML files are in $(BUILDDIR)/xml."
.PHONY: pseudoxml ## to make pseudoxml-XML files for display purposes .PHONY: pseudoxml
pseudoxml: pseudoxml:
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
@echo @echo
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
.PHONY: dummy ## to check syntax errors of document sources .PHONY: dummy
dummy: dummy:
$(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy $(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy
@echo @echo
@echo "Build finished. Dummy builder generates no files." @echo "Build finished. Dummy builder generates no files."
.PHONY: livehtml ## to build and serve live-reloading documentation .PHONY: livehtml
livehtml: livehtml:
sphinx-autobuild -b html --watch ../graphene $(ALLSPHINXOPTS) $(BUILDDIR)/html sphinx-autobuild -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html

View File

@ -1,110 +0,0 @@
API Reference
=============
Schema
------
.. autoclass:: graphene.types.schema.Schema
:members:
.. Uncomment sections / types as API documentation is fleshed out
.. in each class
Object types
------------
.. autoclass:: graphene.ObjectType
.. autoclass:: graphene.InputObjectType
.. autoclass:: graphene.Mutation
:members:
.. _fields-mounted-types:
Fields (Mounted Types)
----------------------
.. autoclass:: graphene.Field
.. autoclass:: graphene.Argument
.. autoclass:: graphene.InputField
Fields (Unmounted Types)
------------------------
.. autoclass:: graphene.types.unmountedtype.UnmountedType
GraphQL Scalars
---------------
.. autoclass:: graphene.Int()
.. autoclass:: graphene.Float()
.. autoclass:: graphene.String()
.. autoclass:: graphene.Boolean()
.. autoclass:: graphene.ID()
Graphene Scalars
----------------
.. autoclass:: graphene.Date()
.. autoclass:: graphene.DateTime()
.. autoclass:: graphene.Time()
.. autoclass:: graphene.Decimal()
.. autoclass:: graphene.UUID()
.. autoclass:: graphene.JSONString()
.. autoclass:: graphene.Base64()
Enum
----
.. autoclass:: graphene.Enum()
Structures
----------
.. autoclass:: graphene.List
.. autoclass:: graphene.NonNull
Type Extension
--------------
.. autoclass:: graphene.Interface()
.. autoclass:: graphene.Union()
Execution Metadata
------------------
.. autoclass:: graphene.ResolveInfo
.. autoclass:: graphene.Context
.. autoclass:: graphql.ExecutionResult
.. Relay
.. -----
.. .. autoclass:: graphene.Node
.. .. autoclass:: graphene.GlobalID
.. .. autoclass:: graphene.ClientIDMutation
.. .. autoclass:: graphene.Connection
.. .. autoclass:: graphene.ConnectionField
.. .. autoclass:: graphene.PageInfo

View File

@ -1,5 +1,4 @@
import os import os
import sys
import sphinx_graphene_theme import sphinx_graphene_theme
@ -23,8 +22,9 @@ on_rtd = os.environ.get("READTHEDOCS", None) == "True"
# add these directories to sys.path here. If the directory is relative to the # add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here. # documentation root, use os.path.abspath to make it absolute, like shown here.
# #
# import os
sys.path.insert(0, os.path.abspath("..")) # import sys
# sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ------------------------------------------------ # -- General configuration ------------------------------------------------
@ -41,7 +41,6 @@ extensions = [
"sphinx.ext.todo", "sphinx.ext.todo",
"sphinx.ext.coverage", "sphinx.ext.coverage",
"sphinx.ext.viewcode", "sphinx.ext.viewcode",
"sphinx.ext.napoleon",
] ]
if not on_rtd: if not on_rtd:
extensions += ["sphinx.ext.githubpages"] extensions += ["sphinx.ext.githubpages"]
@ -63,25 +62,25 @@ source_suffix = ".rst"
master_doc = "index" master_doc = "index"
# General information about the project. # General information about the project.
project = "Graphene" project = u"Graphene"
copyright = "Graphene 2016" copyright = u"Graphene 2016"
author = "Syrus Akbary" author = u"Syrus Akbary"
# The version info for the project you're documenting, acts as replacement for # The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the # |version| and |release|, also used in various other places throughout the
# built documents. # built documents.
# #
# The short X.Y version. # The short X.Y version.
version = "1.0" version = u"1.0"
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = "1.0" release = u"1.0"
# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.
# #
# This is also used if you do content translation via gettext catalogs. # This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases. # Usually you set "language" from the command line for these cases.
# language = None language = None
# There are two options for replacing |today|: either, you set today to some # There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used: # non-false value, then it is used:
@ -277,7 +276,7 @@ latex_elements = {
# (source start file, target name, title, # (source start file, target name, title,
# author, documentclass [howto, manual, or own class]). # author, documentclass [howto, manual, or own class]).
latex_documents = [ latex_documents = [
(master_doc, "Graphene.tex", "Graphene Documentation", "Syrus Akbary", "manual") (master_doc, "Graphene.tex", u"Graphene Documentation", u"Syrus Akbary", "manual")
] ]
# The name of an image file (relative to this directory) to place at the top of # The name of an image file (relative to this directory) to place at the top of
@ -317,7 +316,7 @@ latex_documents = [
# One entry per manual page. List of tuples # One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section). # (source start file, name, description, authors, manual section).
man_pages = [(master_doc, "graphene", "Graphene Documentation", [author], 1)] man_pages = [(master_doc, "graphene", u"Graphene Documentation", [author], 1)]
# If true, show URL addresses after external links. # If true, show URL addresses after external links.
# #
@ -333,7 +332,7 @@ texinfo_documents = [
( (
master_doc, master_doc,
"Graphene", "Graphene",
"Graphene Documentation", u"Graphene Documentation",
author, author,
"Graphene", "Graphene",
"One line description of project.", "One line description of project.",
@ -455,4 +454,5 @@ intersphinx_mapping = {
"http://docs.graphene-python.org/projects/sqlalchemy/en/latest/", "http://docs.graphene-python.org/projects/sqlalchemy/en/latest/",
None, None,
), ),
"graphene_gae": ("http://docs.graphene-python.org/projects/gae/en/latest/", None),
} }

View File

@ -4,7 +4,7 @@ Dataloader
DataLoader is a generic utility to be used as part of your application's DataLoader is a generic utility to be used as part of your application's
data fetching layer to provide a simplified and consistent API over data fetching layer to provide a simplified and consistent API over
various remote data sources such as databases or web services via batching various remote data sources such as databases or web services via batching
and caching. It is provided by a separate package `aiodataloader <https://pypi.org/project/aiodataloader/>`. and caching.
Batching Batching
@ -15,49 +15,38 @@ Create loaders by providing a batch loading function.
.. code:: python .. code:: python
from aiodataloader import DataLoader from promise import Promise
from promise.dataloader import DataLoader
class UserLoader(DataLoader): class UserLoader(DataLoader):
async def batch_load_fn(self, keys): def batch_load_fn(self, keys):
# Here we call a function to return a user for each key in keys # Here we return a promise that will result on the
return [get_user(id=key) for key in keys] # corresponding user for each key in keys
return Promise.resolve([get_user(id=key) for key in keys])
A batch loading async function accepts a list of keys, and returns a list of ``values``. A batch loading function accepts an list of keys, and returns a ``Promise``
which resolves to an list of ``values``.
Then load individual values from the loader. ``DataLoader`` will coalesce all
individual loads which occur within a single frame of execution (executed once
the wrapping promise is resolved) and then call your batch function with all
requested keys.
``DataLoader`` will coalesce all individual loads which occur within a
single frame of execution (executed once the wrapping event loop is resolved)
and then call your batch function with all requested keys.
.. code:: python .. code:: python
user_loader = UserLoader() user_loader = UserLoader()
user1 = await user_loader.load(1) user_loader.load(1).then(lambda user: user_loader.load(user.best_friend_id))
user1_best_friend = await user_loader.load(user1.best_friend_id)
user2 = await user_loader.load(2) user_loader.load(2).then(lambda user: user_loader.load(user.best_friend_id))
user2_best_friend = await user_loader.load(user2.best_friend_id)
A naive application may have issued *four* round-trips to a backend for the A naive application may have issued *four* round-trips to a backend for the
required information, but with ``DataLoader`` this application will make at most *two*. required information, but with ``DataLoader`` this application will make at most *two*.
Note that loaded values are one-to-one with the keys and must have the same
order. This means that if you load all values from a single query, you must
make sure that you then order the query result for the results to match the keys:
.. code:: python
class UserLoader(DataLoader):
async def batch_load_fn(self, keys):
users = {user.id: user for user in User.objects.filter(id__in=keys)}
return [users.get(user_id) for user_id in keys]
``DataLoader`` allows you to decouple unrelated parts of your application without ``DataLoader`` allows you to decouple unrelated parts of your application without
sacrificing the performance of batch data-loading. While the loader presents sacrificing the performance of batch data-loading. While the loader presents
an API that loads individual values, all concurrent requests will be coalesced an API that loads individual values, all concurrent requests will be coalesced
@ -95,7 +84,7 @@ Consider the following GraphQL request:
} }
If ``me``, ``bestFriend`` and ``friends`` each need to send a request to the backend, Naively, if ``me``, ``bestFriend`` and ``friends`` each need to request the backend,
there could be at most 13 database requests! there could be at most 13 database requests!
@ -110,8 +99,8 @@ leaner code and at most 4 database requests, and possibly fewer if there are cac
best_friend = graphene.Field(lambda: User) best_friend = graphene.Field(lambda: User)
friends = graphene.List(lambda: User) friends = graphene.List(lambda: User)
async def resolve_best_friend(root, info): def resolve_best_friend(self, info):
return await user_loader.load(root.best_friend_id) return user_loader.load(self.best_friend_id)
async def resolve_friends(root, info): def resolve_friends(self, info):
return await user_loader.load_many(root.friend_ids) return user_loader.load_many(self.friend_ids)

View File

@ -1,138 +1,59 @@
.. _SchemaExecute:
Executing a query Executing a query
================= =================
For executing a query against a schema, you can directly call the ``execute`` method on it.
For executing a query a schema, you can directly call the ``execute`` method on it.
.. code:: python .. code:: python
from graphene import Schema schema = graphene.Schema(...)
schema = Schema(...)
result = schema.execute('{ name }') result = schema.execute('{ name }')
``result`` represents the result of execution. ``result.data`` is the result of executing the query, ``result.errors`` is ``None`` if no errors occurred, and is a non-empty list if an error occurred. ``result`` represents the result of execution. ``result.data`` is the result of executing the query, ``result.errors`` is ``None`` if no errors occurred, and is a non-empty list if an error occurred.
.. _SchemaExecuteContext:
Context Context
_______ _______
You can pass context to a query via ``context``. You can pass context to a query via ``context_value``.
.. code:: python .. code:: python
from graphene import ObjectType, String, Schema class Query(graphene.ObjectType):
name = graphene.String()
class Query(ObjectType): def resolve_name(self, info):
name = String()
def resolve_name(root, info):
return info.context.get('name') return info.context.get('name')
schema = Schema(Query) schema = graphene.Schema(Query)
result = schema.execute('{ name }', context={'name': 'Syrus'}) result = schema.execute('{ name }', context_value={'name': 'Syrus'})
assert result.data['name'] == 'Syrus'
Variables Variables
_________ _______
You can pass variables to a query via ``variables``. You can pass variables to a query via ``variable_values``.
.. code:: python .. code:: python
from graphene import ObjectType, Field, ID, Schema class Query(graphene.ObjectType):
user = graphene.Field(User)
class Query(ObjectType): def resolve_user(self, info):
user = Field(User, id=ID(required=True)) return info.context.get('user')
def resolve_user(root, info, id): schema = graphene.Schema(Query)
return get_user_by_id(id)
schema = Schema(Query)
result = schema.execute( result = schema.execute(
''' '''query getUser($id: ID) {
query getUser($id: ID) {
user(id: $id) { user(id: $id) {
id
firstName
lastName
}
}
''',
variables={'id': 12},
)
Root Value
__________
Value used for :ref:`ResolverParamParent` in root queries and mutations can be overridden using ``root`` parameter.
.. code:: python
from graphene import ObjectType, Field, Schema
class Query(ObjectType):
me = Field(User)
def resolve_user(root, info):
return {'id': root.id, 'firstName': root.name}
schema = Schema(Query)
user_root = User(id=12, name='bob')
result = schema.execute(
'''
query getUser {
user {
id id
firstName firstName
lastName lastName
} }
} }''',
''', variable_values={'id': 12},
root=user_root
) )
assert result.data['user']['id'] == user_root.id
Operation Name
______________
If there are multiple operations defined in a query string, ``operation_name`` should be used to indicate which should be executed.
.. code:: python
from graphene import ObjectType, Field, Schema
class Query(ObjectType):
user = Field(User)
def resolve_user(root, info):
return get_user_by_id(12)
schema = Schema(Query)
query_string = '''
query getUserWithFirstName {
user {
id
firstName
lastName
}
}
query getUserWithFullName {
user {
id
fullName
}
}
'''
result = schema.execute(
query_string,
operation_name='getUserWithFullName'
)
assert result.data['user']['fullName']

View File

@ -1,8 +0,0 @@
File uploading
==============
File uploading is not part of the official GraphQL spec yet and is not natively
implemented in Graphene.
If your server needs to support file uploading then you can use the library: `graphene-file-upload <https://github.com/lmcgartland/graphene-file-upload>`_ which enhances Graphene to add file
uploads and conforms to the unoffical GraphQL `multipart request spec <https://github.com/jaydenseric/graphql-multipart-request-spec>`_.

View File

@ -8,6 +8,3 @@ Execution
execute execute
middleware middleware
dataloader dataloader
fileuploading
subscriptions
queryvalidation

View File

@ -16,7 +16,7 @@ Resolve arguments
Middlewares ``resolve`` is invoked with several arguments: Middlewares ``resolve`` is invoked with several arguments:
- ``next`` represents the execution chain. Call ``next`` to continue evaluation. - ``next`` represents the execution chain. Call ``next`` to continue evalution.
- ``root`` is the root value object passed throughout the query. - ``root`` is the root value object passed throughout the query.
- ``info`` is the resolver info. - ``info`` is the resolver info.
- ``args`` is the dict of arguments passed to the field. - ``args`` is the dict of arguments passed to the field.
@ -41,14 +41,12 @@ And then execute it with:
result = schema.execute('THE QUERY', middleware=[AuthorizationMiddleware()]) result = schema.execute('THE QUERY', middleware=[AuthorizationMiddleware()])
If the ``middleware`` argument includes multiple middlewares,
these middlewares will be executed bottom-up, i.e. from last to first.
Functional example Functional example
------------------ ------------------
Middleware can also be defined as a function. Here we define a middleware that Middleware can also be defined as a function. Here we define a middleware that
logs the time it takes to resolve each field: logs the time it takes to resolve each field
.. code:: python .. code:: python
@ -57,9 +55,12 @@ logs the time it takes to resolve each field:
def timing_middleware(next, root, info, **args): def timing_middleware(next, root, info, **args):
start = timer() start = timer()
return_value = next(root, info, **args) return_value = next(root, info, **args)
duration = round((timer() - start) * 1000, 2) duration = timer() - start
parent_type_name = root._meta.name if root and hasattr(root, '_meta') else '' logger.debug("{parent_type}.{field_name}: {duration} ms".format(
logger.debug(f"{parent_type_name}.{info.field_name}: {duration} ms") parent_type=root._meta.name if root and hasattr(root, '_meta') else '',
field_name=info.field_name,
duration=round(duration * 1000, 2)
))
return return_value return return_value

View File

@ -1,123 +0,0 @@
Query Validation
================
GraphQL uses query validators to check if Query AST is valid and can be executed. Every GraphQL server implements
standard query validators. For example, there is an validator that tests if queried field exists on queried type, that
makes query fail with "Cannot query field on type" error if it doesn't.
To help with common use cases, graphene provides a few validation rules out of the box.
Depth limit Validator
---------------------
The depth limit validator helps to prevent execution of malicious
queries. It takes in the following arguments.
- ``max_depth`` is the maximum allowed depth for any operation in a GraphQL document.
- ``ignore`` Stops recursive depth checking based on a field name. Either a string or regexp to match the name, or a function that returns a boolean
- ``callback`` Called each time validation runs. Receives an Object which is a map of the depths for each operation.
Usage
-----
Here is how you would implement depth-limiting on your schema.
.. code:: python
from graphql import validate, parse
from graphene import ObjectType, Schema, String
from graphene.validation import depth_limit_validator
class MyQuery(ObjectType):
name = String(required=True)
schema = Schema(query=MyQuery)
# queries which have a depth more than 20
# will not be executed.
validation_errors = validate(
schema=schema.graphql_schema,
document_ast=parse('THE QUERY'),
rules=(
depth_limit_validator(
max_depth=20
),
)
)
Disable Introspection
---------------------
the disable introspection validation rule ensures that your schema cannot be introspected.
This is a useful security measure in production environments.
Usage
-----
Here is how you would disable introspection for your schema.
.. code:: python
from graphql import validate, parse
from graphene import ObjectType, Schema, String
from graphene.validation import DisableIntrospection
class MyQuery(ObjectType):
name = String(required=True)
schema = Schema(query=MyQuery)
# introspection queries will not be executed.
validation_errors = validate(
schema=schema.graphql_schema,
document_ast=parse('THE QUERY'),
rules=(
DisableIntrospection,
)
)
Implementing custom validators
------------------------------
All custom query validators should extend the `ValidationRule <https://github.com/graphql-python/graphql-core/blob/v3.0.5/src/graphql/validation/rules/__init__.py#L37>`_
base class importable from the graphql.validation.rules module. Query validators are visitor classes. They are
instantiated at the time of query validation with one required argument (context: ASTValidationContext). In order to
perform validation, your validator class should define one or more of enter_* and leave_* methods. For possible
enter/leave items as well as details on function documentation, please see contents of the visitor module. To make
validation fail, you should call validator's report_error method with the instance of GraphQLError describing failure
reason. Here is an example query validator that visits field definitions in GraphQL query and fails query validation
if any of those fields are blacklisted:
.. code:: python
from graphql import GraphQLError
from graphql.language import FieldNode
from graphql.validation import ValidationRule
my_blacklist = (
"disallowed_field",
)
def is_blacklisted_field(field_name: str):
return field_name.lower() in my_blacklist
class BlackListRule(ValidationRule):
def enter_field(self, node: FieldNode, *_args):
field_name = node.name.value
if not is_blacklisted_field(field_name):
return
self.report_error(
GraphQLError(
f"Cannot query '{field_name}': field is blacklisted.", node,
)
)

View File

@ -1,40 +0,0 @@
.. _SchemaSubscription:
Subscriptions
=============
To create a subscription, you can directly call the ``subscribe`` method on the
schema. This method is async and must be awaited.
.. code:: python
import asyncio
from datetime import datetime
from graphene import ObjectType, String, Schema, Field
# Every schema requires a query.
class Query(ObjectType):
hello = String()
def resolve_hello(root, info):
return "Hello, world!"
class Subscription(ObjectType):
time_of_day = String()
async def subscribe_time_of_day(root, info):
while True:
yield datetime.now().isoformat()
await asyncio.sleep(1)
schema = Schema(query=Query, subscription=Subscription)
async def main(schema):
subscription = 'subscription { timeOfDay }'
result = await schema.subscribe(subscription)
async for item in result:
print(item.data['timeOfDay'])
asyncio.run(main(schema))
The ``result`` is an async iterator which yields items in the same manner as a query.

View File

@ -11,16 +11,11 @@ Contents:
execution/index execution/index
relay/index relay/index
testing/index testing/index
api/index
.. _Integrations:
Integrations Integrations
------------ -----
* `Graphene-Django <http://docs.graphene-python.org/projects/django/en/latest/>`_ (`source <https://github.com/graphql-python/graphene-django/>`_) * `Graphene-Django <http://docs.graphene-python.org/projects/django/en/latest/>`_ (`source <https://github.com/graphql-python/graphene-django/>`_)
* Flask-Graphql (`source <https://github.com/graphql-python/flask-graphql>`_)
* `Graphene-SQLAlchemy <http://docs.graphene-python.org/projects/sqlalchemy/en/latest/>`_ (`source <https://github.com/graphql-python/graphene-sqlalchemy/>`_) * `Graphene-SQLAlchemy <http://docs.graphene-python.org/projects/sqlalchemy/en/latest/>`_ (`source <https://github.com/graphql-python/graphene-sqlalchemy/>`_)
* `Graphene-GAE <http://docs.graphene-python.org/projects/gae/en/latest/>`_ (`source <https://github.com/graphql-python/graphene-gae/>`_)
* `Graphene-Mongo <http://graphene-mongo.readthedocs.io/en/latest/>`_ (`source <https://github.com/graphql-python/graphene-mongo>`_) * `Graphene-Mongo <http://graphene-mongo.readthedocs.io/en/latest/>`_ (`source <https://github.com/graphql-python/graphene-mongo>`_)
* `Starlette <https://www.starlette.io/graphql/>`_ (`source <https://github.com/encode/starlette>`_)
* `FastAPI <https://fastapi.tiangolo.com/advanced/graphql/>`_ (`source <https://github.com/tiangolo/fastapi>`_)

View File

@ -1,144 +1,57 @@
Getting started Getting started
=============== ===============
Introduction
------------
What is GraphQL? What is GraphQL?
~~~~~~~~~~~~~~~~ ----------------
GraphQL is a query language for your API. For an introduction to GraphQL and an overview of its concepts, please refer
to `the official introduction <http://graphql.org/learn/>`_.
It provides a standard way to:
* *describe data provided by a server* in a statically typed **Schema**
* *request data* in a **Query** which exactly describes your data requirements and
* *receive data* in a **Response** containing only the data you requested.
For an introduction to GraphQL and an overview of its concepts, please refer to `the official GraphQL documentation`_.
.. _the official GraphQL documentation: http://graphql.org/learn/
What is Graphene?
~~~~~~~~~~~~~~~~~
Graphene is a library that provides tools to implement a GraphQL API in Python using a *code-first* approach.
Compare Graphene's *code-first* approach to building a GraphQL API with *schema-first* approaches like `Apollo Server`_ (JavaScript) or Ariadne_ (Python). Instead of writing GraphQL **Schema Definition Language (SDL)**, we write Python code to describe the data provided by your server.
.. _Apollo Server: https://www.apollographql.com/docs/apollo-server/
.. _Ariadne: https://ariadnegraphql.org/
Graphene is fully featured with integrations for the most popular web frameworks and ORMs. Graphene produces schemas that are fully compliant with the GraphQL spec and provides tools and patterns for building a Relay-Compliant API as well.
An example in Graphene
----------------------
Lets build a basic GraphQL schema to say "hello" and "goodbye" in Graphene.
When we send a **Query** requesting only one **Field**, ``hello``, and specify a value for the ``firstName`` **Argument**...
.. code::
{
hello(firstName: "friend")
}
...we would expect the following Response containing only the data requested (the ``goodbye`` field is not resolved).
.. code::
{
"data": {
"hello": "Hello friend!"
}
}
Lets build a basic GraphQL schema from scratch.
Requirements Requirements
~~~~~~~~~~~~ ------------
- Python (3.8, 3.9, 3.10, 3.11, 3.12, pypy) - Python (2.7, 3.4, 3.5, 3.6, pypy)
- Graphene (3.0) - Graphene (2.0)
Project setup Project setup
~~~~~~~~~~~~~ -------------
.. code:: bash .. code:: bash
pip install "graphene>=3.0" pip install "graphene>=2.0"
Creating a basic Schema Creating a basic Schema
~~~~~~~~~~~~~~~~~~~~~~~ -----------------------
In Graphene, we can define a simple schema using the following code: A GraphQL schema describes your data model, and provides a GraphQL
server with an associated set of resolve methods that know how to fetch
data.
We are going to create a very simple schema, with a ``Query`` with only
one field: ``hello`` and an input name. And when we query it, it should return ``"Hello {name}"``.
.. code:: python .. code:: python
from graphene import ObjectType, String, Schema import graphene
class Query(ObjectType): class Query(graphene.ObjectType):
# this defines a Field `hello` in our Schema with a single Argument `first_name` hello = graphene.String(name=graphene.String(default_value="stranger"))
# By default, the argument name will automatically be camel-based into firstName in the generated schema
hello = String(first_name=String(default_value="stranger"))
goodbye = String()
# our Resolver method takes the GraphQL context (root, info) as well as def resolve_hello(self, info, name):
# Argument (first_name) for the Field and returns data for the query Response return 'Hello ' + name
def resolve_hello(root, info, first_name):
return f'Hello {first_name}!'
def resolve_goodbye(root, info): schema = graphene.Schema(query=Query)
return 'See ya!'
schema = Schema(query=Query)
A GraphQL **Schema** describes each **Field** in the data model provided by the server using scalar types like *String*, *Int* and *Enum* and compound types like *List* and *Object*. For more details refer to the Graphene :ref:`TypesReference`.
Our schema can also define any number of **Arguments** for our **Fields**. This is a powerful way for a **Query** to describe the exact data requirements for each **Field**.
For each **Field** in our **Schema**, we write a **Resolver** method to fetch data requested by a client's **Query** using the current context and **Arguments**. For more details, refer to this section on :ref:`Resolvers`.
Schema Definition Language (SDL)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the `GraphQL Schema Definition Language`_, we could describe the fields defined by our example code as shown below.
.. _GraphQL Schema Definition Language: https://graphql.org/learn/schema/
.. code::
type Query {
hello(firstName: String = "stranger"): String
goodbye: String
}
Further examples in this documentation will use SDL to describe schema created by ObjectTypes and other fields.
Querying Querying
~~~~~~~~ --------
Then we can start querying our **Schema** by passing a GraphQL query string to ``execute``: Then we can start querying our schema:
.. code:: python .. code:: python
# we can query for our field (with the default argument) result = schema.execute('{ hello }')
query_string = '{ hello }' print(result.data['hello']) # "Hello stranger"
result = schema.execute(query_string)
print(result.data['hello'])
# "Hello stranger!"
# or passing the argument in the query Congrats! You got your first graphene schema working!
query_with_argument = '{ hello(firstName: "GraphQL") }'
result = schema.execute(query_with_argument)
print(result.data['hello'])
# "Hello GraphQL!"
Next steps
~~~~~~~~~~
Congrats! You got your first Graphene schema working!
Normally, we don't need to directly execute a query string against our schema as Graphene provides many useful Integrations with popular web frameworks like Flask and Django. Check out :ref:`Integrations` for more information on how to get started serving your GraphQL API.

View File

@ -41,5 +41,5 @@ that implements ``Node`` will have a default Connection.
name = graphene.String() name = graphene.String()
ships = relay.ConnectionField(ShipConnection) ships = relay.ConnectionField(ShipConnection)
def resolve_ships(root, info): def resolve_ships(self, info):
return [] return []

View File

@ -19,8 +19,11 @@ Useful links
- `Getting started with Relay`_ - `Getting started with Relay`_
- `Relay Global Identification Specification`_ - `Relay Global Identification Specification`_
- `Relay Cursor Connection Specification`_ - `Relay Cursor Connection Specification`_
- `Relay input Object Mutation`_
.. _Relay: https://relay.dev/docs/guides/graphql-server-specification/ .. _Relay: https://facebook.github.io/relay/docs/en/graphql-server-specification.html
.. _Getting started with Relay: https://relay.dev/docs/getting-started/step-by-step-guide/ .. _Relay specification: https://facebook.github.io/relay/graphql/objectidentification.htm#sec-Node-root-field
.. _Relay Global Identification Specification: https://relay.dev/graphql/objectidentification.htm .. _Getting started with Relay: https://facebook.github.io/relay/docs/en/quick-start-guide.html
.. _Relay Cursor Connection Specification: https://relay.dev/graphql/connections.htm .. _Relay Global Identification Specification: https://facebook.github.io/relay/graphql/objectidentification.htm
.. _Relay Cursor Connection Specification: https://facebook.github.io/relay/graphql/connections.htm
.. _Relay input Object Mutation: https://facebook.github.io/relay/graphql/mutations.htm

View File

@ -51,20 +51,20 @@ Example of a custom node:
name = 'Node' name = 'Node'
@staticmethod @staticmethod
def to_global_id(type_, id): def to_global_id(type, id):
return f"{type_}:{id}" return '{}:{}'.format(type, id)
@staticmethod @staticmethod
def get_node_from_global_id(info, global_id, only_type=None): def get_node_from_global_id(info, global_id, only_type=None):
type_, id = global_id.split(':') type, id = global_id.split(':')
if only_type: if only_type:
# We assure that the node type that we want to retrieve # We assure that the node type that we want to retrieve
# is the same that was indicated in the field type # is the same that was indicated in the field type
assert type_ == only_type._meta.name, 'Received not compatible node.' assert type == only_type._meta.name, 'Received not compatible node.'
if type_ == 'User': if type == 'User':
return get_user(id) return get_user(id)
elif type_ == 'Photo': elif type == 'Photo':
return get_photo(id) return get_photo(id)

View File

@ -1,5 +1,4 @@
# Required library # Required library
Sphinx==6.1.3 Sphinx==1.5.3
sphinx-autobuild==2021.3.14
# Docs template # Docs template
http://graphene-python.org/sphinx_graphene_theme.zip http://graphene-python.org/sphinx_graphene_theme.zip

View File

@ -54,7 +54,7 @@ Execute parameters
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
You can also add extra keyword arguments to the ``execute`` method, such as You can also add extra keyword arguments to the ``execute`` method, such as
``context``, ``root``, ``variables``, ...: ``context_value``, ``root_value``, ``variable_values``, ...:
.. code:: python .. code:: python
@ -63,9 +63,49 @@ You can also add extra keyword arguments to the ``execute`` method, such as
def test_hey(): def test_hey():
client = Client(my_schema) client = Client(my_schema)
executed = client.execute('''{ hey }''', context={'user': 'Peter'}) executed = client.execute('''{ hey }''', context_value={'user': 'Peter'})
assert executed == { assert executed == {
'data': { 'data': {
'hey': 'hello Peter!' 'hey': 'hello Peter!'
} }
} }
Snapshot testing
~~~~~~~~~~~~~~~~
As our APIs evolve, we need to know when our changes introduce any breaking changes that might break
some of the clients of our GraphQL app.
However, writing tests and replicate the same response we expect from our GraphQL application can be
tedious and repetitive task, and sometimes it's easier to skip this process.
Because of that, we recommend the usage of `SnapshotTest <https://github.com/syrusakbary/snapshottest/>`_.
SnapshotTest let us write all this tests in a breeze, as creates automatically the ``snapshots`` for us
the first time the test is executed.
Here is a simple example on how our tests will look if we use ``pytest``:
.. code:: python
def test_hey(snapshot):
client = Client(my_schema)
# This will create a snapshot dir and a snapshot file
# the first time the test is executed, with the response
# of the execution.
snapshot.assert_match(client.execute('''{ hey }'''))
If we are using ``unittest``:
.. code:: python
from snapshottest import TestCase
class APITestCase(TestCase):
def test_api_me(self):
"""Testing the API for /me"""
client = Client(my_schema)
self.assertMatchSnapshot(client.execute('''{ hey }'''))

View File

@ -0,0 +1,43 @@
AbstractTypes
=============
An AbstractType contains fields that can be shared among
``graphene.ObjectType``, ``graphene.Interface``,
``graphene.InputObjectType`` or other ``graphene.AbstractType``.
The basics:
- Each AbstractType is a Python class that inherits from ``graphene.AbstractType``.
- Each attribute of the AbstractType represents a field (a ``graphene.Field`` or
``graphene.InputField`` depending on where it is mounted)
Quick example
-------------
In this example UserFields is an ``AbstractType`` with a name. ``User`` and
``UserInput`` are two types that have their own fields
plus the ones defined in ``UserFields``.
.. code:: python
import graphene
class UserFields(graphene.AbstractType):
name = graphene.String()
class User(graphene.ObjectType, UserFields):
pass
class UserInput(graphene.InputObjectType, UserFields):
pass
.. code::
type User {
name: String
}
inputtype UserInput {
name: String
}

View File

@ -1,7 +1,7 @@
Enums Enums
===== =====
An ``Enum`` is a special ``GraphQL`` type that represents a set of A ``Enum`` is a special ``GraphQL`` type that represents a set of
symbolic names (members) bound to unique, constant values. symbolic names (members) bound to unique, constant values.
Definition Definition
@ -59,10 +59,7 @@ you can add description etc. to your enum without changing the original:
.. code:: python .. code:: python
graphene.Enum.from_enum( graphene.Enum.from_enum(AlreadyExistingPyEnum, description=lambda value: return 'foo' if value == AlreadyExistingPyEnum.Foo else 'bar')
AlreadyExistingPyEnum,
description=lambda v: return 'foo' if v == AlreadyExistingPyEnum.Foo else 'bar'
)
Notes Notes
@ -77,7 +74,6 @@ In the Python ``Enum`` implementation you can access a member by initing the Enu
.. code:: python .. code:: python
from enum import Enum from enum import Enum
class Color(Enum): class Color(Enum):
RED = 1 RED = 1
GREEN = 2 GREEN = 2
@ -86,12 +82,11 @@ In the Python ``Enum`` implementation you can access a member by initing the Enu
assert Color(1) == Color.RED assert Color(1) == Color.RED
However, in Graphene ``Enum`` you need to call `.get` to have the same effect: However, in Graphene ``Enum`` you need to call get to have the same effect:
.. code:: python .. code:: python
from graphene import Enum from graphene import Enum
class Color(Enum): class Color(Enum):
RED = 1 RED = 1
GREEN = 2 GREEN = 2

View File

@ -1,5 +1,3 @@
.. _TypesReference:
=============== ===============
Types Reference Types Reference
=============== ===============
@ -7,11 +5,12 @@ Types Reference
.. toctree:: .. toctree::
:maxdepth: 1 :maxdepth: 1
schema enums
scalars scalars
list-and-nonnull list-and-nonnull
objecttypes objecttypes
enums
interfaces interfaces
unions unions
schema
mutations mutations
abstracttypes

View File

@ -1,5 +1,3 @@
.. _Interfaces:
Interfaces Interfaces
========== ==========
@ -44,7 +42,7 @@ Both of these types have all of the fields from the ``Character`` interface,
but also bring in extra fields, ``home_planet``, ``starships`` and but also bring in extra fields, ``home_planet``, ``starships`` and
``primary_function``, that are specific to that particular type of character. ``primary_function``, that are specific to that particular type of character.
The full GraphQL schema definition will look like this: The full GraphQL schema defition will look like this:
.. code:: .. code::
@ -84,7 +82,7 @@ For example, you can define a field ``hero`` that resolves to any
episode=graphene.Int(required=True) episode=graphene.Int(required=True)
) )
def resolve_hero(root, info, episode): def resolve_hero(_, info, episode):
# Luke is the hero of Episode V # Luke is the hero of Episode V
if episode == 5: if episode == 5:
return get_human(name='Luke Skywalker') return get_human(name='Luke Skywalker')
@ -93,7 +91,7 @@ For example, you can define a field ``hero`` that resolves to any
schema = graphene.Schema(query=Query, types=[Human, Droid]) schema = graphene.Schema(query=Query, types=[Human, Droid])
This allows you to directly query for fields that exist on the Character interface This allows you to directly query for fields that exist on the Character interface
as well as selecting specific fields on any type that implements the interface as well as selecting specific fields on any type that implments the interface
using `inline fragments <https://graphql.org/learn/queries/#inline-fragments>`_. using `inline fragments <https://graphql.org/learn/queries/#inline-fragments>`_.
For example, the following query: For example, the following query:

View File

@ -19,7 +19,7 @@ This example defines a Mutation:
ok = graphene.Boolean() ok = graphene.Boolean()
person = graphene.Field(lambda: Person) person = graphene.Field(lambda: Person)
def mutate(root, info, name): def mutate(self, info, name):
person = Person(name=name) person = Person(name=name)
ok = True ok = True
return CreatePerson(person=person, ok=ok) return CreatePerson(person=person, ok=ok)
@ -27,13 +27,12 @@ This example defines a Mutation:
**person** and **ok** are the output fields of the Mutation when it is **person** and **ok** are the output fields of the Mutation when it is
resolved. resolved.
**Arguments** attributes are the arguments that the Mutation **Input** attributes are the arguments that the Mutation
``CreatePerson`` needs for resolving, in this case **name** will be the ``CreatePerson`` needs for resolving, in this case **name** will be the
only argument for the mutation. only argument for the mutation.
**mutate** is the function that will be applied once the mutation is **mutate** is the function that will be applied once the mutation is
called. This method is just a special resolver that we can change called.
data within. It takes the same arguments as the standard query :ref:`ResolverArguments`.
So, we can finish our schema like this: So, we can finish our schema like this:
@ -85,9 +84,9 @@ We should receive:
InputFields and InputObjectTypes InputFields and InputObjectTypes
---------------------------------- ----------------------------------
InputFields are used in mutations to allow nested input data for mutations. InputFields are used in mutations to allow nested input data for mutations
To use an InputField you define an InputObjectType that specifies the structure of your input data: To use an InputField you define an InputObjectType that specifies the structure of your input data
.. code:: python .. code:: python
@ -104,6 +103,7 @@ To use an InputField you define an InputObjectType that specifies the structure
person = graphene.Field(Person) person = graphene.Field(Person)
@staticmethod
def mutate(root, info, person_data=None): def mutate(root, info, person_data=None):
person = Person( person = Person(
name=person_data.name, name=person_data.name,
@ -112,7 +112,7 @@ To use an InputField you define an InputObjectType that specifies the structure
return CreatePerson(person=person) return CreatePerson(person=person)
Note that **name** and **age** are part of **person_data** now. Note that **name** and **age** are part of **person_data** now
Using the above mutation your new query would look like this: Using the above mutation your new query would look like this:
@ -128,7 +128,7 @@ Using the above mutation your new query would look like this:
} }
InputObjectTypes can also be fields of InputObjectTypes allowing you to have InputObjectTypes can also be fields of InputObjectTypes allowing you to have
as complex of input data as you need: as complex of input data as you need
.. code:: python .. code:: python
@ -157,10 +157,10 @@ To return an existing ObjectType instead of a mutation-specific type, set the **
Output = Person Output = Person
def mutate(root, info, name): def mutate(self, info, name):
return Person(name=name) return Person(name=name)
Then, if we query (``schema.execute(query_str)``) with the following: Then, if we query (``schema.execute(query_str)``) the following:
.. code:: .. code::

View File

@ -1,15 +1,15 @@
.. _ObjectType: ObjectTypes
===========
ObjectType An ObjectType is the single, definitive source of information about your
========== data. It contains the essential fields and behaviors of the data youre
querying.
A Graphene *ObjectType* is the building block used to define the relationship between **Fields** in your **Schema** and how their data is retrieved.
The basics: The basics:
- Each ObjectType is a Python class that inherits from ``graphene.ObjectType``. - Each ObjectType is a Python class that inherits from
``graphene.ObjectType``.
- Each attribute of the ObjectType represents a ``Field``. - Each attribute of the ObjectType represents a ``Field``.
- Each ``Field`` has a :ref:`resolver method<Resolvers>` to fetch data (or :ref:`DefaultResolver`).
Quick example Quick example
------------- -------------
@ -18,17 +18,19 @@ This example model defines a Person, with a first and a last name:
.. code:: python .. code:: python
from graphene import ObjectType, String import graphene
class Person(ObjectType): class Person(graphene.ObjectType):
first_name = String() first_name = graphene.String()
last_name = String() last_name = graphene.String()
full_name = String() full_name = graphene.String()
def resolve_full_name(parent, info): def resolve_full_name(self, info):
return f"{parent.first_name} {parent.last_name}" return '{} {}'.format(self.first_name, self.last_name)
This *ObjectType* defines the field **first\_name**, **last\_name**, and **full\_name**. Each field is specified as a class attribute, and each attribute maps to a Field. Data is fetched by our ``resolve_full_name`` :ref:`resolver method<Resolvers>` for ``full_name`` field and the :ref:`DefaultResolver` for other fields. **first\_name** and **last\_name** are fields of the ObjectType. Each
field is specified as a class attribute, and each attribute maps to a
Field.
The above ``Person`` ObjectType has the following schema representation: The above ``Person`` ObjectType has the following schema representation:
@ -40,396 +42,65 @@ The above ``Person`` ObjectType has the following schema representation:
fullName: String fullName: String
} }
.. _Resolvers:
Resolvers Resolvers
--------- ---------
A **Resolver** is a method that helps us answer **Queries** by fetching data for a **Field** in our **Schema**. A resolver is a method that resolves certain fields within a
``ObjectType``. If not specififed otherwise, the resolver of a
field is the ``resolve_{field_name}`` method on the ``ObjectType``.
Resolvers are lazily executed, so if a field is not included in a query, its resolver will not be executed. By default resolvers take the arguments ``info`` and ``*args``.
Each field on an *ObjectType* in Graphene should have a corresponding resolver method to fetch data. This resolver method should match the field name. For example, in the ``Person`` type above, the ``full_name`` field is resolved by the method ``resolve_full_name``. NOTE: The resolvers on a ``ObjectType`` are always treated as ``staticmethod``\ s,
so the first argument to the resolver method ``self`` (or ``root``) need
not be an actual instance of the ``ObjectType``.
Each resolver method takes the parameters:
* :ref:`ResolverParamParent` for the value object use to resolve most fields Quick example
* :ref:`ResolverParamInfo` for query and schema meta information and per-request context ~~~~~~~~~~~~~
* :ref:`ResolverParamGraphQLArguments` as defined on the **Field**.
.. _ResolverArguments: This example model defines a ``Query`` type, which has a reverse field
that reverses the given ``word`` argument using the ``resolve_reverse``
Resolver Parameters method in the class.
~~~~~~~~~~~~~~~~~~~
.. _ResolverParamParent:
Parent Value Object (*parent*)
******************************
This parameter is typically used to derive the values for most fields on an *ObjectType*.
The first parameter of a resolver method (*parent*) is the value object returned from the resolver of the parent field. If there is no parent field, such as a root Query field, then the value for *parent* is set to the ``root_value`` configured while executing the query (default ``None``). See :ref:`SchemaExecute` for more details on executing queries.
Resolver example
^^^^^^^^^^^^^^^^
If we have a schema with Person type and one field on the root query.
.. code:: python .. code:: python
from graphene import ObjectType, String, Field import graphene
def get_human(name): class Query(graphene.ObjectType):
first_name, last_name = name.split() reverse = graphene.String(word=graphene.String())
return Person(first_name, last_name)
class Person(ObjectType): def resolve_reverse(self, info, word):
full_name = String() return word[::-1]
def resolve_full_name(parent, info):
return f"{parent.first_name} {parent.last_name}"
class Query(ObjectType):
me = Field(Person)
def resolve_me(parent, info):
# returns an object that represents a Person
return get_human(name="Luke Skywalker")
When we execute a query against that schema.
.. code:: python
schema = Schema(query=Query)
query_string = "{ me { fullName } }"
result = schema.execute(query_string)
assert result.data["me"] == {"fullName": "Luke Skywalker"}
Then we go through the following steps to resolve this query:
* ``parent`` is set with the root_value from query execution (None).
* ``Query.resolve_me`` called with ``parent`` None which returns a value object ``Person("Luke", "Skywalker")``.
* This value object is then used as ``parent`` while calling ``Person.resolve_full_name`` to resolve the scalar String value "Luke Skywalker".
* The scalar value is serialized and sent back in the query response.
Each resolver returns the next :ref:`ResolverParamParent` to be used in executing the following resolver in the chain. If the Field is a Scalar type, that value will be serialized and sent in the **Response**. Otherwise, while resolving Compound types like *ObjectType*, the value be passed forward as the next :ref:`ResolverParamParent`.
Naming convention
^^^^^^^^^^^^^^^^^
This :ref:`ResolverParamParent` is sometimes named ``obj``, ``parent``, or ``source`` in other GraphQL documentation. It can also be named after the value object being resolved (ex. ``root`` for a root Query or Mutation, and ``person`` for a Person value object). Sometimes this argument will be named ``self`` in Graphene code, but this can be misleading due to :ref:`ResolverImplicitStaticMethod` while executing queries in Graphene.
.. _ResolverParamInfo:
GraphQL Execution Info (*info*)
*******************************
The second parameter provides two things:
* reference to meta information about the execution of the current GraphQL Query (fields, schema, parsed query, etc.)
* access to per-request ``context`` which can be used to store user authentication, data loader instances or anything else useful for resolving the query.
Only context will be required for most applications. See :ref:`SchemaExecuteContext` for more information about setting context.
.. _ResolverParamGraphQLArguments:
GraphQL Arguments (*\*\*kwargs*)
********************************
Any arguments that a field defines gets passed to the resolver function as
keyword arguments. For example:
.. code:: python
from graphene import ObjectType, Field, String
class Query(ObjectType):
human_by_name = Field(Human, name=String(required=True))
def resolve_human_by_name(parent, info, name):
return get_human(name=name)
You can then execute the following query:
.. code::
query {
humanByName(name: "Luke Skywalker") {
firstName
lastName
}
}
*Note:* There are several arguments to a field that are "reserved" by Graphene
(see :ref:`fields-mounted-types`).
You can still define an argument that clashes with one of these fields by using
the ``args`` parameter like so:
.. code:: python
from graphene import ObjectType, Field, String
class Query(ObjectType):
answer = String(args={'description': String()})
def resolve_answer(parent, info, description):
return description
Convenience Features of Graphene Resolvers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _ResolverImplicitStaticMethod:
Implicit staticmethod
*********************
One surprising feature of Graphene is that all resolver methods are treated implicitly as staticmethods. This means that, unlike other methods in Python, the first argument of a resolver is *never* ``self`` while it is being executed by Graphene. Instead, the first argument is always :ref:`ResolverParamParent`. In practice, this is very convenient as, in GraphQL, we are almost always more concerned with the using the parent value object to resolve queries than attributes on the Python object itself.
The two resolvers in this example are effectively the same.
.. code:: python
from graphene import ObjectType, String
class Person(ObjectType):
first_name = String()
last_name = String()
@staticmethod
def resolve_first_name(parent, info):
'''
Decorating a Python method with `staticmethod` ensures that `self` will not be provided as an
argument. However, Graphene does not need this decorator for this behavior.
'''
return parent.first_name
def resolve_last_name(parent, info):
'''
Normally the first argument for this method would be `self`, but Graphene executes this as
a staticmethod implicitly.
'''
return parent.last_name
# ...
If you prefer your code to be more explicit, feel free to use ``@staticmethod`` decorators. Otherwise, your code may be cleaner without them!
.. _DefaultResolver:
Default Resolver
****************
If a resolver method is not defined for a **Field** attribute on our *ObjectType*, Graphene supplies a default resolver.
If the :ref:`ResolverParamParent` is a dictionary, the resolver will look for a dictionary key matching the field name. Otherwise, the resolver will get the attribute from the parent value object matching the field name.
.. code:: python
from collections import namedtuple
from graphene import ObjectType, String, Field, Schema
PersonValueObject = namedtuple("Person", ["first_name", "last_name"])
class Person(ObjectType):
first_name = String()
last_name = String()
class Query(ObjectType):
me = Field(Person)
my_best_friend = Field(Person)
def resolve_me(parent, info):
# always pass an object for `me` field
return PersonValueObject(first_name="Luke", last_name="Skywalker")
def resolve_my_best_friend(parent, info):
# always pass a dictionary for `my_best_fiend_field`
return {"first_name": "R2", "last_name": "D2"}
schema = Schema(query=Query)
result = schema.execute('''
{
me { firstName lastName }
myBestFriend { firstName lastName }
}
''')
# With default resolvers we can resolve attributes from an object..
assert result.data["me"] == {"firstName": "Luke", "lastName": "Skywalker"}
# With default resolvers, we can also resolve keys from a dictionary..
assert result.data["myBestFriend"] == {"firstName": "R2", "lastName": "D2"}
Advanced
~~~~~~~~
GraphQL Argument defaults
*************************
If you define an argument for a field that is not required (and in a query
execution it is not provided as an argument) it will not be passed to the
resolver function at all. This is so that the developer can differentiate
between a ``undefined`` value for an argument and an explicit ``null`` value.
For example, given this schema:
.. code:: python
from graphene import ObjectType, String
class Query(ObjectType):
hello = String(required=True, name=String())
def resolve_hello(parent, info, name):
return name if name else 'World'
And this query:
.. code::
query {
hello
}
An error will be thrown:
.. code::
TypeError: resolve_hello() missing 1 required positional argument: 'name'
You can fix this error in several ways. Either by combining all keyword arguments
into a dict:
.. code:: python
from graphene import ObjectType, String
class Query(ObjectType):
hello = String(required=True, name=String())
def resolve_hello(parent, info, **kwargs):
name = kwargs.get('name', 'World')
return f'Hello, {name}!'
Or by setting a default value for the keyword argument:
.. code:: python
from graphene import ObjectType, String
class Query(ObjectType):
hello = String(required=True, name=String())
def resolve_hello(parent, info, name='World'):
return f'Hello, {name}!'
One can also set a default value for an Argument in the GraphQL schema itself using Graphene!
.. code:: python
from graphene import ObjectType, String
class Query(ObjectType):
hello = String(
required=True,
name=String(default_value='World')
)
def resolve_hello(parent, info, name):
return f'Hello, {name}!'
Resolvers outside the class Resolvers outside the class
*************************** ~~~~~~~~~~~~~~~~~~~~~~~~~~~
A field can use a custom resolver from outside the class: A field can use a custom resolver from outside the class:
.. code:: python .. code:: python
from graphene import ObjectType, String import graphene
def resolve_full_name(person, info): def reverse(root, info, word):
return f"{person.first_name} {person.last_name}" return word[::-1]
class Person(ObjectType): class Query(graphene.ObjectType):
first_name = String() reverse = graphene.String(word=graphene.String(), resolver=reverse)
last_name = String()
full_name = String(resolver=resolve_full_name)
Instances as value objects Instances as data containers
************************** ----------------------------
Graphene ``ObjectType``\ s can act as value objects too. So with the Graphene ``ObjectType``\ s can act as containers too. So with the
previous example you could use ``Person`` to capture data for each of the *ObjectType*'s fields. previous example you could do:
.. code:: python .. code:: python
peter = Person(first_name='Peter', last_name='Griffin') peter = Person(first_name='Peter', last_name='Griffin')
peter.first_name # prints "Peter" peter.first_name # prints "Peter"
peter.last_name # prints "Griffin" peter.last_name # prints "Griffin"
Field camelcasing
*****************
Graphene automatically camelcases fields on *ObjectType* from ``field_name`` to ``fieldName`` to conform with GraphQL standards. See :ref:`SchemaAutoCamelCase` for more information.
*ObjectType* Configuration - Meta class
---------------------------------------
Graphene uses a Meta inner class on *ObjectType* to set different options.
GraphQL type name
~~~~~~~~~~~~~~~~~
By default the type name in the GraphQL schema will be the same as the class name
that defines the ``ObjectType``. This can be changed by setting the ``name``
property on the ``Meta`` class:
.. code:: python
from graphene import ObjectType
class MyGraphQlSong(ObjectType):
class Meta:
name = 'Song'
GraphQL Description
~~~~~~~~~~~~~~~~~~~
The schema description of an *ObjectType* can be set as a docstring on the Python object or on the Meta inner class.
.. code:: python
from graphene import ObjectType
class MyGraphQlSong(ObjectType):
''' We can set the schema description for an Object Type here on a docstring '''
class Meta:
description = 'But if we set the description in Meta, this value is used instead'
Interfaces & Possible Types
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Setting ``interfaces`` in Meta inner class specifies the GraphQL Interfaces that this Object implements.
Providing ``possible_types`` helps Graphene resolve ambiguous types such as interfaces or Unions.
See :ref:`Interfaces` for more information.
.. code:: python
from graphene import ObjectType, Node
Song = namedtuple('Song', ('title', 'artist'))
class MyGraphQlSong(ObjectType):
class Meta:
interfaces = (Node, )
possible_types = (Song, )
.. _Interface: /docs/interfaces/ .. _Interface: /docs/interfaces/

View File

@ -1,13 +1,6 @@
.. _Scalars:
Scalars Scalars
======= =======
Scalar types represent concrete values at the leaves of a query. There are
several built in types that Graphene provides out of the box which represent common
values in Python. You can also create your own Scalar types to better express
values that you might have in your data model.
All Scalar types accept the following arguments. All are optional: All Scalar types accept the following arguments. All are optional:
``name``: *string* ``name``: *string*
@ -20,7 +13,7 @@ All Scalar types accept the following arguments. All are optional:
``required``: *boolean* ``required``: *boolean*
If ``True``, the server will enforce a value for this field. See `NonNull <../list-and-nonnull.html#nonnull>`_. Default is ``False``. If ``True``, the server will enforce a value for this field. See `NonNull <./list-and-nonnull.html#nonnull>`_. Default is ``False``.
``deprecation_reason``: *string* ``deprecation_reason``: *string*
@ -32,39 +25,35 @@ All Scalar types accept the following arguments. All are optional:
Built in scalars Base scalars
---------------- ------------
Graphene defines the following base Scalar Types that match the default `GraphQL types <https://graphql.org/learn/schema/#scalar-types>`_: Graphene defines the following base Scalar Types:
``graphene.String`` ``graphene.String``
^^^^^^^^^^^^^^^^^^^
Represents textual data, represented as UTF-8 Represents textual data, represented as UTF-8
character sequences. The String type is most often used by GraphQL to character sequences. The String type is most often used by GraphQL to
represent free-form human-readable text. represent free-form human-readable text.
``graphene.Int`` ``graphene.Int``
^^^^^^^^^^^^^^^^
Represents non-fractional signed whole numeric Represents non-fractional signed whole numeric
values. Int is a signed 32bit integer per the values. Int can represent values between `-(2^53 - 1)` and `2^53 - 1` since
`GraphQL spec <https://facebook.github.io/graphql/June2018/#sec-Int>`_ represented in JSON as double-precision floating point numbers specified
by `IEEE 754 <http://en.wikipedia.org/wiki/IEEE_floating_point>`_.
``graphene.Float`` ``graphene.Float``
^^^^^^^^^^^^^^^^^^
Represents signed double-precision fractional Represents signed double-precision fractional
values as specified by values as specified by
`IEEE 754 <http://en.wikipedia.org/wiki/IEEE_floating_point>`_. `IEEE 754 <http://en.wikipedia.org/wiki/IEEE_floating_point>`_.
``graphene.Boolean`` ``graphene.Boolean``
^^^^^^^^^^^^^^^^^^^^
Represents `true` or `false`. Represents `true` or `false`.
``graphene.ID`` ``graphene.ID``
^^^^^^^^^^^^^^^
Represents a unique identifier, often used to Represents a unique identifier, often used to
refetch an object or as key for a cache. The ID type appears in a JSON refetch an object or as key for a cache. The ID type appears in a JSON
@ -72,183 +61,24 @@ Graphene defines the following base Scalar Types that match the default `GraphQL
When expected as an input type, any string (such as `"4"`) or integer When expected as an input type, any string (such as `"4"`) or integer
(such as `4`) input value will be accepted as an ID. (such as `4`) input value will be accepted as an ID.
---- Graphene also provides custom scalars for Dates, Times, and JSON:
Graphene also provides custom scalars for common values: ``graphene.types.datetime.Date``
``graphene.Date``
^^^^^^^^^^^^^^^^^
Represents a Date value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_. Represents a Date value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_.
.. code:: python ``graphene.types.datetime.DateTime``
import datetime
from graphene import Schema, ObjectType, Date
class Query(ObjectType):
one_week_from = Date(required=True, date_input=Date(required=True))
def resolve_one_week_from(root, info, date_input):
assert date_input == datetime.date(2006, 1, 2)
return date_input + datetime.timedelta(weeks=1)
schema = Schema(query=Query)
results = schema.execute("""
query {
oneWeekFrom(dateInput: "2006-01-02")
}
""")
assert results.data == {"oneWeekFrom": "2006-01-09"}
``graphene.DateTime``
^^^^^^^^^^^^^^^^^^^^^
Represents a DateTime value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_. Represents a DateTime value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_.
.. code:: python ``graphene.types.datetime.Time``
import datetime
from graphene import Schema, ObjectType, DateTime
class Query(ObjectType):
one_hour_from = DateTime(required=True, datetime_input=DateTime(required=True))
def resolve_one_hour_from(root, info, datetime_input):
assert datetime_input == datetime.datetime(2006, 1, 2, 15, 4, 5)
return datetime_input + datetime.timedelta(hours=1)
schema = Schema(query=Query)
results = schema.execute("""
query {
oneHourFrom(datetimeInput: "2006-01-02T15:04:05")
}
""")
assert results.data == {"oneHourFrom": "2006-01-02T16:04:05"}
``graphene.Time``
^^^^^^^^^^^^^^^^^
Represents a Time value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_. Represents a Time value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_.
.. code:: python ``graphene.types.json.JSONString``
import datetime
from graphene import Schema, ObjectType, Time
class Query(ObjectType):
one_hour_from = Time(required=True, time_input=Time(required=True))
def resolve_one_hour_from(root, info, time_input):
assert time_input == datetime.time(15, 4, 5)
tmp_time_input = datetime.datetime.combine(datetime.date(1, 1, 1), time_input)
return (tmp_time_input + datetime.timedelta(hours=1)).time()
schema = Schema(query=Query)
results = schema.execute("""
query {
oneHourFrom(timeInput: "15:04:05")
}
""")
assert results.data == {"oneHourFrom": "16:04:05"}
``graphene.Decimal``
^^^^^^^^^^^^^^^^^^^^
Represents a Python Decimal value.
.. code:: python
import decimal
from graphene import Schema, ObjectType, Decimal
class Query(ObjectType):
add_one_to = Decimal(required=True, decimal_input=Decimal(required=True))
def resolve_add_one_to(root, info, decimal_input):
assert decimal_input == decimal.Decimal("10.50")
return decimal_input + decimal.Decimal("1")
schema = Schema(query=Query)
results = schema.execute("""
query {
addOneTo(decimalInput: "10.50")
}
""")
assert results.data == {"addOneTo": "11.50"}
``graphene.JSONString``
^^^^^^^^^^^^^^^^^^^^^^^
Represents a JSON string. Represents a JSON string.
.. code:: python
from graphene import Schema, ObjectType, JSONString, String
class Query(ObjectType):
update_json_key = JSONString(
required=True,
json_input=JSONString(required=True),
key=String(required=True),
value=String(required=True)
)
def resolve_update_json_key(root, info, json_input, key, value):
assert json_input == {"name": "Jane"}
json_input[key] = value
return json_input
schema = Schema(query=Query)
results = schema.execute("""
query {
updateJsonKey(jsonInput: "{\\"name\\": \\"Jane\\"}", key: "name", value: "Beth")
}
""")
assert results.data == {"updateJsonKey": "{\"name\": \"Beth\"}"}
``graphene.Base64``
^^^^^^^^^^^^^^^^^^^
Represents a Base64 encoded string.
.. code:: python
from graphene import Schema, ObjectType, Base64
class Query(ObjectType):
increment_encoded_id = Base64(
required=True,
base64_input=Base64(required=True),
)
def resolve_increment_encoded_id(root, info, base64_input):
assert base64_input == "4"
return int(base64_input) + 1
schema = Schema(query=Query)
results = schema.execute("""
query {
incrementEncodedId(base64Input: "NA==")
}
""")
assert results.data == {"incrementEncodedId": "NQ=="}
Custom scalars Custom scalars
-------------- --------------
@ -270,8 +100,8 @@ The following is an example for creating a DateTime scalar:
return dt.isoformat() return dt.isoformat()
@staticmethod @staticmethod
def parse_literal(node, _variables=None): def parse_literal(node):
if isinstance(node, ast.StringValueNode): if isinstance(node, ast.StringValue):
return datetime.datetime.strptime( return datetime.datetime.strptime(
node.value, "%Y-%m-%dT%H:%M:%S.%f") node.value, "%Y-%m-%dT%H:%M:%S.%f")

View File

@ -1,42 +1,16 @@
Schema Schema
====== ======
A GraphQL **Schema** defines the types and relationships between **Fields** in your API. A Schema is created by supplying the root types of each type of operation, query and mutation (optional).
A schema definition is then supplied to the validator and executor.
A Schema is created by supplying the root :ref:`ObjectType` of each operation, query (mandatory), mutation and subscription.
Schema will collect all type definitions related to the root operations and then supply them to the validator and executor.
.. code:: python .. code:: python
my_schema = Schema( my_schema = Schema(
query=MyRootQuery, query=MyRootQuery,
mutation=MyRootMutation, mutation=MyRootMutation,
subscription=MyRootSubscription
) )
A Root Query is just a special :ref:`ObjectType` that defines the fields that are the entrypoint for your API. Root Mutation and Root Subscription are similar to Root Query, but for different operation types:
* Query fetches data
* Mutation changes data and retrieves the changes
* Subscription sends changes to clients in real-time
Review the `GraphQL documentation on Schema`_ for a brief overview of fields, schema and operations.
.. _GraphQL documentation on Schema: https://graphql.org/learn/schema/
Querying
--------
To query a schema, call the ``execute`` method on it. See :ref:`SchemaExecute` for more details.
.. code:: python
query_string = 'query whoIsMyBestFriend { myBestFriend { lastName } }'
my_schema.execute(query_string)
Types Types
----- -----
@ -44,7 +18,7 @@ There are some cases where the schema cannot access all of the types that we pla
For example, when a field returns an ``Interface``, the schema doesn't know about any of the For example, when a field returns an ``Interface``, the schema doesn't know about any of the
implementations. implementations.
In this case, we need to use the ``types`` argument when creating the Schema: In this case, we need to use the ``types`` argument when creating the Schema.
.. code:: python .. code:: python
@ -54,16 +28,26 @@ In this case, we need to use the ``types`` argument when creating the Schema:
types=[SomeExtraObjectType, ] types=[SomeExtraObjectType, ]
) )
.. _SchemaAutoCamelCase:
Auto camelCase field names Querying
--------
To query a schema, call the ``execute`` method on it.
.. code:: python
my_schema.execute('{ lastName }')
Auto CamelCase field names
-------------------------- --------------------------
By default all field and argument names (that are not By default all field and argument names (that are not
explicitly set with the ``name`` arg) will be converted from explicitly set with the ``name`` arg) will be converted from
``snake_case`` to ``camelCase`` (as the API is usually being consumed by a js/mobile client) ``snake_case`` to ``camelCase`` (as the API is usually being consumed by a js/mobile client)
For example with the ObjectType the ``last_name`` field name is converted to ``lastName``: For example with the ObjectType
.. code:: python .. code:: python
@ -71,10 +55,12 @@ For example with the ObjectType the ``last_name`` field name is converted to ``l
last_name = graphene.String() last_name = graphene.String()
other_name = graphene.String(name='_other_Name') other_name = graphene.String(name='_other_Name')
the ``last_name`` field name is converted to ``lastName``.
In case you don't want to apply this transformation, provide a ``name`` argument to the field constructor. In case you don't want to apply this transformation, provide a ``name`` argument to the field constructor.
``other_name`` converts to ``_other_Name`` (without further transformations). ``other_name`` converts to ``_other_Name`` (without further transformations).
Your query should look like: Your query should look like
.. code:: .. code::
@ -84,7 +70,7 @@ Your query should look like:
} }
To disable this behavior, set the ``auto_camelcase`` to ``False`` upon schema instantiation: To disable this behavior, set the ``auto_camelcase`` to ``False`` upon schema instantiation.
.. code:: python .. code:: python

View File

@ -7,7 +7,7 @@ to specify any common fields between the types.
The basics: The basics:
- Each Union is a Python class that inherits from ``graphene.Union``. - Each Union is a Python class that inherits from ``graphene.Union``.
- Unions don't have any fields on it, just links to the possible ObjectTypes. - Unions don't have any fields on it, just links to the possible objecttypes.
Quick example Quick example
------------- -------------

View File

@ -7,7 +7,7 @@ class GeoInput(graphene.InputObjectType):
@property @property
def latlng(self): def latlng(self):
return f"({self.lat},{self.lng})" return "({},{})".format(self.lat, self.lng)
class Address(graphene.ObjectType): class Address(graphene.ObjectType):
@ -17,7 +17,7 @@ class Address(graphene.ObjectType):
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
address = graphene.Field(Address, geo=GeoInput(required=True)) address = graphene.Field(Address, geo=GeoInput(required=True))
def resolve_address(root, info, geo): def resolve_address(self, info, geo):
return Address(latlng=geo.latlng) return Address(latlng=geo.latlng)
@ -27,7 +27,7 @@ class CreateAddress(graphene.Mutation):
Output = Address Output = Address
def mutate(root, info, geo): def mutate(self, info, geo):
return Address(latlng=geo.latlng) return Address(latlng=geo.latlng)

View File

@ -9,7 +9,7 @@ class User(graphene.ObjectType):
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
me = graphene.Field(User) me = graphene.Field(User)
def resolve_me(root, info): def resolve_me(self, info):
return info.context["user"] return info.context["user"]
@ -25,11 +25,11 @@ query = """
def test_query(): def test_query():
result = schema.execute(query, context={"user": User(id="1", name="Syrus")}) result = schema.execute(query, context_value={"user": User(id="1", name="Syrus")})
assert not result.errors assert not result.errors
assert result.data == {"me": {"id": "1", "name": "Syrus"}} assert result.data == {"me": {"id": "1", "name": "Syrus"}}
if __name__ == "__main__": if __name__ == "__main__":
result = schema.execute(query, context={"user": User(id="X", name="Console")}) result = schema.execute(query, context_value={"user": User(id="X", name="Console")})
print(result.data["me"]) print(result.data["me"])

View File

@ -8,9 +8,10 @@ class Patron(graphene.ObjectType):
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
patron = graphene.Field(Patron) patron = graphene.Field(Patron)
def resolve_patron(root, info): def resolve_patron(self, info):
return Patron(id=1, name="Syrus", age=27) return Patron(id=1, name="Syrus", age=27)

View File

@ -39,13 +39,13 @@ class Query(graphene.ObjectType):
human = graphene.Field(Human, id=graphene.String()) human = graphene.Field(Human, id=graphene.String())
droid = graphene.Field(Droid, id=graphene.String()) droid = graphene.Field(Droid, id=graphene.String())
def resolve_hero(root, info, episode=None): def resolve_hero(self, info, episode=None):
return get_hero(episode) return get_hero(episode)
def resolve_human(root, info, id): def resolve_human(self, info, id):
return get_human(id) return get_human(id)
def resolve_droid(root, info, id): def resolve_droid(self, info, id):
return get_droid(id) return get_droid(id)

View File

@ -0,0 +1,100 @@
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals
from snapshottest import Snapshot
snapshots = Snapshot()
snapshots["test_hero_name_query 1"] = {"data": {"hero": {"name": "R2-D2"}}}
snapshots["test_hero_name_and_friends_query 1"] = {
"data": {
"hero": {
"id": "2001",
"name": "R2-D2",
"friends": [
{"name": "Luke Skywalker"},
{"name": "Han Solo"},
{"name": "Leia Organa"},
],
}
}
}
snapshots["test_nested_query 1"] = {
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Han Solo"},
{"name": "Leia Organa"},
{"name": "C-3PO"},
{"name": "R2-D2"},
],
},
{
"name": "Han Solo",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Luke Skywalker"},
{"name": "Leia Organa"},
{"name": "R2-D2"},
],
},
{
"name": "Leia Organa",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Luke Skywalker"},
{"name": "Han Solo"},
{"name": "C-3PO"},
{"name": "R2-D2"},
],
},
],
}
}
}
snapshots["test_fetch_luke_query 1"] = {"data": {"human": {"name": "Luke Skywalker"}}}
snapshots["test_fetch_some_id_query 1"] = {
"data": {"human": {"name": "Luke Skywalker"}}
}
snapshots["test_fetch_some_id_query2 1"] = {"data": {"human": {"name": "Han Solo"}}}
snapshots["test_invalid_id_query 1"] = {"data": {"human": None}}
snapshots["test_fetch_luke_aliased 1"] = {"data": {"luke": {"name": "Luke Skywalker"}}}
snapshots["test_fetch_luke_and_leia_aliased 1"] = {
"data": {"luke": {"name": "Luke Skywalker"}, "leia": {"name": "Leia Organa"}}
}
snapshots["test_duplicate_fields 1"] = {
"data": {
"luke": {"name": "Luke Skywalker", "homePlanet": "Tatooine"},
"leia": {"name": "Leia Organa", "homePlanet": "Alderaan"},
}
}
snapshots["test_use_fragment 1"] = {
"data": {
"luke": {"name": "Luke Skywalker", "homePlanet": "Tatooine"},
"leia": {"name": "Leia Organa", "homePlanet": "Alderaan"},
}
}
snapshots["test_check_type_of_r2 1"] = {
"data": {"hero": {"__typename": "Droid", "name": "R2-D2"}}
}
snapshots["test_check_type_of_luke 1"] = {
"data": {"hero": {"__typename": "Human", "name": "Luke Skywalker"}}
}

View File

@ -8,19 +8,19 @@ setup()
client = Client(schema) client = Client(schema)
def test_hero_name_query(): def test_hero_name_query(snapshot):
result = client.execute(""" query = """
query HeroNameQuery { query HeroNameQuery {
hero { hero {
name name
} }
} }
""") """
assert result == {"data": {"hero": {"name": "R2-D2"}}} snapshot.assert_match(client.execute(query))
def test_hero_name_and_friends_query(): def test_hero_name_and_friends_query(snapshot):
result = client.execute(""" query = """
query HeroNameAndFriendsQuery { query HeroNameAndFriendsQuery {
hero { hero {
id id
@ -30,24 +30,12 @@ def test_hero_name_and_friends_query():
} }
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {
"hero": {
"id": "2001",
"name": "R2-D2",
"friends": [
{"name": "Luke Skywalker"},
{"name": "Han Solo"},
{"name": "Leia Organa"},
],
}
}
}
def test_nested_query(): def test_nested_query(snapshot):
result = client.execute(""" query = """
query NestedQuery { query NestedQuery {
hero { hero {
name name
@ -60,113 +48,70 @@ def test_nested_query():
} }
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Han Solo"},
{"name": "Leia Organa"},
{"name": "C-3PO"},
{"name": "R2-D2"},
],
},
{
"name": "Han Solo",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Luke Skywalker"},
{"name": "Leia Organa"},
{"name": "R2-D2"},
],
},
{
"name": "Leia Organa",
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
"friends": [
{"name": "Luke Skywalker"},
{"name": "Han Solo"},
{"name": "C-3PO"},
{"name": "R2-D2"},
],
},
],
}
}
}
def test_fetch_luke_query(): def test_fetch_luke_query(snapshot):
result = client.execute(""" query = """
query FetchLukeQuery { query FetchLukeQuery {
human(id: "1000") { human(id: "1000") {
name name
} }
} }
""") """
assert result == {"data": {"human": {"name": "Luke Skywalker"}}} snapshot.assert_match(client.execute(query))
def test_fetch_some_id_query(): def test_fetch_some_id_query(snapshot):
result = client.execute( query = """
"""
query FetchSomeIDQuery($someId: String!) { query FetchSomeIDQuery($someId: String!) {
human(id: $someId) { human(id: $someId) {
name name
} }
} }
""", """
variables={"someId": "1000"}, params = {"someId": "1000"}
) snapshot.assert_match(client.execute(query, variable_values=params))
assert result == {"data": {"human": {"name": "Luke Skywalker"}}}
def test_fetch_some_id_query2(): def test_fetch_some_id_query2(snapshot):
result = client.execute( query = """
"""
query FetchSomeIDQuery($someId: String!) { query FetchSomeIDQuery($someId: String!) {
human(id: $someId) { human(id: $someId) {
name name
} }
} }
""", """
variables={"someId": "1002"}, params = {"someId": "1002"}
) snapshot.assert_match(client.execute(query, variable_values=params))
assert result == {"data": {"human": {"name": "Han Solo"}}}
def test_invalid_id_query(): def test_invalid_id_query(snapshot):
result = client.execute( query = """
"""
query humanQuery($id: String!) { query humanQuery($id: String!) {
human(id: $id) { human(id: $id) {
name name
} }
} }
""", """
variables={"id": "not a valid id"}, params = {"id": "not a valid id"}
) snapshot.assert_match(client.execute(query, variable_values=params))
assert result == {"data": {"human": None}}
def test_fetch_luke_aliased(): def test_fetch_luke_aliased(snapshot):
result = client.execute(""" query = """
query FetchLukeAliased { query FetchLukeAliased {
luke: human(id: "1000") { luke: human(id: "1000") {
name name
} }
} }
""") """
assert result == {"data": {"luke": {"name": "Luke Skywalker"}}} snapshot.assert_match(client.execute(query))
def test_fetch_luke_and_leia_aliased(): def test_fetch_luke_and_leia_aliased(snapshot):
result = client.execute(""" query = """
query FetchLukeAndLeiaAliased { query FetchLukeAndLeiaAliased {
luke: human(id: "1000") { luke: human(id: "1000") {
name name
@ -175,14 +120,12 @@ def test_fetch_luke_and_leia_aliased():
name name
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {"luke": {"name": "Luke Skywalker"}, "leia": {"name": "Leia Organa"}}
}
def test_duplicate_fields(): def test_duplicate_fields(snapshot):
result = client.execute(""" query = """
query DuplicateFields { query DuplicateFields {
luke: human(id: "1000") { luke: human(id: "1000") {
name name
@ -193,17 +136,12 @@ def test_duplicate_fields():
homePlanet homePlanet
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {
"luke": {"name": "Luke Skywalker", "homePlanet": "Tatooine"},
"leia": {"name": "Leia Organa", "homePlanet": "Alderaan"},
}
}
def test_use_fragment(): def test_use_fragment(snapshot):
result = client.execute(""" query = """
query UseFragment { query UseFragment {
luke: human(id: "1000") { luke: human(id: "1000") {
...HumanFragment ...HumanFragment
@ -216,36 +154,29 @@ def test_use_fragment():
name name
homePlanet homePlanet
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {
"luke": {"name": "Luke Skywalker", "homePlanet": "Tatooine"},
"leia": {"name": "Leia Organa", "homePlanet": "Alderaan"},
}
}
def test_check_type_of_r2(): def test_check_type_of_r2(snapshot):
result = client.execute(""" query = """
query CheckTypeOfR2 { query CheckTypeOfR2 {
hero { hero {
__typename __typename
name name
} }
} }
""") """
assert result == {"data": {"hero": {"__typename": "Droid", "name": "R2-D2"}}} snapshot.assert_match(client.execute(query))
def test_check_type_of_luke(): def test_check_type_of_luke(snapshot):
result = client.execute(""" query = """
query CheckTypeOfLuke { query CheckTypeOfLuke {
hero(episode: EMPIRE) { hero(episode: EMPIRE) {
__typename __typename
name name
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {"hero": {"__typename": "Human", "name": "Luke Skywalker"}}
}

View File

@ -14,7 +14,7 @@ def setup():
# Yeah, technically it's Corellian. But it flew in the service of the rebels, # Yeah, technically it's Corellian. But it flew in the service of the rebels,
# so for the purposes of this demo it's a rebel ship. # so for the purposes of this demo it's a rebel ship.
falcon = Ship(id="4", name="Millennium Falcon") falcon = Ship(id="4", name="Millenium Falcon")
homeOne = Ship(id="5", name="Home One") homeOne = Ship(id="5", name="Home One")

View File

@ -64,10 +64,10 @@ class Query(graphene.ObjectType):
empire = graphene.Field(Faction) empire = graphene.Field(Faction)
node = relay.Node.Field() node = relay.Node.Field()
def resolve_rebels(root, info): def resolve_rebels(self, info):
return get_rebels() return get_rebels()
def resolve_empire(root, info): def resolve_empire(self, info):
return get_empire() return get_empire()

View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals
from snapshottest import Snapshot
snapshots = Snapshot()
snapshots["test_correct_fetch_first_ship_rebels 1"] = {
"data": {
"rebels": {
"name": "Alliance to Restore the Republic",
"ships": {
"pageInfo": {
"startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
"endCursor": "YXJyYXljb25uZWN0aW9uOjA=",
"hasNextPage": True,
"hasPreviousPage": False,
},
"edges": [
{"cursor": "YXJyYXljb25uZWN0aW9uOjA=", "node": {"name": "X-Wing"}}
],
},
}
}
}

View File

@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals
from snapshottest import Snapshot
snapshots = Snapshot()
snapshots["test_mutations 1"] = {
"data": {
"introduceShip": {
"ship": {"id": "U2hpcDo5", "name": "Peter"},
"faction": {
"name": "Alliance to Restore the Republic",
"ships": {
"edges": [
{"node": {"id": "U2hpcDox", "name": "X-Wing"}},
{"node": {"id": "U2hpcDoy", "name": "Y-Wing"}},
{"node": {"id": "U2hpcDoz", "name": "A-Wing"}},
{"node": {"id": "U2hpcDo0", "name": "Millenium Falcon"}},
{"node": {"id": "U2hpcDo1", "name": "Home One"}},
{"node": {"id": "U2hpcDo5", "name": "Peter"}},
]
},
},
}
}
}

View File

@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals
from snapshottest import Snapshot
snapshots = Snapshot()
snapshots["test_correctly_fetches_id_name_rebels 1"] = {
"data": {
"rebels": {"id": "RmFjdGlvbjox", "name": "Alliance to Restore the Republic"}
}
}
snapshots["test_correctly_refetches_rebels 1"] = {
"data": {"node": {"id": "RmFjdGlvbjox", "name": "Alliance to Restore the Republic"}}
}
snapshots["test_correctly_fetches_id_name_empire 1"] = {
"data": {"empire": {"id": "RmFjdGlvbjoy", "name": "Galactic Empire"}}
}
snapshots["test_correctly_refetches_empire 1"] = {
"data": {"node": {"id": "RmFjdGlvbjoy", "name": "Galactic Empire"}}
}
snapshots["test_correctly_refetches_xwing 1"] = {
"data": {"node": {"id": "U2hpcDox", "name": "X-Wing"}}
}
snapshots[
"test_str_schema 1"
] = """schema {
query: Query
mutation: Mutation
}
type Faction implements Node {
id: ID!
name: String
ships(before: String, after: String, first: Int, last: Int): ShipConnection
}
input IntroduceShipInput {
shipName: String!
factionId: String!
clientMutationId: String
}
type IntroduceShipPayload {
ship: Ship
faction: Faction
clientMutationId: String
}
type Mutation {
introduceShip(input: IntroduceShipInput!): IntroduceShipPayload
}
interface Node {
id: ID!
}
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
endCursor: String
}
type Query {
rebels: Faction
empire: Faction
node(id: ID!): Node
}
type Ship implements Node {
id: ID!
name: String
}
type ShipConnection {
pageInfo: PageInfo!
edges: [ShipEdge]!
}
type ShipEdge {
node: Ship
cursor: String!
}
"""

View File

@ -8,46 +8,26 @@ setup()
client = Client(schema) client = Client(schema)
def test_correct_fetch_first_ship_rebels(): def test_correct_fetch_first_ship_rebels(snapshot):
result = client.execute(""" query = """
query RebelsShipsQuery { query RebelsShipsQuery {
rebels { rebels {
name, name,
ships(first: 1) { ships(first: 1) {
pageInfo { pageInfo {
startCursor startCursor
endCursor endCursor
hasNextPage hasNextPage
hasPreviousPage hasPreviousPage
} }
edges { edges {
cursor cursor
node { node {
name name
}
}
} }
} }
} }
""") }
assert result == {
"data": {
"rebels": {
"name": "Alliance to Restore the Republic",
"ships": {
"pageInfo": {
"startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
"endCursor": "YXJyYXljb25uZWN0aW9uOjA=",
"hasNextPage": True,
"hasPreviousPage": False,
},
"edges": [
{
"cursor": "YXJyYXljb25uZWN0aW9uOjA=",
"node": {"name": "X-Wing"},
}
],
},
}
}
} }
"""
snapshot.assert_match(client.execute(query))

View File

@ -8,45 +8,26 @@ setup()
client = Client(schema) client = Client(schema)
def test_mutations(): def test_mutations(snapshot):
result = client.execute(""" query = """
mutation MyMutation { mutation MyMutation {
introduceShip(input:{clientMutationId:"abc", shipName: "Peter", factionId: "1"}) { introduceShip(input:{clientMutationId:"abc", shipName: "Peter", factionId: "1"}) {
ship { ship {
id id
name name
} }
faction { faction {
name name
ships { ships {
edges { edges {
node { node {
id id
name name
}
}
} }
} }
} }
} }
""") }
assert result == {
"data": {
"introduceShip": {
"ship": {"id": "U2hpcDo5", "name": "Peter"},
"faction": {
"name": "Alliance to Restore the Republic",
"ships": {
"edges": [
{"node": {"id": "U2hpcDox", "name": "X-Wing"}},
{"node": {"id": "U2hpcDoy", "name": "Y-Wing"}},
{"node": {"id": "U2hpcDoz", "name": "A-Wing"}},
{"node": {"id": "U2hpcDo0", "name": "Millennium Falcon"}},
{"node": {"id": "U2hpcDo1", "name": "Home One"}},
{"node": {"id": "U2hpcDo5", "name": "Peter"}},
]
},
},
}
}
} }
"""
snapshot.assert_match(client.execute(query))

View File

@ -1,5 +1,3 @@
import textwrap
from graphene.test import Client from graphene.test import Client
from ..data import setup from ..data import setup
@ -10,115 +8,24 @@ setup()
client = Client(schema) client = Client(schema)
def test_str_schema(): def test_str_schema(snapshot):
assert str(schema).strip() == textwrap.dedent( snapshot.assert_match(str(schema))
'''\
type Query {
rebels: Faction
empire: Faction
node(
"""The ID of the object"""
id: ID!
): Node
}
"""A faction in the Star Wars saga"""
type Faction implements Node {
"""The ID of the object"""
id: ID!
"""The name of the faction."""
name: String
"""The ships used by the faction."""
ships(before: String, after: String, first: Int, last: Int): ShipConnection
}
"""An object with an ID"""
interface Node {
"""The ID of the object"""
id: ID!
}
type ShipConnection {
"""Pagination data for this connection."""
pageInfo: PageInfo!
"""Contains the nodes in this connection."""
edges: [ShipEdge]!
}
"""
The Relay compliant `PageInfo` type, containing data necessary to paginate this connection.
"""
type PageInfo {
"""When paginating forwards, are there more items?"""
hasNextPage: Boolean!
"""When paginating backwards, are there more items?"""
hasPreviousPage: Boolean!
"""When paginating backwards, the cursor to continue."""
startCursor: String
"""When paginating forwards, the cursor to continue."""
endCursor: String
}
"""A Relay edge containing a `Ship` and its cursor."""
type ShipEdge {
"""The item at the end of the edge"""
node: Ship
"""A cursor for use in pagination"""
cursor: String!
}
"""A ship in the Star Wars saga"""
type Ship implements Node {
"""The ID of the object"""
id: ID!
"""The name of the ship."""
name: String
}
type Mutation {
introduceShip(input: IntroduceShipInput!): IntroduceShipPayload
}
type IntroduceShipPayload {
ship: Ship
faction: Faction
clientMutationId: String
}
input IntroduceShipInput {
shipName: String!
factionId: String!
clientMutationId: String
}'''
)
def test_correctly_fetches_id_name_rebels(): def test_correctly_fetches_id_name_rebels(snapshot):
result = client.execute(""" query = """
query RebelsQuery { query RebelsQuery {
rebels { rebels {
id id
name name
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {
"rebels": {"id": "RmFjdGlvbjox", "name": "Alliance to Restore the Republic"}
}
}
def test_correctly_refetches_rebels(): def test_correctly_refetches_rebels(snapshot):
result = client.execute(""" query = """
query RebelsRefetchQuery { query RebelsRefetchQuery {
node(id: "RmFjdGlvbjox") { node(id: "RmFjdGlvbjox") {
id id
@ -127,30 +34,24 @@ def test_correctly_refetches_rebels():
} }
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {
"node": {"id": "RmFjdGlvbjox", "name": "Alliance to Restore the Republic"}
}
}
def test_correctly_fetches_id_name_empire(): def test_correctly_fetches_id_name_empire(snapshot):
result = client.execute(""" query = """
query EmpireQuery { query EmpireQuery {
empire { empire {
id id
name name
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {"empire": {"id": "RmFjdGlvbjoy", "name": "Galactic Empire"}}
}
def test_correctly_refetches_empire(): def test_correctly_refetches_empire(snapshot):
result = client.execute(""" query = """
query EmpireRefetchQuery { query EmpireRefetchQuery {
node(id: "RmFjdGlvbjoy") { node(id: "RmFjdGlvbjoy") {
id id
@ -159,14 +60,12 @@ def test_correctly_refetches_empire():
} }
} }
} }
""") """
assert result == { snapshot.assert_match(client.execute(query))
"data": {"node": {"id": "RmFjdGlvbjoy", "name": "Galactic Empire"}}
}
def test_correctly_refetches_xwing(): def test_correctly_refetches_xwing(snapshot):
result = client.execute(""" query = """
query XWingRefetchQuery { query XWingRefetchQuery {
node(id: "U2hpcDox") { node(id: "U2hpcDox") {
id id
@ -175,5 +74,5 @@ def test_correctly_refetches_xwing():
} }
} }
} }
""") """
assert result == {"data": {"node": {"id": "U2hpcDox", "name": "X-Wing"}}} snapshot.assert_match(client.execute(query))

View File

@ -1,98 +1,88 @@
from .pyutils.version import get_version from .pyutils.version import get_version
from .types import (
AbstractType,
ObjectType,
InputObjectType,
Interface,
Mutation,
Field,
InputField,
Schema,
Scalar,
String,
ID,
Int,
Float,
Boolean,
Date,
DateTime,
Time,
JSONString,
UUID,
List,
NonNull,
Enum,
Argument,
Dynamic,
Union,
Context,
ResolveInfo,
)
from .relay import ( from .relay import (
BaseGlobalIDType, Node,
is_node,
GlobalID,
ClientIDMutation, ClientIDMutation,
Connection, Connection,
ConnectionField, ConnectionField,
DefaultGlobalIDType,
GlobalID,
Node,
PageInfo, PageInfo,
SimpleGlobalIDType,
UUIDGlobalIDType,
is_node,
) )
from .types import (
ID,
UUID,
Argument,
Base64,
BigInt,
Boolean,
Context,
Date,
DateTime,
Decimal,
Dynamic,
Enum,
Field,
Float,
InputField,
InputObjectType,
Int,
Interface,
JSONString,
List,
Mutation,
NonNull,
ObjectType,
ResolveInfo,
Scalar,
Schema,
String,
Time,
Union,
)
from .utils.module_loading import lazy_import
from .utils.resolve_only_args import resolve_only_args from .utils.resolve_only_args import resolve_only_args
from .utils.module_loading import lazy_import
VERSION = (3, 4, 3, "final", 0)
VERSION = (2, 1, 3, "final", 0)
__version__ = get_version(VERSION) __version__ = get_version(VERSION)
__all__ = [ __all__ = [
"__version__", "__version__",
"Argument", "ObjectType",
"Base64", "InputObjectType",
"BigInt", "Interface",
"BaseGlobalIDType", "Mutation",
"Field",
"InputField",
"Schema",
"Scalar",
"String",
"ID",
"Int",
"Float",
"Enum",
"Boolean", "Boolean",
"Date",
"DateTime",
"Time",
"JSONString",
"UUID",
"List",
"NonNull",
"Argument",
"Dynamic",
"Union",
"resolve_only_args",
"Node",
"is_node",
"GlobalID",
"ClientIDMutation", "ClientIDMutation",
"Connection", "Connection",
"ConnectionField", "ConnectionField",
"Context",
"Date",
"DateTime",
"Decimal",
"DefaultGlobalIDType",
"Dynamic",
"Enum",
"Field",
"Float",
"GlobalID",
"ID",
"InputField",
"InputObjectType",
"Int",
"Interface",
"JSONString",
"List",
"Mutation",
"Node",
"NonNull",
"ObjectType",
"PageInfo", "PageInfo",
"ResolveInfo",
"Scalar",
"Schema",
"SimpleGlobalIDType",
"String",
"Time",
"Union",
"UUID",
"UUIDGlobalIDType",
"is_node",
"lazy_import", "lazy_import",
"resolve_only_args", "Context",
"ResolveInfo",
# Deprecated
"AbstractType",
] ]

View File

@ -0,0 +1,21 @@
from __future__ import absolute_import
import six
from graphql.pyutils.compat import Enum
try:
from inspect import signature
except ImportError:
from .signature import signature
if six.PY2:
def func_name(func):
return func.func_name
else:
def func_name(func):
return func.__name__

View File

@ -0,0 +1,23 @@
is_init_subclass_available = hasattr(object, "__init_subclass__")
if not is_init_subclass_available:
class InitSubclassMeta(type):
"""Metaclass that implements PEP 487 protocol"""
def __new__(cls, name, bases, ns, **kwargs):
__init_subclass__ = ns.pop("__init_subclass__", None)
if __init_subclass__:
__init_subclass__ = classmethod(__init_subclass__)
ns["__init_subclass__"] = __init_subclass__
return super(InitSubclassMeta, cls).__new__(cls, name, bases, ns, **kwargs)
def __init__(cls, name, bases, ns, **kwargs):
super(InitSubclassMeta, cls).__init__(name, bases, ns)
super_class = super(cls, cls)
if hasattr(super_class, "__init_subclass__"):
super_class.__init_subclass__.__func__(cls, **kwargs)
else:
InitSubclassMeta = type # type: ignore

View File

@ -0,0 +1,847 @@
# Copyright 2001-2013 Python Software Foundation; All Rights Reserved
"""Function signature objects for callables
Back port of Python 3.3's function signature tools from the inspect module,
modified to be compatible with Python 2.7 and 3.2+.
"""
from __future__ import absolute_import, division, print_function
import functools
import itertools
import re
import types
from collections import OrderedDict
__version__ = "0.4"
__all__ = ["BoundArguments", "Parameter", "Signature", "signature"]
_WrapperDescriptor = type(type.__call__)
_MethodWrapper = type(all.__call__)
_NonUserDefinedCallables = (
_WrapperDescriptor,
_MethodWrapper,
types.BuiltinFunctionType,
)
def formatannotation(annotation, base_module=None):
if isinstance(annotation, type):
if annotation.__module__ in ("builtins", "__builtin__", base_module):
return annotation.__name__
return annotation.__module__ + "." + annotation.__name__
return repr(annotation)
def _get_user_defined_method(cls, method_name, *nested):
try:
if cls is type:
return
meth = getattr(cls, method_name)
for name in nested:
meth = getattr(meth, name, meth)
except AttributeError:
return
else:
if not isinstance(meth, _NonUserDefinedCallables):
# Once '__signature__' will be added to 'C'-level
# callables, this check won't be necessary
return meth
def signature(obj):
"""Get a signature object for the passed callable."""
if not callable(obj):
raise TypeError("{!r} is not a callable object".format(obj))
if isinstance(obj, types.MethodType):
sig = signature(obj.__func__)
if obj.__self__ is None:
# Unbound method: the first parameter becomes positional-only
if sig.parameters:
first = sig.parameters.values()[0].replace(kind=_POSITIONAL_ONLY)
return sig.replace(
parameters=(first,) + tuple(sig.parameters.values())[1:]
)
else:
return sig
else:
# In this case we skip the first parameter of the underlying
# function (usually `self` or `cls`).
return sig.replace(parameters=tuple(sig.parameters.values())[1:])
try:
sig = obj.__signature__
except AttributeError:
pass
else:
if sig is not None:
return sig
try:
# Was this function wrapped by a decorator?
wrapped = obj.__wrapped__
except AttributeError:
pass
else:
return signature(wrapped)
if isinstance(obj, types.FunctionType):
return Signature.from_function(obj)
if isinstance(obj, functools.partial):
sig = signature(obj.func)
new_params = OrderedDict(sig.parameters.items())
partial_args = obj.args or ()
partial_keywords = obj.keywords or {}
try:
ba = sig.bind_partial(*partial_args, **partial_keywords)
except TypeError as ex:
msg = "partial object {!r} has incorrect arguments".format(obj)
raise ValueError(msg)
for arg_name, arg_value in ba.arguments.items():
param = new_params[arg_name]
if arg_name in partial_keywords:
# We set a new default value, because the following code
# is correct:
#
# >>> def foo(a): print(a)
# >>> print(partial(partial(foo, a=10), a=20)())
# 20
# >>> print(partial(partial(foo, a=10), a=20)(a=30))
# 30
#
# So, with 'partial' objects, passing a keyword argument is
# like setting a new default value for the corresponding
# parameter
#
# We also mark this parameter with '_partial_kwarg'
# flag. Later, in '_bind', the 'default' value of this
# parameter will be added to 'kwargs', to simulate
# the 'functools.partial' real call.
new_params[arg_name] = param.replace(
default=arg_value, _partial_kwarg=True
)
elif (
param.kind not in (_VAR_KEYWORD, _VAR_POSITIONAL)
and not param._partial_kwarg
):
new_params.pop(arg_name)
return sig.replace(parameters=new_params.values())
sig = None
if isinstance(obj, type):
# obj is a class or a metaclass
# First, let's see if it has an overloaded __call__ defined
# in its metaclass
call = _get_user_defined_method(type(obj), "__call__")
if call is not None:
sig = signature(call)
else:
# Now we check if the 'obj' class has a '__new__' method
new = _get_user_defined_method(obj, "__new__")
if new is not None:
sig = signature(new)
else:
# Finally, we should have at least __init__ implemented
init = _get_user_defined_method(obj, "__init__")
if init is not None:
sig = signature(init)
elif not isinstance(obj, _NonUserDefinedCallables):
# An object with __call__
# We also check that the 'obj' is not an instance of
# _WrapperDescriptor or _MethodWrapper to avoid
# infinite recursion (and even potential segfault)
call = _get_user_defined_method(type(obj), "__call__", "im_func")
if call is not None:
sig = signature(call)
if sig is not None:
# For classes and objects we skip the first parameter of their
# __call__, __new__, or __init__ methods
return sig.replace(parameters=tuple(sig.parameters.values())[1:])
if isinstance(obj, types.BuiltinFunctionType):
# Raise a nicer error message for builtins
msg = "no signature found for builtin function {!r}".format(obj)
raise ValueError(msg)
raise ValueError("callable {!r} is not supported by signature".format(obj))
class _void(object):
"""A private marker - used in Parameter & Signature"""
class _empty(object):
pass
class _ParameterKind(int):
def __new__(self, *args, **kwargs):
obj = int.__new__(self, *args)
obj._name = kwargs["name"]
return obj
def __str__(self):
return self._name
def __repr__(self):
return "<_ParameterKind: {!r}>".format(self._name)
_POSITIONAL_ONLY = _ParameterKind(0, name="POSITIONAL_ONLY")
_POSITIONAL_OR_KEYWORD = _ParameterKind(1, name="POSITIONAL_OR_KEYWORD")
_VAR_POSITIONAL = _ParameterKind(2, name="VAR_POSITIONAL")
_KEYWORD_ONLY = _ParameterKind(3, name="KEYWORD_ONLY")
_VAR_KEYWORD = _ParameterKind(4, name="VAR_KEYWORD")
class Parameter(object):
"""Represents a parameter in a function signature.
Has the following public attributes:
* name : str
The name of the parameter as a string.
* default : object
The default value for the parameter if specified. If the
parameter has no default value, this attribute is not set.
* annotation
The annotation for the parameter if specified. If the
parameter has no annotation, this attribute is not set.
* kind : str
Describes how argument values are bound to the parameter.
Possible values: `Parameter.POSITIONAL_ONLY`,
`Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
`Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
"""
__slots__ = ("_name", "_kind", "_default", "_annotation", "_partial_kwarg")
POSITIONAL_ONLY = _POSITIONAL_ONLY
POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
VAR_POSITIONAL = _VAR_POSITIONAL
KEYWORD_ONLY = _KEYWORD_ONLY
VAR_KEYWORD = _VAR_KEYWORD
empty = _empty
def __init__(
self, name, kind, default=_empty, annotation=_empty, _partial_kwarg=False
):
if kind not in (
_POSITIONAL_ONLY,
_POSITIONAL_OR_KEYWORD,
_VAR_POSITIONAL,
_KEYWORD_ONLY,
_VAR_KEYWORD,
):
raise ValueError("invalid value for 'Parameter.kind' attribute")
self._kind = kind
if default is not _empty:
if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
msg = "{} parameters cannot have default values".format(kind)
raise ValueError(msg)
self._default = default
self._annotation = annotation
if name is None:
if kind != _POSITIONAL_ONLY:
raise ValueError(
"None is not a valid name for a " "non-positional-only parameter"
)
self._name = name
else:
name = str(name)
if kind != _POSITIONAL_ONLY and not re.match(r"[a-z_]\w*$", name, re.I):
msg = "{!r} is not a valid parameter name".format(name)
raise ValueError(msg)
self._name = name
self._partial_kwarg = _partial_kwarg
@property
def name(self):
return self._name
@property
def default(self):
return self._default
@property
def annotation(self):
return self._annotation
@property
def kind(self):
return self._kind
def replace(
self,
name=_void,
kind=_void,
annotation=_void,
default=_void,
_partial_kwarg=_void,
):
"""Creates a customized copy of the Parameter."""
if name is _void:
name = self._name
if kind is _void:
kind = self._kind
if annotation is _void:
annotation = self._annotation
if default is _void:
default = self._default
if _partial_kwarg is _void:
_partial_kwarg = self._partial_kwarg
return type(self)(
name,
kind,
default=default,
annotation=annotation,
_partial_kwarg=_partial_kwarg,
)
def __str__(self):
kind = self.kind
formatted = self._name
if kind == _POSITIONAL_ONLY:
if formatted is None:
formatted = ""
formatted = "<{}>".format(formatted)
# Add annotation and default value
if self._annotation is not _empty:
formatted = "{}:{}".format(formatted, formatannotation(self._annotation))
if self._default is not _empty:
formatted = "{}={}".format(formatted, repr(self._default))
if kind == _VAR_POSITIONAL:
formatted = "*" + formatted
elif kind == _VAR_KEYWORD:
formatted = "**" + formatted
return formatted
def __repr__(self):
return "<{} at {:#x} {!r}>".format(self.__class__.__name__, id(self), self.name)
def __hash__(self):
msg = "unhashable type: '{}'".format(self.__class__.__name__)
raise TypeError(msg)
def __eq__(self, other):
return (
issubclass(other.__class__, Parameter)
and self._name == other._name
and self._kind == other._kind
and self._default == other._default
and self._annotation == other._annotation
)
def __ne__(self, other):
return not self.__eq__(other)
class BoundArguments(object):
"""Result of `Signature.bind` call. Holds the mapping of arguments
to the function's parameters.
Has the following public attributes:
* arguments : OrderedDict
An ordered mutable mapping of parameters' names to arguments' values.
Does not contain arguments' default values.
* signature : Signature
The Signature object that created this instance.
* args : tuple
Tuple of positional arguments values.
* kwargs : dict
Dict of keyword arguments values.
"""
def __init__(self, signature, arguments):
self.arguments = arguments
self._signature = signature
@property
def signature(self):
return self._signature
@property
def args(self):
args = []
for param_name, param in self._signature.parameters.items():
if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or param._partial_kwarg:
# Keyword arguments mapped by 'functools.partial'
# (Parameter._partial_kwarg is True) are mapped
# in 'BoundArguments.kwargs', along with VAR_KEYWORD &
# KEYWORD_ONLY
break
try:
arg = self.arguments[param_name]
except KeyError:
# We're done here. Other arguments
# will be mapped in 'BoundArguments.kwargs'
break
else:
if param.kind == _VAR_POSITIONAL:
# *args
args.extend(arg)
else:
# plain argument
args.append(arg)
return tuple(args)
@property
def kwargs(self):
kwargs = {}
kwargs_started = False
for param_name, param in self._signature.parameters.items():
if not kwargs_started:
if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or param._partial_kwarg:
kwargs_started = True
else:
if param_name not in self.arguments:
kwargs_started = True
continue
if not kwargs_started:
continue
try:
arg = self.arguments[param_name]
except KeyError:
pass
else:
if param.kind == _VAR_KEYWORD:
# **kwargs
kwargs.update(arg)
else:
# plain keyword argument
kwargs[param_name] = arg
return kwargs
def __hash__(self):
msg = "unhashable type: '{}'".format(self.__class__.__name__)
raise TypeError(msg)
def __eq__(self, other):
return (
issubclass(other.__class__, BoundArguments)
and self.signature == other.signature
and self.arguments == other.arguments
)
def __ne__(self, other):
return not self.__eq__(other)
class Signature(object):
"""A Signature object represents the overall signature of a function.
It stores a Parameter object for each parameter accepted by the
function, as well as information specific to the function itself.
A Signature object has the following public attributes and methods:
* parameters : OrderedDict
An ordered mapping of parameters' names to the corresponding
Parameter objects (keyword-only arguments are in the same order
as listed in `code.co_varnames`).
* return_annotation : object
The annotation for the return type of the function if specified.
If the function has no annotation for its return type, this
attribute is not set.
* bind(*args, **kwargs) -> BoundArguments
Creates a mapping from positional and keyword arguments to
parameters.
* bind_partial(*args, **kwargs) -> BoundArguments
Creates a partial mapping from positional and keyword arguments
to parameters (simulating 'functools.partial' behavior.)
"""
__slots__ = ("_return_annotation", "_parameters")
_parameter_cls = Parameter
_bound_arguments_cls = BoundArguments
empty = _empty
def __init__(
self, parameters=None, return_annotation=_empty, __validate_parameters__=True
):
"""Constructs Signature from the given list of Parameter
objects and 'return_annotation'. All arguments are optional.
"""
if parameters is None:
params = OrderedDict()
else:
if __validate_parameters__:
params = OrderedDict()
top_kind = _POSITIONAL_ONLY
for idx, param in enumerate(parameters):
kind = param.kind
if kind < top_kind:
msg = "wrong parameter order: {0} before {1}"
msg = msg.format(top_kind, param.kind)
raise ValueError(msg)
else:
top_kind = kind
name = param.name
if name is None:
name = str(idx)
param = param.replace(name=name)
if name in params:
msg = "duplicate parameter name: {!r}".format(name)
raise ValueError(msg)
params[name] = param
else:
params = OrderedDict(((param.name, param) for param in parameters))
self._parameters = params
self._return_annotation = return_annotation
@classmethod
def from_function(cls, func):
"""Constructs Signature for the given python function"""
if not isinstance(func, types.FunctionType):
raise TypeError("{!r} is not a Python function".format(func))
Parameter = cls._parameter_cls
# Parameter information.
func_code = func.__code__
pos_count = func_code.co_argcount
arg_names = func_code.co_varnames
positional = tuple(arg_names[:pos_count])
keyword_only_count = getattr(func_code, "co_kwonlyargcount", 0)
keyword_only = arg_names[pos_count : (pos_count + keyword_only_count)]
annotations = getattr(func, "__annotations__", {})
defaults = func.__defaults__
kwdefaults = getattr(func, "__kwdefaults__", None)
if defaults:
pos_default_count = len(defaults)
else:
pos_default_count = 0
parameters = []
# Non-keyword-only parameters w/o defaults.
non_default_count = pos_count - pos_default_count
for name in positional[:non_default_count]:
annotation = annotations.get(name, _empty)
parameters.append(
Parameter(name, annotation=annotation, kind=_POSITIONAL_OR_KEYWORD)
)
# ... w/ defaults.
for offset, name in enumerate(positional[non_default_count:]):
annotation = annotations.get(name, _empty)
parameters.append(
Parameter(
name,
annotation=annotation,
kind=_POSITIONAL_OR_KEYWORD,
default=defaults[offset],
)
)
# *args
if func_code.co_flags & 0x04:
name = arg_names[pos_count + keyword_only_count]
annotation = annotations.get(name, _empty)
parameters.append(
Parameter(name, annotation=annotation, kind=_VAR_POSITIONAL)
)
# Keyword-only parameters.
for name in keyword_only:
default = _empty
if kwdefaults is not None:
default = kwdefaults.get(name, _empty)
annotation = annotations.get(name, _empty)
parameters.append(
Parameter(
name, annotation=annotation, kind=_KEYWORD_ONLY, default=default
)
)
# **kwargs
if func_code.co_flags & 0x08:
index = pos_count + keyword_only_count
if func_code.co_flags & 0x04:
index += 1
name = arg_names[index]
annotation = annotations.get(name, _empty)
parameters.append(Parameter(name, annotation=annotation, kind=_VAR_KEYWORD))
return cls(
parameters,
return_annotation=annotations.get("return", _empty),
__validate_parameters__=False,
)
@property
def parameters(self):
try:
return types.MappingProxyType(self._parameters)
except AttributeError:
return OrderedDict(self._parameters.items())
@property
def return_annotation(self):
return self._return_annotation
def replace(self, parameters=_void, return_annotation=_void):
"""Creates a customized copy of the Signature.
Pass 'parameters' and/or 'return_annotation' arguments
to override them in the new copy.
"""
if parameters is _void:
parameters = self.parameters.values()
if return_annotation is _void:
return_annotation = self._return_annotation
return type(self)(parameters, return_annotation=return_annotation)
def __hash__(self):
msg = "unhashable type: '{}'".format(self.__class__.__name__)
raise TypeError(msg)
def __eq__(self, other):
if (
not issubclass(type(other), Signature)
or self.return_annotation != other.return_annotation
or len(self.parameters) != len(other.parameters)
):
return False
other_positions = {
param: idx for idx, param in enumerate(other.parameters.keys())
}
for idx, (param_name, param) in enumerate(self.parameters.items()):
if param.kind == _KEYWORD_ONLY:
try:
other_param = other.parameters[param_name]
except KeyError:
return False
else:
if param != other_param:
return False
else:
try:
other_idx = other_positions[param_name]
except KeyError:
return False
else:
if idx != other_idx or param != other.parameters[param_name]:
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def _bind(self, args, kwargs, partial=False):
"""Private method. Don't use directly."""
arguments = OrderedDict()
parameters = iter(self.parameters.values())
parameters_ex = ()
arg_vals = iter(args)
if partial:
# Support for binding arguments to 'functools.partial' objects.
# See 'functools.partial' case in 'signature()' implementation
# for details.
for param_name, param in self.parameters.items():
if param._partial_kwarg and param_name not in kwargs:
# Simulating 'functools.partial' behavior
kwargs[param_name] = param.default
while True:
# Let's iterate through the positional arguments and corresponding
# parameters
try:
arg_val = next(arg_vals)
except StopIteration:
# No more positional arguments
try:
param = next(parameters)
except StopIteration:
# No more parameters. That's it. Just need to check that
# we have no `kwargs` after this while loop
break
else:
if param.kind == _VAR_POSITIONAL:
# That's OK, just empty *args. Let's start parsing
# kwargs
break
elif param.name in kwargs:
if param.kind == _POSITIONAL_ONLY:
msg = "{arg!r} parameter is positional only, " "but was passed as a keyword"
msg = msg.format(arg=param.name)
raise TypeError(msg)
parameters_ex = (param,)
break
elif param.kind == _VAR_KEYWORD or param.default is not _empty:
# That's fine too - we have a default value for this
# parameter. So, lets start parsing `kwargs`, starting
# with the current parameter
parameters_ex = (param,)
break
else:
if partial:
parameters_ex = (param,)
break
else:
msg = "{arg!r} parameter lacking default value"
msg = msg.format(arg=param.name)
raise TypeError(msg)
else:
# We have a positional argument to process
try:
param = next(parameters)
except StopIteration:
raise TypeError("too many positional arguments")
else:
if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
# Looks like we have no parameter for this positional
# argument
raise TypeError("too many positional arguments")
if param.kind == _VAR_POSITIONAL:
# We have an '*args'-like argument, let's fill it with
# all positional arguments we have left and move on to
# the next phase
values = [arg_val]
values.extend(arg_vals)
arguments[param.name] = tuple(values)
break
if param.name in kwargs:
raise TypeError(
"multiple values for argument "
"{arg!r}".format(arg=param.name)
)
arguments[param.name] = arg_val
# Now, we iterate through the remaining parameters to process
# keyword arguments
kwargs_param = None
for param in itertools.chain(parameters_ex, parameters):
if param.kind == _POSITIONAL_ONLY:
# This should never happen in case of a properly built
# Signature object (but let's have this check here
# to ensure correct behaviour just in case)
raise TypeError(
"{arg!r} parameter is positional only, "
"but was passed as a keyword".format(arg=param.name)
)
if param.kind == _VAR_KEYWORD:
# Memorize that we have a '**kwargs'-like parameter
kwargs_param = param
continue
param_name = param.name
try:
arg_val = kwargs.pop(param_name)
except KeyError:
# We have no value for this parameter. It's fine though,
# if it has a default value, or it is an '*args'-like
# parameter, left alone by the processing of positional
# arguments.
if (
not partial
and param.kind != _VAR_POSITIONAL
and param.default is _empty
):
raise TypeError(
"{arg!r} parameter lacking default value".format(arg=param_name)
)
else:
arguments[param_name] = arg_val
if kwargs:
if kwargs_param is not None:
# Process our '**kwargs'-like parameter
arguments[kwargs_param.name] = kwargs
else:
raise TypeError("too many keyword arguments")
return self._bound_arguments_cls(self, arguments)
def bind(self, *args, **kwargs):
"""Get a BoundArguments object, that maps the passed `args`
and `kwargs` to the function's signature. Raises `TypeError`
if the passed arguments can not be bound.
"""
return self._bind(args, kwargs)
def bind_partial(self, *args, **kwargs):
"""Get a BoundArguments object, that partially maps the
passed `args` and `kwargs` to the function's signature.
Raises `TypeError` if the passed arguments can not be bound.
"""
return self._bind(args, kwargs, partial=True)
def __str__(self):
result = []
render_kw_only_separator = True
for idx, param in enumerate(self.parameters.values()):
formatted = str(param)
kind = param.kind
if kind == _VAR_POSITIONAL:
# OK, we have an '*args'-like parameter, so we won't need
# a '*' to separate keyword-only arguments
render_kw_only_separator = False
elif kind == _KEYWORD_ONLY and render_kw_only_separator:
# We have a keyword-only parameter to render and we haven't
# rendered an '*args'-like parameter before, so add a '*'
# separator to the parameters list ("foo(arg1, *, arg2)" case)
result.append("*")
# This condition should be only triggered once, so
# reset the flag
render_kw_only_separator = False
result.append(formatted)
rendered = "({})".format(", ".join(result))
if self.return_annotation is not _empty:
anno = formatannotation(self.return_annotation)
rendered += " -> {}".format(anno)
return rendered

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
import datetime import datetime
import os import os
import subprocess import subprocess
@ -17,7 +19,10 @@ def get_version(version=None):
sub = "" sub = ""
if version[3] == "alpha" and version[4] == 0: if version[3] == "alpha" and version[4] == 0:
git_changeset = get_git_changeset() git_changeset = get_git_changeset()
sub = ".dev%s" % git_changeset if git_changeset else ".dev" if git_changeset:
sub = ".dev%s" % git_changeset
else:
sub = ".dev"
elif version[3] != "final": elif version[3] != "final":
mapping = {"alpha": "a", "beta": "b", "rc": "rc"} mapping = {"alpha": "a", "beta": "b", "rc": "rc"}
sub = mapping[version[3]] + str(version[4]) sub = mapping[version[3]] + str(version[4])
@ -71,6 +76,6 @@ def get_git_changeset():
) )
timestamp = git_log.communicate()[0] timestamp = git_log.communicate()[0]
timestamp = datetime.datetime.utcfromtimestamp(int(timestamp)) timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
except Exception: except:
return None return None
return timestamp.strftime("%Y%m%d%H%M%S") return timestamp.strftime("%Y%m%d%H%M%S")

View File

@ -1,23 +1,13 @@
from .node import Node, is_node, GlobalID from .node import Node, is_node, GlobalID
from .mutation import ClientIDMutation from .mutation import ClientIDMutation
from .connection import Connection, ConnectionField, PageInfo from .connection import Connection, ConnectionField, PageInfo
from .id_type import (
BaseGlobalIDType,
DefaultGlobalIDType,
SimpleGlobalIDType,
UUIDGlobalIDType,
)
__all__ = [ __all__ = [
"BaseGlobalIDType", "Node",
"is_node",
"GlobalID",
"ClientIDMutation", "ClientIDMutation",
"Connection", "Connection",
"ConnectionField", "ConnectionField",
"DefaultGlobalIDType",
"GlobalID",
"Node",
"PageInfo", "PageInfo",
"SimpleGlobalIDType",
"UUIDGlobalIDType",
"is_node",
] ]

View File

@ -1,51 +1,17 @@
import re import re
from collections.abc import Iterable from collections import Iterable, OrderedDict
from functools import partial from functools import partial
from typing import Type
from graphql_relay import connection_from_array from graphql_relay import connection_from_list
from promise import Promise, is_thenable
from ..types import Boolean, Enum, Int, Interface, List, NonNull, Scalar, String, Union from ..types import Boolean, Enum, Int, Interface, List, NonNull, Scalar, String, Union
from ..types.field import Field from ..types.field import Field
from ..types.objecttype import ObjectType, ObjectTypeOptions from ..types.objecttype import ObjectType, ObjectTypeOptions
from ..utils.thenables import maybe_thenable from .node import is_node
from .node import is_node, AbstractNode
def get_edge_class(
connection_class: Type["Connection"],
_node: Type[AbstractNode],
base_name: str,
strict_types: bool = False,
):
edge_class = getattr(connection_class, "Edge", None)
class EdgeBase:
node = Field(
NonNull(_node) if strict_types else _node,
description="The item at the end of the edge",
)
cursor = String(required=True, description="A cursor for use in pagination")
class EdgeMeta:
description = f"A Relay edge containing a `{base_name}` and its cursor."
edge_name = f"{base_name}Edge"
edge_bases = [edge_class, EdgeBase] if edge_class else [EdgeBase]
if not isinstance(edge_class, ObjectType):
edge_bases = [*edge_bases, ObjectType]
return type(edge_name, tuple(edge_bases), {"Meta": EdgeMeta})
class PageInfo(ObjectType): class PageInfo(ObjectType):
class Meta:
description = (
"The Relay compliant `PageInfo` type, containing data necessary to"
" paginate this connection."
)
has_next_page = Boolean( has_next_page = Boolean(
required=True, required=True,
name="hasNextPage", name="hasNextPage",
@ -69,17 +35,6 @@ class PageInfo(ObjectType):
) )
# noinspection PyPep8Naming
def page_info_adapter(startCursor, endCursor, hasPreviousPage, hasNextPage):
"""Adapter for creating PageInfo instances"""
return PageInfo(
start_cursor=startCursor,
end_cursor=endCursor,
has_previous_page=hasPreviousPage,
has_next_page=hasNextPage,
)
class ConnectionOptions(ObjectTypeOptions): class ConnectionOptions(ObjectTypeOptions):
node = None node = None
@ -89,79 +44,73 @@ class Connection(ObjectType):
abstract = True abstract = True
@classmethod @classmethod
def __init_subclass_with_meta__( def __init_subclass_with_meta__(cls, node=None, name=None, **options):
cls, node=None, name=None, strict_types=False, _meta=None, **options _meta = ConnectionOptions(cls)
): assert node, "You have to provide a node in {}.Meta".format(cls.__name__)
if not _meta: assert issubclass(
_meta = ConnectionOptions(cls)
assert node, f"You have to provide a node in {cls.__name__}.Meta"
assert isinstance(node, NonNull) or issubclass(
node, (Scalar, Enum, ObjectType, Interface, Union, NonNull) node, (Scalar, Enum, ObjectType, Interface, Union, NonNull)
), f'Received incompatible node "{node}" for Connection {cls.__name__}.' ), ('Received incompatible node "{}" for Connection {}.').format(
node, cls.__name__
)
base_name = re.sub("Connection$", "", name or cls.__name__) or node._meta.name base_name = re.sub("Connection$", "", name or cls.__name__) or node._meta.name
if not name: if not name:
name = f"{base_name}Connection" name = "{}Connection".format(base_name)
edge_class = getattr(cls, "Edge", None)
_node = node
class EdgeBase(object):
node = Field(_node, description="The item at the end of the edge")
cursor = String(required=True, description="A cursor for use in pagination")
edge_name = "{}Edge".format(base_name)
if edge_class:
edge_bases = (edge_class, EdgeBase, ObjectType)
else:
edge_bases = (EdgeBase, ObjectType)
edge = type(edge_name, edge_bases, {})
cls.Edge = edge
options["name"] = name options["name"] = name
_meta.node = node _meta.node = node
_meta.fields = OrderedDict(
if not _meta.fields: [
_meta.fields = {} ("page_info", Field(PageInfo, name="pageInfo", required=True)),
("edges", Field(NonNull(List(edge)))),
if "page_info" not in _meta.fields: ]
_meta.fields["page_info"] = Field( )
PageInfo,
name="pageInfo",
required=True,
description="Pagination data for this connection.",
)
if "edges" not in _meta.fields:
edge_class = get_edge_class(cls, node, base_name, strict_types) # type: ignore
cls.Edge = edge_class
_meta.fields["edges"] = Field(
NonNull(List(NonNull(edge_class) if strict_types else edge_class)),
description="Contains the nodes in this connection.",
)
return super(Connection, cls).__init_subclass_with_meta__( return super(Connection, cls).__init_subclass_with_meta__(
_meta=_meta, **options _meta=_meta, **options
) )
# noinspection PyPep8Naming
def connection_adapter(cls, edges, pageInfo):
"""Adapter for creating Connection instances"""
return cls(edges=edges, page_info=pageInfo)
class IterableConnectionField(Field): class IterableConnectionField(Field):
def __init__(self, type_, *args, **kwargs): def __init__(self, type, *args, **kwargs):
kwargs.setdefault("before", String()) kwargs.setdefault("before", String())
kwargs.setdefault("after", String()) kwargs.setdefault("after", String())
kwargs.setdefault("first", Int()) kwargs.setdefault("first", Int())
kwargs.setdefault("last", Int()) kwargs.setdefault("last", Int())
super(IterableConnectionField, self).__init__(type_, *args, **kwargs) super(IterableConnectionField, self).__init__(type, *args, **kwargs)
@property @property
def type(self): def type(self):
type_ = super(IterableConnectionField, self).type type = super(IterableConnectionField, self).type
connection_type = type_ connection_type = type
if isinstance(type_, NonNull): if isinstance(type, NonNull):
connection_type = type_.of_type connection_type = type.of_type
if is_node(connection_type): if is_node(connection_type):
raise Exception( raise Exception(
"ConnectionFields now need a explicit ConnectionType for Nodes.\n" "ConnectionField's now need a explicit ConnectionType for Nodes.\n"
"Read more: https://github.com/graphql-python/graphene/blob/v2.0.0/UPGRADE-v2.0.md#node-connections" "Read more: https://github.com/graphql-python/graphene/blob/v2.0.0/UPGRADE-v2.0.md#node-connections"
) )
assert issubclass( assert issubclass(connection_type, Connection), (
connection_type, Connection '{} type have to be a subclass of Connection. Received "{}".'
), f'{self.__class__.__name__} type has to be a subclass of Connection. Received "{connection_type}".' ).format(self.__class__.__name__, connection_type)
return type_ return type
@classmethod @classmethod
def resolve_connection(cls, connection_type, args, resolved): def resolve_connection(cls, connection_type, args, resolved):
@ -169,15 +118,15 @@ class IterableConnectionField(Field):
return resolved return resolved
assert isinstance(resolved, Iterable), ( assert isinstance(resolved, Iterable), (
f"Resolved value from the connection field has to be an iterable or instance of {connection_type}. " "Resolved value from the connection field have to be iterable or instance of {}. "
f'Received "{resolved}"' 'Received "{}"'
) ).format(connection_type, resolved)
connection = connection_from_array( connection = connection_from_list(
resolved, resolved,
args, args,
connection_type=partial(connection_adapter, connection_type), connection_type=connection_type,
edge_type=connection_type.Edge, edge_type=connection_type.Edge,
page_info_type=page_info_adapter, pageinfo_type=PageInfo,
) )
connection.iterable = resolved connection.iterable = resolved
return connection return connection
@ -190,10 +139,13 @@ class IterableConnectionField(Field):
connection_type = connection_type.of_type connection_type = connection_type.of_type
on_resolve = partial(cls.resolve_connection, connection_type, args) on_resolve = partial(cls.resolve_connection, connection_type, args)
return maybe_thenable(resolved, on_resolve) if is_thenable(resolved):
return Promise.resolve(resolved).then(on_resolve)
def wrap_resolve(self, parent_resolver): return on_resolve(resolved)
resolver = super(IterableConnectionField, self).wrap_resolve(parent_resolver)
def get_resolver(self, parent_resolver):
resolver = super(IterableConnectionField, self).get_resolver(parent_resolver)
return partial(self.connection_resolver, resolver, self.type) return partial(self.connection_resolver, resolver, self.type)

View File

@ -1,87 +0,0 @@
from graphql_relay import from_global_id, to_global_id
from ..types import ID, UUID
from ..types.base import BaseType
from typing import Type
class BaseGlobalIDType:
"""
Base class that define the required attributes/method for a type.
"""
graphene_type: Type[BaseType] = ID
@classmethod
def resolve_global_id(cls, info, global_id):
# return _type, _id
raise NotImplementedError
@classmethod
def to_global_id(cls, _type, _id):
# return _id
raise NotImplementedError
class DefaultGlobalIDType(BaseGlobalIDType):
"""
Default global ID type: base64 encoded version of "<node type name>: <node id>".
"""
graphene_type = ID
@classmethod
def resolve_global_id(cls, info, global_id):
try:
_type, _id = from_global_id(global_id)
if not _type:
raise ValueError("Invalid Global ID")
return _type, _id
except Exception as e:
raise Exception(
f'Unable to parse global ID "{global_id}". '
'Make sure it is a base64 encoded string in the format: "TypeName:id". '
f"Exception message: {e}"
)
@classmethod
def to_global_id(cls, _type, _id):
return to_global_id(_type, _id)
class SimpleGlobalIDType(BaseGlobalIDType):
"""
Simple global ID type: simply the id of the object.
To be used carefully as the user is responsible for ensuring that the IDs are indeed global
(otherwise it could cause request caching issues).
"""
graphene_type = ID
@classmethod
def resolve_global_id(cls, info, global_id):
_type = info.return_type.graphene_type._meta.name
return _type, global_id
@classmethod
def to_global_id(cls, _type, _id):
return _id
class UUIDGlobalIDType(BaseGlobalIDType):
"""
UUID global ID type.
By definition UUID are global so they are used as they are.
"""
graphene_type = UUID
@classmethod
def resolve_global_id(cls, info, global_id):
_type = info.return_type.graphene_type._meta.name
return _type, global_id
@classmethod
def to_global_id(cls, _type, _id):
return _id

View File

@ -1,8 +1,10 @@
import re import re
from collections import OrderedDict
from promise import Promise, is_thenable
from ..types import Field, InputObjectType, String from ..types import Field, InputObjectType, String
from ..types.mutation import Mutation from ..types.mutation import Mutation
from ..utils.thenables import maybe_thenable
class ClientIDMutation(Mutation): class ClientIDMutation(Mutation):
@ -27,24 +29,26 @@ class ClientIDMutation(Mutation):
input_fields = {} input_fields = {}
cls.Input = type( cls.Input = type(
f"{base_name}Input", "{}Input".format(base_name),
bases, bases,
dict(input_fields, client_mutation_id=String(name="clientMutationId")), OrderedDict(
input_fields, client_mutation_id=String(name="clientMutationId")
),
) )
arguments = dict( arguments = OrderedDict(
input=cls.Input(required=True) input=cls.Input(required=True)
# 'client_mutation_id': String(name='clientMutationId') # 'client_mutation_id': String(name='clientMutationId')
) )
mutate_and_get_payload = getattr(cls, "mutate_and_get_payload", None) mutate_and_get_payload = getattr(cls, "mutate_and_get_payload", None)
if cls.mutate and cls.mutate.__func__ == ClientIDMutation.mutate.__func__: if cls.mutate and cls.mutate.__func__ == ClientIDMutation.mutate.__func__:
assert mutate_and_get_payload, ( assert mutate_and_get_payload, (
f"{name or cls.__name__}.mutate_and_get_payload method is required" "{name}.mutate_and_get_payload method is required"
" in a ClientIDMutation." " in a ClientIDMutation."
) ).format(name=name or cls.__name__)
if not name: if not name:
name = f"{base_name}Payload" name = "{}Payload".format(base_name)
super(ClientIDMutation, cls).__init_subclass_with_meta__( super(ClientIDMutation, cls).__init_subclass_with_meta__(
output=None, arguments=arguments, name=name, **options output=None, arguments=arguments, name=name, **options
@ -58,9 +62,14 @@ class ClientIDMutation(Mutation):
payload.client_mutation_id = input.get("client_mutation_id") payload.client_mutation_id = input.get("client_mutation_id")
except Exception: except Exception:
raise Exception( raise Exception(
f"Cannot set client_mutation_id in the payload object {repr(payload)}" ("Cannot set client_mutation_id in the payload object {}").format(
repr(payload)
)
) )
return payload return payload
result = cls.mutate_and_get_payload(root, info, **input) result = cls.mutate_and_get_payload(root, info, **input)
return maybe_thenable(result, on_resolve) if is_thenable(result):
return Promise.resolve(result).then(on_resolve)
return on_resolve(result)

View File

@ -1,10 +1,12 @@
from collections import OrderedDict
from functools import partial from functools import partial
from inspect import isclass from inspect import isclass
from ..types import Field, Interface, ObjectType from graphql_relay import from_global_id, to_global_id
from ..types import ID, Field, Interface, ObjectType
from ..types.interface import InterfaceOptions from ..types.interface import InterfaceOptions
from ..types.utils import get_type from ..types.utils import get_type
from .id_type import BaseGlobalIDType, DefaultGlobalIDType
def is_node(objecttype): def is_node(objecttype):
@ -17,22 +19,16 @@ def is_node(objecttype):
if not issubclass(objecttype, ObjectType): if not issubclass(objecttype, ObjectType):
return False return False
return any(issubclass(i, Node) for i in objecttype._meta.interfaces) for i in objecttype._meta.interfaces:
if issubclass(i, Node):
return True
return False
class GlobalID(Field): class GlobalID(Field):
def __init__( def __init__(self, node=None, parent_type=None, required=True, *args, **kwargs):
self, super(GlobalID, self).__init__(ID, required=required, *args, **kwargs)
node=None,
parent_type=None,
required=True,
global_id_type=DefaultGlobalIDType,
*args,
**kwargs,
):
super(GlobalID, self).__init__(
global_id_type.graphene_type, required=required, *args, **kwargs
)
self.node = node or Node self.node = node or Node
self.parent_type_name = parent_type._meta.name if parent_type else None self.parent_type_name = parent_type._meta.name if parent_type else None
@ -42,7 +38,7 @@ class GlobalID(Field):
parent_type_name = parent_type_name or info.parent_type.name parent_type_name = parent_type_name or info.parent_type.name
return node.to_global_id(parent_type_name, type_id) # root._meta.name return node.to_global_id(parent_type_name, type_id) # root._meta.name
def wrap_resolve(self, parent_resolver): def get_resolver(self, parent_resolver):
return partial( return partial(
self.id_resolver, self.id_resolver,
parent_resolver, parent_resolver,
@ -52,22 +48,20 @@ class GlobalID(Field):
class NodeField(Field): class NodeField(Field):
def __init__(self, node, type_=False, **kwargs): def __init__(self, node, type=False, deprecation_reason=None, name=None, **kwargs):
assert issubclass(node, Node), "NodeField can only operate in Nodes" assert issubclass(node, Node), "NodeField can only operate in Nodes"
self.node_type = node self.node_type = node
self.field_type = type_ self.field_type = type
global_id_type = node._meta.global_id_type
super(NodeField, self).__init__( super(NodeField, self).__init__(
# If we don't specify a type, the field type will be the node interface # If we don's specify a type, the field type will be the node
type_ or node, # interface
id=global_id_type.graphene_type( type or node,
required=True, description="The ID of the object" description="The ID of the object",
), id=ID(required=True),
**kwargs,
) )
def wrap_resolve(self, parent_resolver): def get_resolver(self, parent_resolver):
return partial(self.node_type.node_resolver, get_type(self.field_type)) return partial(self.node_type.node_resolver, get_type(self.field_type))
@ -76,23 +70,13 @@ class AbstractNode(Interface):
abstract = True abstract = True
@classmethod @classmethod
def __init_subclass_with_meta__(cls, global_id_type=DefaultGlobalIDType, **options): def __init_subclass_with_meta__(cls, **options):
assert issubclass(
global_id_type, BaseGlobalIDType
), "Custom ID type need to be implemented as a subclass of BaseGlobalIDType."
_meta = InterfaceOptions(cls) _meta = InterfaceOptions(cls)
_meta.global_id_type = global_id_type _meta.fields = OrderedDict(
_meta.fields = { id=GlobalID(cls, description="The ID of the object.")
"id": GlobalID( )
cls, global_id_type=global_id_type, description="The ID of the object"
)
}
super(AbstractNode, cls).__init_subclass_with_meta__(_meta=_meta, **options) super(AbstractNode, cls).__init_subclass_with_meta__(_meta=_meta, **options)
@classmethod
def resolve_global_id(cls, info, global_id):
return cls._meta.global_id_type.resolve_global_id(info, global_id)
class Node(AbstractNode): class Node(AbstractNode):
"""An object with an ID""" """An object with an ID"""
@ -107,29 +91,29 @@ class Node(AbstractNode):
@classmethod @classmethod
def get_node_from_global_id(cls, info, global_id, only_type=None): def get_node_from_global_id(cls, info, global_id, only_type=None):
_type, _id = cls.resolve_global_id(info, global_id) try:
_type, _id = cls.from_global_id(global_id)
graphene_type = info.schema.get_type(_type) graphene_type = info.schema.get_type(_type).graphene_type
if graphene_type is None: except Exception:
raise Exception(f'Relay Node "{_type}" not found in schema') return None
graphene_type = graphene_type.graphene_type
if only_type: if only_type:
assert ( assert graphene_type == only_type, ("Must receive a {} id.").format(
graphene_type == only_type only_type._meta.name
), f"Must receive a {only_type._meta.name} id." )
# We make sure the ObjectType implements the "Node" interface # We make sure the ObjectType implements the "Node" interface
if cls not in graphene_type._meta.interfaces: if cls not in graphene_type._meta.interfaces:
raise Exception( return None
f'ObjectType "{_type}" does not implement the "{cls}" interface.'
)
get_node = getattr(graphene_type, "get_node", None) get_node = getattr(graphene_type, "get_node", None)
if get_node: if get_node:
return get_node(info, _id) return get_node(info, _id)
@classmethod @classmethod
def to_global_id(cls, type_, id): def from_global_id(cls, global_id):
return cls._meta.global_id_type.to_global_id(type_, id) return from_global_id(global_id)
@classmethod
def to_global_id(cls, type, id):
return to_global_id(type, id)

View File

@ -1,15 +1,7 @@
import re import pytest
from pytest import raises
from ...types import Argument, Field, Int, List, NonNull, ObjectType, Schema, String from ...types import Argument, Field, Int, List, NonNull, ObjectType, Schema, String
from ..connection import ( from ..connection import Connection, ConnectionField, PageInfo
Connection,
ConnectionField,
PageInfo,
ConnectionOptions,
get_edge_class,
)
from ..node import Node from ..node import Node
@ -32,7 +24,7 @@ def test_connection():
assert MyObjectConnection._meta.name == "MyObjectConnection" assert MyObjectConnection._meta.name == "MyObjectConnection"
fields = MyObjectConnection._meta.fields fields = MyObjectConnection._meta.fields
assert list(fields) == ["page_info", "edges", "extra"] assert list(fields.keys()) == ["page_info", "edges", "extra"]
edge_field = fields["edges"] edge_field = fields["edges"]
pageinfo_field = fields["page_info"] pageinfo_field = fields["page_info"]
@ -47,7 +39,7 @@ def test_connection():
def test_connection_inherit_abstracttype(): def test_connection_inherit_abstracttype():
class BaseConnection: class BaseConnection(object):
extra = String() extra = String()
class MyObjectConnection(BaseConnection, Connection): class MyObjectConnection(BaseConnection, Connection):
@ -56,118 +48,13 @@ def test_connection_inherit_abstracttype():
assert MyObjectConnection._meta.name == "MyObjectConnection" assert MyObjectConnection._meta.name == "MyObjectConnection"
fields = MyObjectConnection._meta.fields fields = MyObjectConnection._meta.fields
assert list(fields) == ["page_info", "edges", "extra"] assert list(fields.keys()) == ["page_info", "edges", "extra"]
def test_connection_extra_abstract_fields():
class ConnectionWithNodes(Connection):
class Meta:
abstract = True
@classmethod
def __init_subclass_with_meta__(cls, node=None, name=None, **options):
_meta = ConnectionOptions(cls)
_meta.fields = {
"nodes": Field(
NonNull(List(node)),
description="Contains all the nodes in this connection.",
),
}
return super(ConnectionWithNodes, cls).__init_subclass_with_meta__(
node=node, name=name, _meta=_meta, **options
)
class MyObjectConnection(ConnectionWithNodes):
class Meta:
node = MyObject
class Edge:
other = String()
assert MyObjectConnection._meta.name == "MyObjectConnection"
fields = MyObjectConnection._meta.fields
assert list(fields) == ["nodes", "page_info", "edges"]
edge_field = fields["edges"]
pageinfo_field = fields["page_info"]
nodes_field = fields["nodes"]
assert isinstance(edge_field, Field)
assert isinstance(edge_field.type, NonNull)
assert isinstance(edge_field.type.of_type, List)
assert edge_field.type.of_type.of_type == MyObjectConnection.Edge
assert isinstance(pageinfo_field, Field)
assert isinstance(pageinfo_field.type, NonNull)
assert pageinfo_field.type.of_type == PageInfo
assert isinstance(nodes_field, Field)
assert isinstance(nodes_field.type, NonNull)
assert isinstance(nodes_field.type.of_type, List)
assert nodes_field.type.of_type.of_type == MyObject
def test_connection_override_fields():
class ConnectionWithNodes(Connection):
class Meta:
abstract = True
@classmethod
def __init_subclass_with_meta__(cls, node=None, name=None, **options):
_meta = ConnectionOptions(cls)
base_name = (
re.sub("Connection$", "", name or cls.__name__) or node._meta.name
)
edge_class = get_edge_class(cls, node, base_name)
_meta.fields = {
"page_info": Field(
NonNull(
PageInfo,
name="pageInfo",
required=True,
description="Pagination data for this connection.",
)
),
"edges": Field(
NonNull(List(NonNull(edge_class))),
description="Contains the nodes in this connection.",
),
}
return super(ConnectionWithNodes, cls).__init_subclass_with_meta__(
node=node, name=name, _meta=_meta, **options
)
class MyObjectConnection(ConnectionWithNodes):
class Meta:
node = MyObject
assert MyObjectConnection._meta.name == "MyObjectConnection"
fields = MyObjectConnection._meta.fields
assert list(fields) == ["page_info", "edges"]
edge_field = fields["edges"]
pageinfo_field = fields["page_info"]
assert isinstance(edge_field, Field)
assert isinstance(edge_field.type, NonNull)
assert isinstance(edge_field.type.of_type, List)
assert isinstance(edge_field.type.of_type.of_type, NonNull)
assert edge_field.type.of_type.of_type.of_type.__name__ == "MyObjectEdge"
# This page info is NonNull
assert isinstance(pageinfo_field, Field)
assert isinstance(edge_field.type, NonNull)
assert pageinfo_field.type.of_type == PageInfo
def test_connection_name(): def test_connection_name():
custom_name = "MyObjectCustomNameConnection" custom_name = "MyObjectCustomNameConnection"
class BaseConnection: class BaseConnection(object):
extra = String() extra = String()
class MyObjectConnection(BaseConnection, Connection): class MyObjectConnection(BaseConnection, Connection):
@ -189,7 +76,7 @@ def test_edge():
Edge = MyObjectConnection.Edge Edge = MyObjectConnection.Edge
assert Edge._meta.name == "MyObjectEdge" assert Edge._meta.name == "MyObjectEdge"
edge_fields = Edge._meta.fields edge_fields = Edge._meta.fields
assert list(edge_fields) == ["node", "cursor", "other"] assert list(edge_fields.keys()) == ["node", "cursor", "other"]
assert isinstance(edge_fields["node"], Field) assert isinstance(edge_fields["node"], Field)
assert edge_fields["node"].type == MyObject assert edge_fields["node"].type == MyObject
@ -199,7 +86,7 @@ def test_edge():
def test_edge_with_bases(): def test_edge_with_bases():
class BaseEdge: class BaseEdge(object):
extra = String() extra = String()
class MyObjectConnection(Connection): class MyObjectConnection(Connection):
@ -212,7 +99,7 @@ def test_edge_with_bases():
Edge = MyObjectConnection.Edge Edge = MyObjectConnection.Edge
assert Edge._meta.name == "MyObjectEdge" assert Edge._meta.name == "MyObjectEdge"
edge_fields = Edge._meta.fields edge_fields = Edge._meta.fields
assert list(edge_fields) == ["node", "cursor", "extra", "other"] assert list(edge_fields.keys()) == ["node", "cursor", "extra", "other"]
assert isinstance(edge_fields["node"], Field) assert isinstance(edge_fields["node"], Field)
assert edge_fields["node"].type == MyObject assert edge_fields["node"].type == MyObject
@ -221,21 +108,10 @@ def test_edge_with_bases():
assert edge_fields["other"].type == String assert edge_fields["other"].type == String
def test_edge_with_nonnull_node():
class MyObjectConnection(Connection):
class Meta:
node = NonNull(MyObject)
edge_fields = MyObjectConnection.Edge._meta.fields
assert isinstance(edge_fields["node"], Field)
assert isinstance(edge_fields["node"].type, NonNull)
assert edge_fields["node"].type.of_type == MyObject
def test_pageinfo(): def test_pageinfo():
assert PageInfo._meta.name == "PageInfo" assert PageInfo._meta.name == "PageInfo"
fields = PageInfo._meta.fields fields = PageInfo._meta.fields
assert list(fields) == [ assert list(fields.keys()) == [
"has_next_page", "has_next_page",
"has_previous_page", "has_previous_page",
"start_cursor", "start_cursor",
@ -259,10 +135,10 @@ def test_connectionfield():
def test_connectionfield_node_deprecated(): def test_connectionfield_node_deprecated():
field = ConnectionField(MyObject) field = ConnectionField(MyObject)
with raises(Exception) as exc_info: with pytest.raises(Exception) as exc_info:
field.type field.type
assert "ConnectionFields now need a explicit ConnectionType for Nodes." in str( assert "ConnectionField's now need a explicit ConnectionType for Nodes." in str(
exc_info.value exc_info.value
) )
@ -299,20 +175,3 @@ def test_connectionfield_required():
executed = schema.execute("{ testConnection { edges { cursor } } }") executed = schema.execute("{ testConnection { edges { cursor } } }")
assert not executed.errors assert not executed.errors
assert executed.data == {"testConnection": {"edges": []}} assert executed.data == {"testConnection": {"edges": []}}
def test_connectionfield_strict_types():
class MyObjectConnection(Connection):
class Meta:
node = MyObject
strict_types = True
connection_field = ConnectionField(MyObjectConnection)
edges_field_type = connection_field.type._meta.fields["edges"].type
assert isinstance(edges_field_type, NonNull)
edges_list_element_type = edges_field_type.of_type.of_type
assert isinstance(edges_list_element_type, NonNull)
node_field = edges_list_element_type.of_type._meta.fields["node"]
assert isinstance(node_field.type, NonNull)

View File

@ -1,121 +0,0 @@
from pytest import mark
from graphql_relay.utils import base64
from graphene.types import ObjectType, Schema, String
from graphene.relay.connection import Connection, ConnectionField, PageInfo
from graphene.relay.node import Node
letter_chars = ["A", "B", "C", "D", "E"]
class Letter(ObjectType):
class Meta:
interfaces = (Node,)
letter = String()
class LetterConnection(Connection):
class Meta:
node = Letter
class Query(ObjectType):
letters = ConnectionField(LetterConnection)
connection_letters = ConnectionField(LetterConnection)
async_letters = ConnectionField(LetterConnection)
node = Node.Field()
def resolve_letters(self, info, **args):
return list(letters.values())
async def resolve_async_letters(self, info, **args):
return list(letters.values())
def resolve_connection_letters(self, info, **args):
return LetterConnection(
page_info=PageInfo(has_next_page=True, has_previous_page=False),
edges=[
LetterConnection.Edge(node=Letter(id=0, letter="A"), cursor="a-cursor")
],
)
schema = Schema(Query)
letters = {letter: Letter(id=i, letter=letter) for i, letter in enumerate(letter_chars)}
def edges(selected_letters):
return [
{
"node": {"id": base64("Letter:%s" % letter.id), "letter": letter.letter},
"cursor": base64("arrayconnection:%s" % letter.id),
}
for letter in [letters[i] for i in selected_letters]
]
def cursor_for(ltr):
letter = letters[ltr]
return base64("arrayconnection:%s" % letter.id)
def execute(args=""):
if args:
args = "(" + args + ")"
return schema.execute(
"""
{
letters%s {
edges {
node {
id
letter
}
cursor
}
pageInfo {
hasPreviousPage
hasNextPage
startCursor
endCursor
}
}
}
"""
% args
)
@mark.asyncio
async def test_connection_async():
result = await schema.execute_async(
"""
{
asyncLetters(first:1) {
edges {
node {
id
letter
}
}
pageInfo {
hasPreviousPage
hasNextPage
}
}
}
"""
)
assert not result.errors
assert result.data == {
"asyncLetters": {
"edges": [{"node": {"id": "TGV0dGVyOjA=", "letter": "A"}}],
"pageInfo": {"hasPreviousPage": False, "hasNextPage": True},
}
}

View File

@ -1,6 +1,7 @@
from pytest import mark from collections import OrderedDict
from graphql_relay.utils import base64 from graphql_relay.utils import base64
from promise import Promise
from ...types import ObjectType, Schema, String from ...types import ObjectType, Schema, String
from ..connection import Connection, ConnectionField, PageInfo from ..connection import Connection, ConnectionField, PageInfo
@ -24,15 +25,15 @@ class LetterConnection(Connection):
class Query(ObjectType): class Query(ObjectType):
letters = ConnectionField(LetterConnection) letters = ConnectionField(LetterConnection)
connection_letters = ConnectionField(LetterConnection) connection_letters = ConnectionField(LetterConnection)
async_letters = ConnectionField(LetterConnection) promise_letters = ConnectionField(LetterConnection)
node = Node.Field() node = Node.Field()
def resolve_letters(self, info, **args): def resolve_letters(self, info, **args):
return list(letters.values()) return list(letters.values())
async def resolve_async_letters(self, info, **args): def resolve_promise_letters(self, info, **args):
return list(letters.values()) return Promise.resolve(list(letters.values()))
def resolve_connection_letters(self, info, **args): def resolve_connection_letters(self, info, **args):
return LetterConnection( return LetterConnection(
@ -45,16 +46,18 @@ class Query(ObjectType):
schema = Schema(Query) schema = Schema(Query)
letters = {letter: Letter(id=i, letter=letter) for i, letter in enumerate(letter_chars)} letters = OrderedDict()
for i, letter in enumerate(letter_chars):
letters[letter] = Letter(id=i, letter=letter)
def edges(selected_letters): def edges(selected_letters):
return [ return [
{ {
"node": {"id": base64("Letter:%s" % letter.id), "letter": letter.letter}, "node": {"id": base64("Letter:%s" % l.id), "letter": l.letter},
"cursor": base64("arrayconnection:%s" % letter.id), "cursor": base64("arrayconnection:%s" % l.id),
} }
for letter in [letters[i] for i in selected_letters] for l in [letters[i] for i in selected_letters]
] ]
@ -63,10 +66,11 @@ def cursor_for(ltr):
return base64("arrayconnection:%s" % letter.id) return base64("arrayconnection:%s" % letter.id)
async def execute(args=""): def execute(args=""):
if args: if args:
args = "(" + args + ")" args = "(" + args + ")"
return await schema.execute_async(
return schema.execute(
""" """
{ {
letters%s { letters%s {
@ -90,8 +94,8 @@ async def execute(args=""):
) )
async def check(args, letters, has_previous_page=False, has_next_page=False): def check(args, letters, has_previous_page=False, has_next_page=False):
result = await execute(args) result = execute(args)
expected_edges = edges(letters) expected_edges = edges(letters)
expected_page_info = { expected_page_info = {
"hasPreviousPage": has_previous_page, "hasPreviousPage": has_previous_page,
@ -106,126 +110,114 @@ async def check(args, letters, has_previous_page=False, has_next_page=False):
} }
@mark.asyncio def test_returns_all_elements_without_filters():
async def test_returns_all_elements_without_filters(): check("", "ABCDE")
await check("", "ABCDE")
@mark.asyncio def test_respects_a_smaller_first():
async def test_respects_a_smaller_first(): check("first: 2", "AB", has_next_page=True)
await check("first: 2", "AB", has_next_page=True)
@mark.asyncio def test_respects_an_overly_large_first():
async def test_respects_an_overly_large_first(): check("first: 10", "ABCDE")
await check("first: 10", "ABCDE")
@mark.asyncio def test_respects_a_smaller_last():
async def test_respects_a_smaller_last(): check("last: 2", "DE", has_previous_page=True)
await check("last: 2", "DE", has_previous_page=True)
@mark.asyncio def test_respects_an_overly_large_last():
async def test_respects_an_overly_large_last(): check("last: 10", "ABCDE")
await check("last: 10", "ABCDE")
@mark.asyncio def test_respects_first_and_after():
async def test_respects_first_and_after(): check('first: 2, after: "{}"'.format(cursor_for("B")), "CD", has_next_page=True)
await check(f'first: 2, after: "{cursor_for("B")}"', "CD", has_next_page=True)
@mark.asyncio def test_respects_first_and_after_with_long_first():
async def test_respects_first_and_after_with_long_first(): check('first: 10, after: "{}"'.format(cursor_for("B")), "CDE")
await check(f'first: 10, after: "{cursor_for("B")}"', "CDE")
@mark.asyncio def test_respects_last_and_before():
async def test_respects_last_and_before(): check('last: 2, before: "{}"'.format(cursor_for("D")), "BC", has_previous_page=True)
await check(f'last: 2, before: "{cursor_for("D")}"', "BC", has_previous_page=True)
@mark.asyncio def test_respects_last_and_before_with_long_last():
async def test_respects_last_and_before_with_long_last(): check('last: 10, before: "{}"'.format(cursor_for("D")), "ABC")
await check(f'last: 10, before: "{cursor_for("D")}"', "ABC")
@mark.asyncio def test_respects_first_and_after_and_before_too_few():
async def test_respects_first_and_after_and_before_too_few(): check(
await check( 'first: 2, after: "{}", before: "{}"'.format(cursor_for("A"), cursor_for("E")),
f'first: 2, after: "{cursor_for("A")}", before: "{cursor_for("E")}"',
"BC", "BC",
has_next_page=True, has_next_page=True,
) )
@mark.asyncio def test_respects_first_and_after_and_before_too_many():
async def test_respects_first_and_after_and_before_too_many(): check(
await check( 'first: 4, after: "{}", before: "{}"'.format(cursor_for("A"), cursor_for("E")),
f'first: 4, after: "{cursor_for("A")}", before: "{cursor_for("E")}"', "BCD" "BCD",
) )
@mark.asyncio def test_respects_first_and_after_and_before_exactly_right():
async def test_respects_first_and_after_and_before_exactly_right(): check(
await check( 'first: 3, after: "{}", before: "{}"'.format(cursor_for("A"), cursor_for("E")),
f'first: 3, after: "{cursor_for("A")}", before: "{cursor_for("E")}"', "BCD" "BCD",
) )
@mark.asyncio def test_respects_last_and_after_and_before_too_few():
async def test_respects_last_and_after_and_before_too_few(): check(
await check( 'last: 2, after: "{}", before: "{}"'.format(cursor_for("A"), cursor_for("E")),
f'last: 2, after: "{cursor_for("A")}", before: "{cursor_for("E")}"',
"CD", "CD",
has_previous_page=True, has_previous_page=True,
) )
@mark.asyncio def test_respects_last_and_after_and_before_too_many():
async def test_respects_last_and_after_and_before_too_many(): check(
await check( 'last: 4, after: "{}", before: "{}"'.format(cursor_for("A"), cursor_for("E")),
f'last: 4, after: "{cursor_for("A")}", before: "{cursor_for("E")}"', "BCD" "BCD",
) )
@mark.asyncio def test_respects_last_and_after_and_before_exactly_right():
async def test_respects_last_and_after_and_before_exactly_right(): check(
await check( 'last: 3, after: "{}", before: "{}"'.format(cursor_for("A"), cursor_for("E")),
f'last: 3, after: "{cursor_for("A")}", before: "{cursor_for("E")}"', "BCD" "BCD",
) )
@mark.asyncio def test_returns_no_elements_if_first_is_0():
async def test_returns_no_elements_if_first_is_0(): check("first: 0", "", has_next_page=True)
await check("first: 0", "", has_next_page=True)
@mark.asyncio def test_returns_all_elements_if_cursors_are_invalid():
async def test_returns_all_elements_if_cursors_are_invalid(): check('before: "invalid" after: "invalid"', "ABCDE")
await check('before: "invalid" after: "invalid"', "ABCDE")
@mark.asyncio def test_returns_all_elements_if_cursors_are_on_the_outside():
async def test_returns_all_elements_if_cursors_are_on_the_outside(): check(
await check( 'before: "{}" after: "{}"'.format(
f'before: "{base64("arrayconnection:%s" % 6)}" after: "{base64("arrayconnection:%s" % -1)}"', base64("arrayconnection:%s" % 6), base64("arrayconnection:%s" % -1)
),
"ABCDE", "ABCDE",
) )
@mark.asyncio def test_returns_no_elements_if_cursors_cross():
async def test_returns_no_elements_if_cursors_cross(): check(
await check( 'before: "{}" after: "{}"'.format(
f'before: "{base64("arrayconnection:%s" % 2)}" after: "{base64("arrayconnection:%s" % 4)}"', base64("arrayconnection:%s" % 2), base64("arrayconnection:%s" % 4)
),
"", "",
) )
@mark.asyncio def test_connection_type_nodes():
async def test_connection_type_nodes(): result = schema.execute(
result = await schema.execute_async(
""" """
{ {
connectionLetters { connectionLetters {
@ -256,12 +248,11 @@ async def test_connection_type_nodes():
} }
@mark.asyncio def test_connection_promise():
async def test_connection_async(): result = schema.execute(
result = await schema.execute_async(
""" """
{ {
asyncLetters(first:1) { promiseLetters(first:1) {
edges { edges {
node { node {
id id
@ -279,7 +270,7 @@ async def test_connection_async():
assert not result.errors assert not result.errors
assert result.data == { assert result.data == {
"asyncLetters": { "promiseLetters": {
"edges": [{"node": {"id": "TGV0dGVyOjA=", "letter": "A"}}], "edges": [{"node": {"id": "TGV0dGVyOjA=", "letter": "A"}}],
"pageInfo": {"hasPreviousPage": False, "hasNextPage": True}, "pageInfo": {"hasPreviousPage": False, "hasNextPage": True},
} }

View File

@ -1,325 +0,0 @@
import re
from uuid import uuid4
from graphql import graphql_sync
from ..id_type import BaseGlobalIDType, SimpleGlobalIDType, UUIDGlobalIDType
from ..node import Node
from ...types import Int, ObjectType, Schema, String
class TestUUIDGlobalID:
def setup_method(self):
self.user_list = [
{"id": uuid4(), "name": "First"},
{"id": uuid4(), "name": "Second"},
{"id": uuid4(), "name": "Third"},
{"id": uuid4(), "name": "Fourth"},
]
self.users = {user["id"]: user for user in self.user_list}
class CustomNode(Node):
class Meta:
global_id_type = UUIDGlobalIDType
class User(ObjectType):
class Meta:
interfaces = [CustomNode]
name = String()
@classmethod
def get_node(cls, _type, _id):
return self.users[_id]
class RootQuery(ObjectType):
user = CustomNode.Field(User)
self.schema = Schema(query=RootQuery, types=[User])
self.graphql_schema = self.schema.graphql_schema
def test_str_schema_correct(self):
"""
Check that the schema has the expected and custom node interface and user type and that they both use UUIDs
"""
parsed = re.findall(r"(.+) \{\n\s*([\w\W]*?)\n\}", str(self.schema))
types = [t for t, f in parsed]
fields = [f for t, f in parsed]
custom_node_interface = "interface CustomNode"
assert custom_node_interface in types
assert (
'"""The ID of the object"""\n id: UUID!'
== fields[types.index(custom_node_interface)]
)
user_type = "type User implements CustomNode"
assert user_type in types
assert (
'"""The ID of the object"""\n id: UUID!\n name: String'
== fields[types.index(user_type)]
)
def test_get_by_id(self):
query = """query userById($id: UUID!) {
user(id: $id) {
id
name
}
}"""
# UUID need to be converted to string for serialization
result = graphql_sync(
self.graphql_schema,
query,
variable_values={"id": str(self.user_list[0]["id"])},
)
assert not result.errors
assert result.data["user"]["id"] == str(self.user_list[0]["id"])
assert result.data["user"]["name"] == self.user_list[0]["name"]
class TestSimpleGlobalID:
def setup_method(self):
self.user_list = [
{"id": "my global primary key in clear 1", "name": "First"},
{"id": "my global primary key in clear 2", "name": "Second"},
{"id": "my global primary key in clear 3", "name": "Third"},
{"id": "my global primary key in clear 4", "name": "Fourth"},
]
self.users = {user["id"]: user for user in self.user_list}
class CustomNode(Node):
class Meta:
global_id_type = SimpleGlobalIDType
class User(ObjectType):
class Meta:
interfaces = [CustomNode]
name = String()
@classmethod
def get_node(cls, _type, _id):
return self.users[_id]
class RootQuery(ObjectType):
user = CustomNode.Field(User)
self.schema = Schema(query=RootQuery, types=[User])
self.graphql_schema = self.schema.graphql_schema
def test_str_schema_correct(self):
"""
Check that the schema has the expected and custom node interface and user type and that they both use UUIDs
"""
parsed = re.findall(r"(.+) \{\n\s*([\w\W]*?)\n\}", str(self.schema))
types = [t for t, f in parsed]
fields = [f for t, f in parsed]
custom_node_interface = "interface CustomNode"
assert custom_node_interface in types
assert (
'"""The ID of the object"""\n id: ID!'
== fields[types.index(custom_node_interface)]
)
user_type = "type User implements CustomNode"
assert user_type in types
assert (
'"""The ID of the object"""\n id: ID!\n name: String'
== fields[types.index(user_type)]
)
def test_get_by_id(self):
query = """query {
user(id: "my global primary key in clear 3") {
id
name
}
}"""
result = graphql_sync(self.graphql_schema, query)
assert not result.errors
assert result.data["user"]["id"] == self.user_list[2]["id"]
assert result.data["user"]["name"] == self.user_list[2]["name"]
class TestCustomGlobalID:
def setup_method(self):
self.user_list = [
{"id": 1, "name": "First"},
{"id": 2, "name": "Second"},
{"id": 3, "name": "Third"},
{"id": 4, "name": "Fourth"},
]
self.users = {user["id"]: user for user in self.user_list}
class CustomGlobalIDType(BaseGlobalIDType):
"""
Global id that is simply and integer in clear.
"""
graphene_type = Int
@classmethod
def resolve_global_id(cls, info, global_id):
_type = info.return_type.graphene_type._meta.name
return _type, global_id
@classmethod
def to_global_id(cls, _type, _id):
return _id
class CustomNode(Node):
class Meta:
global_id_type = CustomGlobalIDType
class User(ObjectType):
class Meta:
interfaces = [CustomNode]
name = String()
@classmethod
def get_node(cls, _type, _id):
return self.users[_id]
class RootQuery(ObjectType):
user = CustomNode.Field(User)
self.schema = Schema(query=RootQuery, types=[User])
self.graphql_schema = self.schema.graphql_schema
def test_str_schema_correct(self):
"""
Check that the schema has the expected and custom node interface and user type and that they both use UUIDs
"""
parsed = re.findall(r"(.+) \{\n\s*([\w\W]*?)\n\}", str(self.schema))
types = [t for t, f in parsed]
fields = [f for t, f in parsed]
custom_node_interface = "interface CustomNode"
assert custom_node_interface in types
assert (
'"""The ID of the object"""\n id: Int!'
== fields[types.index(custom_node_interface)]
)
user_type = "type User implements CustomNode"
assert user_type in types
assert (
'"""The ID of the object"""\n id: Int!\n name: String'
== fields[types.index(user_type)]
)
def test_get_by_id(self):
query = """query {
user(id: 2) {
id
name
}
}"""
result = graphql_sync(self.graphql_schema, query)
assert not result.errors
assert result.data["user"]["id"] == self.user_list[1]["id"]
assert result.data["user"]["name"] == self.user_list[1]["name"]
class TestIncompleteCustomGlobalID:
def setup_method(self):
self.user_list = [
{"id": 1, "name": "First"},
{"id": 2, "name": "Second"},
{"id": 3, "name": "Third"},
{"id": 4, "name": "Fourth"},
]
self.users = {user["id"]: user for user in self.user_list}
def test_must_define_to_global_id(self):
"""
Test that if the `to_global_id` method is not defined, we can query the object, but we can't request its ID.
"""
class CustomGlobalIDType(BaseGlobalIDType):
graphene_type = Int
@classmethod
def resolve_global_id(cls, info, global_id):
_type = info.return_type.graphene_type._meta.name
return _type, global_id
class CustomNode(Node):
class Meta:
global_id_type = CustomGlobalIDType
class User(ObjectType):
class Meta:
interfaces = [CustomNode]
name = String()
@classmethod
def get_node(cls, _type, _id):
return self.users[_id]
class RootQuery(ObjectType):
user = CustomNode.Field(User)
self.schema = Schema(query=RootQuery, types=[User])
self.graphql_schema = self.schema.graphql_schema
query = """query {
user(id: 2) {
name
}
}"""
result = graphql_sync(self.graphql_schema, query)
assert not result.errors
assert result.data["user"]["name"] == self.user_list[1]["name"]
query = """query {
user(id: 2) {
id
name
}
}"""
result = graphql_sync(self.graphql_schema, query)
assert result.errors is not None
assert len(result.errors) == 1
assert result.errors[0].path == ["user", "id"]
def test_must_define_resolve_global_id(self):
"""
Test that if the `resolve_global_id` method is not defined, we can't query the object by ID.
"""
class CustomGlobalIDType(BaseGlobalIDType):
graphene_type = Int
@classmethod
def to_global_id(cls, _type, _id):
return _id
class CustomNode(Node):
class Meta:
global_id_type = CustomGlobalIDType
class User(ObjectType):
class Meta:
interfaces = [CustomNode]
name = String()
@classmethod
def get_node(cls, _type, _id):
return self.users[_id]
class RootQuery(ObjectType):
user = CustomNode.Field(User)
self.schema = Schema(query=RootQuery, types=[User])
self.graphql_schema = self.schema.graphql_schema
query = """query {
user(id: 2) {
id
name
}
}"""
result = graphql_sync(self.graphql_schema, query)
assert result.errors is not None
assert len(result.errors) == 1
assert result.errors[0].path == ["user"]

View File

@ -17,7 +17,7 @@ class User(ObjectType):
name = String() name = String()
class Info: class Info(object):
def __init__(self, parent_type): def __init__(self, parent_type):
self.parent_type = GrapheneObjectType( self.parent_type = GrapheneObjectType(
graphene_type=parent_type, graphene_type=parent_type,
@ -45,7 +45,7 @@ def test_global_id_allows_overriding_of_node_and_required():
def test_global_id_defaults_to_info_parent_type(): def test_global_id_defaults_to_info_parent_type():
my_id = "1" my_id = "1"
gid = GlobalID() gid = GlobalID()
id_resolver = gid.wrap_resolve(lambda *_: my_id) id_resolver = gid.get_resolver(lambda *_: my_id)
my_global_id = id_resolver(None, Info(User)) my_global_id = id_resolver(None, Info(User))
assert my_global_id == to_global_id(User._meta.name, my_id) assert my_global_id == to_global_id(User._meta.name, my_id)
@ -53,6 +53,6 @@ def test_global_id_defaults_to_info_parent_type():
def test_global_id_allows_setting_customer_parent_type(): def test_global_id_allows_setting_customer_parent_type():
my_id = "1" my_id = "1"
gid = GlobalID(parent_type=User) gid = GlobalID(parent_type=User)
id_resolver = gid.wrap_resolve(lambda *_: my_id) id_resolver = gid.get_resolver(lambda *_: my_id)
my_global_id = id_resolver(None, None) my_global_id = id_resolver(None, None)
assert my_global_id == to_global_id(User._meta.name, my_id) assert my_global_id == to_global_id(User._meta.name, my_id)

View File

@ -1,4 +1,5 @@
from pytest import mark, raises import pytest
from promise import Promise
from ...types import ( from ...types import (
ID, ID,
@ -14,7 +15,7 @@ from ...types.scalars import String
from ..mutation import ClientIDMutation from ..mutation import ClientIDMutation
class SharedFields: class SharedFields(object):
shared = String() shared = String()
@ -36,7 +37,7 @@ class SaySomething(ClientIDMutation):
return SaySomething(phrase=str(what)) return SaySomething(phrase=str(what))
class FixedSaySomething: class FixedSaySomething(object):
__slots__ = ("phrase",) __slots__ = ("phrase",)
def __init__(self, phrase): def __init__(self, phrase):
@ -54,15 +55,15 @@ class SaySomethingFixed(ClientIDMutation):
return FixedSaySomething(phrase=str(what)) return FixedSaySomething(phrase=str(what))
class SaySomethingAsync(ClientIDMutation): class SaySomethingPromise(ClientIDMutation):
class Input: class Input:
what = String() what = String()
phrase = String() phrase = String()
@staticmethod @staticmethod
async def mutate_and_get_payload(self, info, what, client_mutation_id=None): def mutate_and_get_payload(self, info, what, client_mutation_id=None):
return SaySomething(phrase=str(what)) return Promise.resolve(SaySomething(phrase=str(what)))
# MyEdge = MyNode.Connection.Edge # MyEdge = MyNode.Connection.Edge
@ -96,7 +97,7 @@ class RootQuery(ObjectType):
class Mutation(ObjectType): class Mutation(ObjectType):
say = SaySomething.Field() say = SaySomething.Field()
say_fixed = SaySomethingFixed.Field() say_fixed = SaySomethingFixed.Field()
say_async = SaySomethingAsync.Field() say_promise = SaySomethingPromise.Field()
other = OtherMutation.Field() other = OtherMutation.Field()
@ -104,7 +105,7 @@ schema = Schema(query=RootQuery, mutation=Mutation)
def test_no_mutate_and_get_payload(): def test_no_mutate_and_get_payload():
with raises(AssertionError) as excinfo: with pytest.raises(AssertionError) as excinfo:
class MyMutation(ClientIDMutation): class MyMutation(ClientIDMutation):
pass pass
@ -117,12 +118,12 @@ def test_no_mutate_and_get_payload():
def test_mutation(): def test_mutation():
fields = SaySomething._meta.fields fields = SaySomething._meta.fields
assert list(fields) == ["phrase", "client_mutation_id"] assert list(fields.keys()) == ["phrase", "client_mutation_id"]
assert SaySomething._meta.name == "SaySomethingPayload" assert SaySomething._meta.name == "SaySomethingPayload"
assert isinstance(fields["phrase"], Field) assert isinstance(fields["phrase"], Field)
field = SaySomething.Field() field = SaySomething.Field()
assert field.type == SaySomething assert field.type == SaySomething
assert list(field.args) == ["input"] assert list(field.args.keys()) == ["input"]
assert isinstance(field.args["input"], Argument) assert isinstance(field.args["input"], Argument)
assert isinstance(field.args["input"].type, NonNull) assert isinstance(field.args["input"].type, NonNull)
assert field.args["input"].type.of_type == SaySomething.Input assert field.args["input"].type.of_type == SaySomething.Input
@ -135,7 +136,7 @@ def test_mutation_input():
Input = SaySomething.Input Input = SaySomething.Input
assert issubclass(Input, InputObjectType) assert issubclass(Input, InputObjectType)
fields = Input._meta.fields fields = Input._meta.fields
assert list(fields) == ["what", "client_mutation_id"] assert list(fields.keys()) == ["what", "client_mutation_id"]
assert isinstance(fields["what"], InputField) assert isinstance(fields["what"], InputField)
assert fields["what"].type == String assert fields["what"].type == String
assert isinstance(fields["client_mutation_id"], InputField) assert isinstance(fields["client_mutation_id"], InputField)
@ -144,11 +145,11 @@ def test_mutation_input():
def test_subclassed_mutation(): def test_subclassed_mutation():
fields = OtherMutation._meta.fields fields = OtherMutation._meta.fields
assert list(fields) == ["name", "my_node_edge", "client_mutation_id"] assert list(fields.keys()) == ["name", "my_node_edge", "client_mutation_id"]
assert isinstance(fields["name"], Field) assert isinstance(fields["name"], Field)
field = OtherMutation.Field() field = OtherMutation.Field()
assert field.type == OtherMutation assert field.type == OtherMutation
assert list(field.args) == ["input"] assert list(field.args.keys()) == ["input"]
assert isinstance(field.args["input"], Argument) assert isinstance(field.args["input"], Argument)
assert isinstance(field.args["input"].type, NonNull) assert isinstance(field.args["input"].type, NonNull)
assert field.args["input"].type.of_type == OtherMutation.Input assert field.args["input"].type.of_type == OtherMutation.Input
@ -158,7 +159,7 @@ def test_subclassed_mutation_input():
Input = OtherMutation.Input Input = OtherMutation.Input
assert issubclass(Input, InputObjectType) assert issubclass(Input, InputObjectType)
fields = Input._meta.fields fields = Input._meta.fields
assert list(fields) == ["shared", "additional_field", "client_mutation_id"] assert list(fields.keys()) == ["shared", "additional_field", "client_mutation_id"]
assert isinstance(fields["shared"], InputField) assert isinstance(fields["shared"], InputField)
assert fields["shared"].type == String assert fields["shared"].type == String
assert isinstance(fields["additional_field"], InputField) assert isinstance(fields["additional_field"], InputField)
@ -184,13 +185,12 @@ def test_node_query_fixed():
) )
@mark.asyncio def test_node_query_promise():
async def test_node_query_async(): executed = schema.execute(
executed = await schema.execute_async( 'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
'mutation a { sayAsync(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
) )
assert not executed.errors assert not executed.errors
assert executed.data == {"sayAsync": {"phrase": "hello"}} assert executed.data == {"sayPromise": {"phrase": "hello"}}
def test_edge_query(): def test_edge_query():

View File

@ -1,90 +0,0 @@
from pytest import mark
from graphene.types import ID, Field, ObjectType, Schema
from graphene.types.scalars import String
from graphene.relay.mutation import ClientIDMutation
from graphene.test import Client
class SharedFields(object):
shared = String()
class MyNode(ObjectType):
# class Meta:
# interfaces = (Node, )
id = ID()
name = String()
class SaySomethingAsync(ClientIDMutation):
class Input:
what = String()
phrase = String()
@staticmethod
async def mutate_and_get_payload(self, info, what, client_mutation_id=None):
return SaySomethingAsync(phrase=str(what))
# MyEdge = MyNode.Connection.Edge
class MyEdge(ObjectType):
node = Field(MyNode)
cursor = String()
class OtherMutation(ClientIDMutation):
class Input(SharedFields):
additional_field = String()
name = String()
my_node_edge = Field(MyEdge)
@staticmethod
def mutate_and_get_payload(
self, info, shared="", additional_field="", client_mutation_id=None
):
edge_type = MyEdge
return OtherMutation(
name=shared + additional_field,
my_node_edge=edge_type(cursor="1", node=MyNode(name="name")),
)
class RootQuery(ObjectType):
something = String()
class Mutation(ObjectType):
say_promise = SaySomethingAsync.Field()
other = OtherMutation.Field()
schema = Schema(query=RootQuery, mutation=Mutation)
client = Client(schema)
@mark.asyncio
async def test_node_query_promise():
executed = await client.execute_async(
'mutation a { sayPromise(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
)
assert isinstance(executed, dict)
assert "errors" not in executed
assert executed["data"] == {"sayPromise": {"phrase": "hello"}}
@mark.asyncio
async def test_edge_query():
executed = await client.execute_async(
'mutation a { other(input: {clientMutationId:"1"}) { clientMutationId, myNodeEdge { cursor node { name }} } }'
)
assert isinstance(executed, dict)
assert "errors" not in executed
assert executed["data"] == {
"other": {
"clientMutationId": "1",
"myNodeEdge": {"cursor": "1", "node": {"name": "name"}},
}
}

View File

@ -1,5 +1,4 @@
import re from collections import OrderedDict
from textwrap import dedent
from graphql_relay import to_global_id from graphql_relay import to_global_id
@ -7,7 +6,8 @@ from ...types import ObjectType, Schema, String
from ..node import Node, is_node from ..node import Node, is_node
class SharedNodeFields: class SharedNodeFields(object):
shared = String() shared = String()
something_else = String() something_else = String()
@ -54,7 +54,6 @@ def test_node_good():
assert "id" in MyNode._meta.fields assert "id" in MyNode._meta.fields
assert is_node(MyNode) assert is_node(MyNode)
assert not is_node(object) assert not is_node(object)
assert not is_node("node")
def test_node_query(): def test_node_query():
@ -71,33 +70,23 @@ def test_subclassed_node_query():
% to_global_id("MyOtherNode", 1) % to_global_id("MyOtherNode", 1)
) )
assert not executed.errors assert not executed.errors
assert executed.data == { assert executed.data == OrderedDict(
"node": { {
"shared": "1", "node": OrderedDict(
"extraField": "extra field info.", [
"somethingElse": "----", ("shared", "1"),
("extraField", "extra field info."),
("somethingElse", "----"),
]
)
} }
} )
def test_node_requesting_non_node(): def test_node_requesting_non_node():
executed = schema.execute( executed = schema.execute(
'{ node(id:"%s") { __typename } } ' % Node.to_global_id("RootQuery", 1) '{ node(id:"%s") { __typename } } ' % Node.to_global_id("RootQuery", 1)
) )
assert executed.errors
assert re.match(
r"ObjectType .* does not implement the .* interface.",
executed.errors[0].message,
)
assert executed.data == {"node": None}
def test_node_requesting_unknown_type():
executed = schema.execute(
'{ node(id:"%s") { __typename } } ' % Node.to_global_id("UnknownType", 1)
)
assert executed.errors
assert re.match(r"Relay Node .* not found in schema", executed.errors[0].message)
assert executed.data == {"node": None} assert executed.data == {"node": None}
@ -105,8 +94,7 @@ def test_node_query_incorrect_id():
executed = schema.execute( executed = schema.execute(
'{ node(id:"%s") { ... on MyNode { name } } }' % "something:2" '{ node(id:"%s") { ... on MyNode { name } } }' % "something:2"
) )
assert executed.errors assert not executed.errors
assert re.match(r"Unable to parse global ID .*", executed.errors[0].message)
assert executed.data == {"node": None} assert executed.data == {"node": None}
@ -122,17 +110,6 @@ def test_node_field_custom():
assert node_field.node_type == Node assert node_field.node_type == Node
def test_node_field_args():
field_args = {
"name": "my_custom_name",
"description": "my_custom_description",
"deprecation_reason": "my_custom_deprecation_reason",
}
node_field = Node.Field(**field_args)
for field_arg, value in field_args.items():
assert getattr(node_field, field_arg) == value
def test_node_field_only_type(): def test_node_field_only_type():
executed = schema.execute( executed = schema.execute(
'{ onlyNode(id:"%s") { __typename, name } } ' % Node.to_global_id("MyNode", 1) '{ onlyNode(id:"%s") { __typename, name } } ' % Node.to_global_id("MyNode", 1)
@ -147,7 +124,7 @@ def test_node_field_only_type_wrong():
% Node.to_global_id("MyOtherNode", 1) % Node.to_global_id("MyOtherNode", 1)
) )
assert len(executed.errors) == 1 assert len(executed.errors) == 1
assert str(executed.errors[0]).startswith("Must receive a MyNode id.") assert str(executed.errors[0]) == "Must receive a MyNode id."
assert executed.data == {"onlyNode": None} assert executed.data == {"onlyNode": None}
@ -166,54 +143,39 @@ def test_node_field_only_lazy_type_wrong():
% Node.to_global_id("MyOtherNode", 1) % Node.to_global_id("MyOtherNode", 1)
) )
assert len(executed.errors) == 1 assert len(executed.errors) == 1
assert str(executed.errors[0]).startswith("Must receive a MyNode id.") assert str(executed.errors[0]) == "Must receive a MyNode id."
assert executed.data == {"onlyNodeLazy": None} assert executed.data == {"onlyNodeLazy": None}
def test_str_schema(): def test_str_schema():
assert ( assert (
str(schema).strip() str(schema)
== dedent( == """
''' schema {
schema { query: RootQuery
query: RootQuery }
}
type MyNode implements Node { type MyNode implements Node {
"""The ID of the object""" id: ID!
id: ID! name: String
name: String }
}
"""An object with an ID""" type MyOtherNode implements Node {
interface Node { id: ID!
"""The ID of the object""" shared: String
id: ID! somethingElse: String
} extraField: String
}
type MyOtherNode implements Node { interface Node {
"""The ID of the object""" id: ID!
id: ID! }
shared: String
somethingElse: String
extraField: String
}
type RootQuery { type RootQuery {
first: String first: String
node( node(id: ID!): Node
"""The ID of the object""" onlyNode(id: ID!): MyNode
id: ID! onlyNodeLazy(id: ID!): MyNode
): Node }
onlyNode( """.lstrip()
"""The ID of the object"""
id: ID!
): MyNode
onlyNodeLazy(
"""The ID of the object"""
id: ID!
): MyNode
}
'''
).strip()
) )

View File

@ -1,6 +1,4 @@
from textwrap import dedent from graphql import graphql
from graphql import graphql_sync
from ...types import Interface, ObjectType, Schema from ...types import Interface, ObjectType, Schema
from ...types.scalars import Int, String from ...types.scalars import Int, String
@ -12,12 +10,12 @@ class CustomNode(Node):
name = "Node" name = "Node"
@staticmethod @staticmethod
def to_global_id(type_, id): def to_global_id(type, id):
return id return id
@staticmethod @staticmethod
def get_node_from_global_id(info, id, only_type=None): def get_node_from_global_id(info, id, only_type=None):
assert info.schema is graphql_schema assert info.schema == schema
if id in user_data: if id in user_data:
return user_data.get(id) return user_data.get(id)
else: else:
@ -25,14 +23,14 @@ class CustomNode(Node):
class BasePhoto(Interface): class BasePhoto(Interface):
width = Int(description="The width of the photo in pixels") width = Int()
class User(ObjectType): class User(ObjectType):
class Meta: class Meta:
interfaces = [CustomNode] interfaces = [CustomNode]
name = String(description="The full name of the user") name = String()
class Photo(ObjectType): class Photo(ObjectType):
@ -50,52 +48,37 @@ class RootQuery(ObjectType):
schema = Schema(query=RootQuery, types=[User, Photo]) schema = Schema(query=RootQuery, types=[User, Photo])
graphql_schema = schema.graphql_schema
def test_str_schema_correct(): def test_str_schema_correct():
assert ( assert (
str(schema).strip() str(schema)
== dedent( == """schema {
''' query: RootQuery
schema { }
query: RootQuery
}
type User implements Node { interface BasePhoto {
"""The ID of the object""" width: Int
id: ID! }
"""The full name of the user""" interface Node {
name: String id: ID!
} }
interface Node { type Photo implements Node, BasePhoto {
"""The ID of the object""" id: ID!
id: ID! width: Int
} }
type Photo implements Node & BasePhoto { type RootQuery {
"""The ID of the object""" node(id: ID!): Node
id: ID! }
"""The width of the photo in pixels""" type User implements Node {
width: Int id: ID!
} name: String
}
interface BasePhoto { """
"""The width of the photo in pixels"""
width: Int
}
type RootQuery {
node(
"""The ID of the object"""
id: ID!
): Node
}
'''
).strip()
) )
@ -108,7 +91,7 @@ def test_gets_the_correct_id_for_users():
} }
""" """
expected = {"node": {"id": "1"}} expected = {"node": {"id": "1"}}
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -122,7 +105,7 @@ def test_gets_the_correct_id_for_photos():
} }
""" """
expected = {"node": {"id": "4"}} expected = {"node": {"id": "4"}}
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -139,7 +122,7 @@ def test_gets_the_correct_name_for_users():
} }
""" """
expected = {"node": {"id": "1", "name": "John Doe"}} expected = {"node": {"id": "1", "name": "John Doe"}}
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -156,7 +139,7 @@ def test_gets_the_correct_width_for_photos():
} }
""" """
expected = {"node": {"id": "4", "width": 400}} expected = {"node": {"id": "4", "width": 400}}
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -171,7 +154,7 @@ def test_gets_the_correct_typename_for_users():
} }
""" """
expected = {"node": {"id": "1", "__typename": "User"}} expected = {"node": {"id": "1", "__typename": "User"}}
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -186,7 +169,7 @@ def test_gets_the_correct_typename_for_photos():
} }
""" """
expected = {"node": {"id": "4", "__typename": "Photo"}} expected = {"node": {"id": "4", "__typename": "Photo"}}
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -203,7 +186,7 @@ def test_ignores_photo_fragments_on_user():
} }
""" """
expected = {"node": {"id": "1"}} expected = {"node": {"id": "1"}}
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -217,7 +200,7 @@ def test_returns_null_for_bad_ids():
} }
""" """
expected = {"node": None} expected = {"node": None}
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -256,7 +239,7 @@ def test_have_correct_node_interface():
], ],
} }
} }
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected
@ -308,6 +291,6 @@ def test_has_correct_node_root_field():
} }
} }
} }
result = graphql_sync(graphql_schema, query) result = graphql(schema, query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected

View File

@ -1,3 +1,6 @@
from promise import Promise, is_thenable
import six
from graphql.error import format_error as format_graphql_error
from graphql.error import GraphQLError from graphql.error import GraphQLError
from graphene.types.schema import Schema from graphene.types.schema import Schema
@ -5,20 +8,25 @@ from graphene.types.schema import Schema
def default_format_error(error): def default_format_error(error):
if isinstance(error, GraphQLError): if isinstance(error, GraphQLError):
return error.formatted return format_graphql_error(error)
return {"message": str(error)}
return {"message": six.text_type(error)}
def format_execution_result(execution_result, format_error): def format_execution_result(execution_result, format_error):
if execution_result: if execution_result:
response = {} response = {}
if execution_result.errors: if execution_result.errors:
response["errors"] = [format_error(e) for e in execution_result.errors] response["errors"] = [format_error(e) for e in execution_result.errors]
response["data"] = execution_result.data
if not execution_result.invalid:
response["data"] = execution_result.data
return response return response
class Client: class Client(object):
def __init__(self, schema, format_error=None, **execute_options): def __init__(self, schema, format_error=None, **execute_options):
assert isinstance(schema, Schema) assert isinstance(schema, Schema)
self.schema = schema self.schema = schema
@ -30,10 +38,7 @@ class Client:
def execute(self, *args, **kwargs): def execute(self, *args, **kwargs):
executed = self.schema.execute(*args, **dict(self.execute_options, **kwargs)) executed = self.schema.execute(*args, **dict(self.execute_options, **kwargs))
return self.format_result(executed) if is_thenable(executed):
return Promise.resolve(executed).then(self.format_result)
async def execute_async(self, *args, **kwargs):
executed = await self.schema.execute_async(
*args, **dict(self.execute_options, **kwargs)
)
return self.format_result(executed) return self.format_result(executed)

View File

@ -1,41 +0,0 @@
# https://github.com/graphql-python/graphene/issues/1293
from datetime import datetime, timezone
import graphene
from graphql.utilities import print_schema
class Filters(graphene.InputObjectType):
datetime_after = graphene.DateTime(
required=False,
default_value=datetime.fromtimestamp(1434549820.776, timezone.utc),
)
datetime_before = graphene.DateTime(
required=False,
default_value=datetime.fromtimestamp(1444549820.776, timezone.utc),
)
class SetDatetime(graphene.Mutation):
class Arguments:
filters = Filters(required=True)
ok = graphene.Boolean()
def mutate(root, info, filters):
return SetDatetime(ok=True)
class Query(graphene.ObjectType):
goodbye = graphene.String()
class Mutations(graphene.ObjectType):
set_datetime = SetDatetime.Field()
def test_schema_printable_with_default_datetime_value():
schema = graphene.Schema(query=Query, mutation=Mutations)
schema_str = print_schema(schema.graphql_schema)
assert schema_str, "empty schema printed"

View File

@ -1,36 +0,0 @@
from ...types import ObjectType, Schema, String, NonNull
class Query(ObjectType):
hello = String(input=NonNull(String))
def resolve_hello(self, info, input):
if input == "nothing":
return None
return f"Hello {input}!"
schema = Schema(query=Query)
def test_required_input_provided():
"""
Test that a required argument works when provided.
"""
input_value = "Potato"
result = schema.execute('{ hello(input: "%s") }' % input_value)
assert not result.errors
assert result.data == {"hello": "Hello Potato!"}
def test_required_input_missing():
"""
Test that a required argument raised an error if not provided.
"""
result = schema.execute("{ hello }")
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message
== "Field 'hello' argument 'input' of type 'String!' is required, but it was not provided."
)

View File

@ -1,53 +0,0 @@
import pytest
from ...types.base64 import Base64
from ...types.datetime import Date, DateTime
from ...types.decimal import Decimal
from ...types.generic import GenericScalar
from ...types.json import JSONString
from ...types.objecttype import ObjectType
from ...types.scalars import ID, BigInt, Boolean, Float, Int, String
from ...types.schema import Schema
from ...types.uuid import UUID
@pytest.mark.parametrize(
"input_type,input_value",
[
(Date, '"2022-02-02"'),
(GenericScalar, '"foo"'),
(Int, "1"),
(BigInt, "12345678901234567890"),
(Float, "1.1"),
(String, '"foo"'),
(Boolean, "true"),
(ID, "1"),
(DateTime, '"2022-02-02T11:11:11"'),
(UUID, '"cbebbc62-758e-4f75-a890-bc73b5017d81"'),
(Decimal, '"1.1"'),
(JSONString, '"{\\"key\\":\\"foo\\",\\"value\\":\\"bar\\"}"'),
(Base64, '"Q2hlbG8gd29ycmxkCg=="'),
],
)
def test_parse_literal_with_variables(input_type, input_value):
# input_b needs to be evaluated as literal while the variable dict for
# input_a is passed along.
class Query(ObjectType):
generic = GenericScalar(input_a=GenericScalar(), input_b=input_type())
def resolve_generic(self, info, input_a=None, input_b=None):
return input
schema = Schema(query=Query)
query = f"""
query Test($a: GenericScalar){{
generic(inputA: $a, inputB: {input_value})
}}
"""
result = schema.execute(
query,
variables={"a": "bar"},
)
assert not result.errors

View File

@ -21,7 +21,7 @@ class CreatePostResult(graphene.Union):
class CreatePost(graphene.Mutation): class CreatePost(graphene.Mutation):
class Arguments: class Input:
text = graphene.String(required=True) text = graphene.String(required=True)
result = graphene.Field(CreatePostResult) result = graphene.Field(CreatePostResult)

View File

@ -1,6 +1,6 @@
# https://github.com/graphql-python/graphene/issues/356 # https://github.com/graphql-python/graphene/issues/356
from pytest import raises import pytest
import graphene import graphene
from graphene import relay from graphene import relay
@ -23,11 +23,10 @@ def test_issue():
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
things = relay.ConnectionField(MyUnion) things = relay.ConnectionField(MyUnion)
with raises(Exception) as exc_info: with pytest.raises(Exception) as exc_info:
graphene.Schema(query=Query) graphene.Schema(query=Query)
assert str(exc_info.value) == ( assert str(exc_info.value) == (
"Query fields cannot be resolved." "IterableConnectionField type have to be a subclass of Connection. "
" IterableConnectionField type has to be a subclass of Connection." 'Received "MyUnion".'
' Received "MyUnion".'
) )

View File

@ -1,27 +0,0 @@
import pickle
from ...types.enum import Enum
class PickleEnum(Enum):
# is defined outside of test because pickle unable to dump class inside ot pytest function
A = "a"
B = 1
def test_enums_pickling():
a = PickleEnum.A
pickled = pickle.dumps(a)
restored = pickle.loads(pickled)
assert type(a) is type(restored)
assert a == restored
assert a.value == restored.value
assert a.name == restored.name
b = PickleEnum.B
pickled = pickle.dumps(b)
restored = pickle.loads(pickled)
assert type(a) is type(restored)
assert b == restored
assert b.value == restored.value
assert b.name == restored.name

View File

@ -1,8 +0,0 @@
import graphene
def test_issue():
options = {"description": "This my enum", "deprecation_reason": "For the funs"}
new_enum = graphene.Enum("MyEnum", [("some", "data")], **options)
assert new_enum._meta.description == options["description"]
assert new_enum._meta.deprecation_reason == options["deprecation_reason"]

View File

@ -1,53 +1,55 @@
from graphql import GraphQLResolveInfo as ResolveInfo # flake8: noqa
from graphql import ResolveInfo
from .argument import Argument from .objecttype import ObjectType
from .base64 import Base64 from .interface import Interface
from .context import Context from .mutation import Mutation
from .scalars import Scalar, String, ID, Int, Float, Boolean
from .datetime import Date, DateTime, Time from .datetime import Date, DateTime, Time
from .decimal import Decimal from .json import JSONString
from .dynamic import Dynamic from .uuid import UUID
from .schema import Schema
from .structures import List, NonNull
from .enum import Enum from .enum import Enum
from .field import Field from .field import Field
from .inputfield import InputField from .inputfield import InputField
from .argument import Argument
from .inputobjecttype import InputObjectType from .inputobjecttype import InputObjectType
from .interface import Interface from .dynamic import Dynamic
from .json import JSONString
from .mutation import Mutation
from .objecttype import ObjectType
from .scalars import ID, BigInt, Boolean, Float, Int, Scalar, String
from .schema import Schema
from .structures import List, NonNull
from .union import Union from .union import Union
from .uuid import UUID from .context import Context
# Deprecated
from .abstracttype import AbstractType
__all__ = [ __all__ = [
"Argument", "ObjectType",
"Base64", "InputObjectType",
"BigInt", "Interface",
"Boolean", "Mutation",
"Context",
"Date",
"DateTime",
"Decimal",
"Dynamic",
"Enum", "Enum",
"Field", "Field",
"Float",
"ID",
"InputField", "InputField",
"InputObjectType",
"Int",
"Interface",
"JSONString",
"List",
"Mutation",
"NonNull",
"ObjectType",
"ResolveInfo",
"Scalar",
"Schema", "Schema",
"Scalar",
"String", "String",
"ID",
"Int",
"Float",
"Date",
"DateTime",
"Time", "Time",
"JSONString",
"UUID", "UUID",
"Boolean",
"List",
"NonNull",
"Argument",
"Dynamic",
"Union", "Union",
"Context",
"ResolveInfo",
# Deprecated
"AbstractType",
] ]

View File

@ -0,0 +1,11 @@
from ..utils.deprecated import warn_deprecation
from ..utils.subclass_with_meta import SubclassWithMeta
class AbstractType(SubclassWithMeta):
def __init_subclass__(cls, *args, **kwargs):
warn_deprecation(
"Abstract type is deprecated, please use normal object inheritance instead.\n"
"See more: https://github.com/graphql-python/graphene/blob/master/UPGRADE-v2.0.md#deprecations"
)
super(AbstractType, cls).__init_subclass__(*args, **kwargs)

View File

@ -1,5 +1,5 @@
from collections import OrderedDict
from itertools import chain from itertools import chain
from graphql import Undefined
from .dynamic import Dynamic from .dynamic import Dynamic
from .mountedtype import MountedType from .mountedtype import MountedType
@ -8,45 +8,10 @@ from .utils import get_type
class Argument(MountedType): class Argument(MountedType):
"""
Makes an Argument available on a Field in the GraphQL schema.
Arguments will be parsed and provided to resolver methods for fields as keyword arguments.
All ``arg`` and ``**extra_args`` for a ``graphene.Field`` are implicitly mounted as Argument
using the below parameters.
.. code:: python
from graphene import String, Boolean, Argument
age = String(
# Boolean implicitly mounted as Argument
dog_years=Boolean(description="convert to dog years"),
# Boolean explicitly mounted as Argument
decades=Argument(Boolean, default_value=False),
)
args:
type (class for a graphene.UnmountedType): must be a class (not an instance) of an
unmounted graphene type (ex. scalar or object) which is used for the type of this
argument in the GraphQL schema.
required (optional, bool): indicates this argument as not null in the graphql schema. Same behavior
as graphene.NonNull. Default False.
name (optional, str): the name of the GraphQL argument. Defaults to parameter name.
description (optional, str): the description of the GraphQL argument in the schema.
default_value (optional, Any): The value to be provided if the user does not set this argument in
the operation.
deprecation_reason (optional, str): Setting this value indicates that the argument is
depreciated and may provide instruction or reason on how for clients to proceed. Cannot be
set if the argument is required (see spec).
"""
def __init__( def __init__(
self, self,
type_, type,
default_value=Undefined, default_value=None,
deprecation_reason=None,
description=None, description=None,
name=None, name=None,
required=False, required=False,
@ -55,16 +20,12 @@ class Argument(MountedType):
super(Argument, self).__init__(_creation_counter=_creation_counter) super(Argument, self).__init__(_creation_counter=_creation_counter)
if required: if required:
assert ( type = NonNull(type)
deprecation_reason is None
), f"Argument {name} is required, cannot deprecate it."
type_ = NonNull(type_)
self.name = name self.name = name
self._type = type_ self._type = type
self.default_value = default_value self.default_value = default_value
self.description = description self.description = description
self.deprecation_reason = deprecation_reason
@property @property
def type(self): def type(self):
@ -76,7 +37,6 @@ class Argument(MountedType):
and self.type == other.type and self.type == other.type
and self.default_value == other.default_value and self.default_value == other.default_value
and self.description == other.description and self.description == other.description
and self.deprecation_reason == other.deprecation_reason
) )
@ -90,7 +50,7 @@ def to_arguments(args, extra_args=None):
else: else:
extra_args = [] extra_args = []
iter_arguments = chain(args.items(), extra_args) iter_arguments = chain(args.items(), extra_args)
arguments = {} arguments = OrderedDict()
for default_name, arg in iter_arguments: for default_name, arg in iter_arguments:
if isinstance(arg, Dynamic): if isinstance(arg, Dynamic):
arg = arg.get_type() arg = arg.get_type()
@ -104,17 +64,20 @@ def to_arguments(args, extra_args=None):
if isinstance(arg, (InputField, Field)): if isinstance(arg, (InputField, Field)):
raise ValueError( raise ValueError(
f"Expected {default_name} to be Argument, " "Expected {} to be Argument, but received {}. Try using Argument({}).".format(
f"but received {type(arg).__name__}. Try using Argument({arg.type})." default_name, type(arg).__name__, arg.type
)
) )
if not isinstance(arg, Argument): if not isinstance(arg, Argument):
raise ValueError(f'Unknown argument "{default_name}".') raise ValueError('Unknown argument "{}".'.format(default_name))
arg_name = default_name or arg.name arg_name = default_name or arg.name
assert ( assert (
arg_name not in arguments arg_name not in arguments
), f'More than one Argument have same name "{arg_name}".' ), 'More than one Argument have same name "{}".'.format(
arg_name
)
arguments[arg_name] = arg arguments[arg_name] = arg
return arguments return arguments

View File

@ -1,17 +1,15 @@
from typing import Type, Optional from ..utils.subclass_with_meta import SubclassWithMeta
from ..utils.subclass_with_meta import SubclassWithMeta, SubclassWithMeta_Meta
from ..utils.trim_docstring import trim_docstring from ..utils.trim_docstring import trim_docstring
class BaseOptions: class BaseOptions(object):
name: Optional[str] = None name = None # type: str
description: Optional[str] = None description = None # type: str
_frozen: bool = False _frozen = False # type: bool
def __init__(self, class_type: Type): def __init__(self, class_type):
self.class_type: Type = class_type self.class_type = class_type # type: Type
def freeze(self): def freeze(self):
self._frozen = True self._frozen = True
@ -20,13 +18,10 @@ class BaseOptions:
if not self._frozen: if not self._frozen:
super(BaseOptions, self).__setattr__(name, value) super(BaseOptions, self).__setattr__(name, value)
else: else:
raise Exception(f"Can't modify frozen Options {self}") raise Exception("Can't modify frozen Options {}".format(self))
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__name__} name={repr(self.name)}>" return "<{} name={}>".format(self.__class__.__name__, repr(self.name))
BaseTypeMeta = SubclassWithMeta_Meta
class BaseType(SubclassWithMeta): class BaseType(SubclassWithMeta):
@ -35,10 +30,8 @@ class BaseType(SubclassWithMeta):
return type(class_name, (cls,), {"Meta": options}) return type(class_name, (cls,), {"Meta": options})
@classmethod @classmethod
def __init_subclass_with_meta__( def __init_subclass_with_meta__(cls, name=None, description=None, _meta=None):
cls, name=None, description=None, _meta=None, **_kwargs assert "_meta" not in cls.__dict__, "Can't assign directly meta"
):
assert "_meta" not in cls.__dict__, "Can't assign meta directly"
if not _meta: if not _meta:
return return
_meta.name = name or cls.__name__ _meta.name = name or cls.__name__

View File

@ -1,43 +0,0 @@
from binascii import Error as _Error
from base64 import b64decode, b64encode
from graphql.error import GraphQLError
from graphql.language import StringValueNode, print_ast
from .scalars import Scalar
class Base64(Scalar):
"""
The `Base64` scalar type represents a base64-encoded String.
"""
@staticmethod
def serialize(value):
if not isinstance(value, bytes):
if isinstance(value, str):
value = value.encode("utf-8")
else:
value = str(value).encode("utf-8")
return b64encode(value).decode("utf-8")
@classmethod
def parse_literal(cls, node, _variables=None):
if not isinstance(node, StringValueNode):
raise GraphQLError(
f"Base64 cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
if not isinstance(value, bytes):
if not isinstance(value, str):
raise GraphQLError(
f"Base64 cannot represent non-string value: {repr(value)}"
)
value = value.encode("utf-8")
try:
return b64decode(value, validate=True).decode("utf-8")
except _Error:
raise GraphQLError(f"Base64 cannot decode value: {repr(value)}")

View File

@ -1,25 +1,4 @@
class Context: class Context(object):
"""
Context can be used to make a convenient container for attributes to provide
for execution for resolvers of a GraphQL operation like a query.
.. code:: python
from graphene import Context
context = Context(loaders=build_dataloaders(), request=my_web_request)
schema.execute('{ hello(name: "world") }', context=context)
def resolve_hello(parent, info, name):
info.context.request # value set in Context
info.context.loaders # value set in Context
# ...
args:
**params (Dict[str, Any]): values to make available on Context instance as attributes.
"""
def __init__(self, **params): def __init__(self, **params):
for key, value in params.items(): for key, value in params.items():
setattr(self, key, value) setattr(self, key, value)

View File

@ -1,9 +1,9 @@
from __future__ import absolute_import
import datetime import datetime
from dateutil.parser import isoparse from aniso8601 import parse_date, parse_datetime, parse_time
from graphql.language import ast
from graphql.error import GraphQLError
from graphql.language import StringValueNode, print_ast
from .scalars import Scalar from .scalars import Scalar
@ -19,28 +19,22 @@ class Date(Scalar):
def serialize(date): def serialize(date):
if isinstance(date, datetime.datetime): if isinstance(date, datetime.datetime):
date = date.date() date = date.date()
if not isinstance(date, datetime.date): assert isinstance(
raise GraphQLError(f"Date cannot represent value: {repr(date)}") date, datetime.date
), 'Received not compatible date "{}"'.format(repr(date))
return date.isoformat() return date.isoformat()
@classmethod @classmethod
def parse_literal(cls, node, _variables=None): def parse_literal(cls, node):
if not isinstance(node, StringValueNode): if isinstance(node, ast.StringValue):
raise GraphQLError( return cls.parse_value(node.value)
f"Date cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod @staticmethod
def parse_value(value): def parse_value(value):
if isinstance(value, datetime.date):
return value
if not isinstance(value, str):
raise GraphQLError(f"Date cannot represent non-string value: {repr(value)}")
try: try:
return datetime.date.fromisoformat(value) return parse_date(value)
except ValueError: except ValueError:
raise GraphQLError(f"Date cannot represent value: {repr(value)}") return None
class DateTime(Scalar): class DateTime(Scalar):
@ -52,30 +46,22 @@ class DateTime(Scalar):
@staticmethod @staticmethod
def serialize(dt): def serialize(dt):
if not isinstance(dt, (datetime.datetime, datetime.date)): assert isinstance(
raise GraphQLError(f"DateTime cannot represent value: {repr(dt)}") dt, (datetime.datetime, datetime.date)
), 'Received not compatible datetime "{}"'.format(repr(dt))
return dt.isoformat() return dt.isoformat()
@classmethod @classmethod
def parse_literal(cls, node, _variables=None): def parse_literal(cls, node):
if not isinstance(node, StringValueNode): if isinstance(node, ast.StringValue):
raise GraphQLError( return cls.parse_value(node.value)
f"DateTime cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod @staticmethod
def parse_value(value): def parse_value(value):
if isinstance(value, datetime.datetime):
return value
if not isinstance(value, str):
raise GraphQLError(
f"DateTime cannot represent non-string value: {repr(value)}"
)
try: try:
return isoparse(value) return parse_datetime(value)
except ValueError: except ValueError:
raise GraphQLError(f"DateTime cannot represent value: {repr(value)}") return None
class Time(Scalar): class Time(Scalar):
@ -87,25 +73,19 @@ class Time(Scalar):
@staticmethod @staticmethod
def serialize(time): def serialize(time):
if not isinstance(time, datetime.time): assert isinstance(
raise GraphQLError(f"Time cannot represent value: {repr(time)}") time, datetime.time
), 'Received not compatible time "{}"'.format(repr(time))
return time.isoformat() return time.isoformat()
@classmethod @classmethod
def parse_literal(cls, node, _variables=None): def parse_literal(cls, node):
if not isinstance(node, StringValueNode): if isinstance(node, ast.StringValue):
raise GraphQLError( return cls.parse_value(node.value)
f"Time cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@classmethod @classmethod
def parse_value(cls, value): def parse_value(cls, value):
if isinstance(value, datetime.time):
return value
if not isinstance(value, str):
raise GraphQLError(f"Time cannot represent non-string value: {repr(value)}")
try: try:
return datetime.time.fromisoformat(value) return parse_time(value)
except ValueError: except ValueError:
raise GraphQLError(f"Time cannot represent value: {repr(value)}") return None

View File

@ -1,34 +0,0 @@
from decimal import Decimal as _Decimal
from graphql import Undefined
from graphql.language.ast import StringValueNode, IntValueNode
from .scalars import Scalar
class Decimal(Scalar):
"""
The `Decimal` scalar type represents a python Decimal.
"""
@staticmethod
def serialize(dec):
if isinstance(dec, str):
dec = _Decimal(dec)
assert isinstance(
dec, _Decimal
), f'Received not compatible Decimal "{repr(dec)}"'
return str(dec)
@classmethod
def parse_literal(cls, node, _variables=None):
if isinstance(node, (StringValueNode, IntValueNode)):
return cls.parse_value(node.value)
return Undefined
@staticmethod
def parse_value(value):
try:
return _Decimal(value)
except Exception:
return Undefined

View File

@ -1,5 +1,3 @@
from enum import Enum as PyEnum
from graphql import ( from graphql import (
GraphQLEnumType, GraphQLEnumType,
GraphQLInputObjectType, GraphQLInputObjectType,
@ -10,7 +8,7 @@ from graphql import (
) )
class GrapheneGraphQLType: class GrapheneGraphQLType(object):
""" """
A class for extending the base GraphQLType with the related A class for extending the base GraphQLType with the related
graphene_type graphene_type
@ -20,11 +18,6 @@ class GrapheneGraphQLType:
self.graphene_type = kwargs.pop("graphene_type") self.graphene_type = kwargs.pop("graphene_type")
super(GrapheneGraphQLType, self).__init__(*args, **kwargs) super(GrapheneGraphQLType, self).__init__(*args, **kwargs)
def __copy__(self):
result = GrapheneGraphQLType(graphene_type=self.graphene_type)
result.__dict__.update(self.__dict__)
return result
class GrapheneInterfaceType(GrapheneGraphQLType, GraphQLInterfaceType): class GrapheneInterfaceType(GrapheneGraphQLType, GraphQLInterfaceType):
pass pass
@ -43,19 +36,7 @@ class GrapheneScalarType(GrapheneGraphQLType, GraphQLScalarType):
class GrapheneEnumType(GrapheneGraphQLType, GraphQLEnumType): class GrapheneEnumType(GrapheneGraphQLType, GraphQLEnumType):
def serialize(self, value): pass
if not isinstance(value, PyEnum):
enum = self.graphene_type._meta.enum
try:
# Try and get enum by value
value = enum(value)
except ValueError:
# Try and get enum by name
try:
value = enum[value]
except KeyError:
pass
return super(GrapheneEnumType, self).serialize(value)
class GrapheneInputObjectType(GrapheneGraphQLType, GraphQLInputObjectType): class GrapheneInputObjectType(GrapheneGraphQLType, GraphQLInputObjectType):

View File

@ -10,10 +10,10 @@ class Dynamic(MountedType):
the schema. So we can have lazy fields. the schema. So we can have lazy fields.
""" """
def __init__(self, type_, with_schema=False, _creation_counter=None): def __init__(self, type, with_schema=False, _creation_counter=None):
super(Dynamic, self).__init__(_creation_counter=_creation_counter) super(Dynamic, self).__init__(_creation_counter=_creation_counter)
assert inspect.isfunction(type_) or isinstance(type_, partial) assert inspect.isfunction(type) or isinstance(type, partial)
self.type = type_ self.type = type
self.with_schema = with_schema self.with_schema = with_schema
def get_type(self, schema=None): def get_type(self, schema=None):

View File

@ -1,7 +1,10 @@
from enum import Enum as PyEnum from collections import OrderedDict
import six
from graphene.utils.subclass_with_meta import SubclassWithMeta_Meta from graphene.utils.subclass_with_meta import SubclassWithMeta_Meta
from ..pyutils.compat import Enum as PyEnum
from .base import BaseOptions, BaseType from .base import BaseOptions, BaseType
from .unmountedtype import UnmountedType from .unmountedtype import UnmountedType
@ -12,10 +15,6 @@ def eq_enum(self, other):
return self.value is other return self.value is other
def hash_enum(self):
return hash(self.name)
EnumType = type(PyEnum) EnumType = type(PyEnum)
@ -25,17 +24,15 @@ class EnumOptions(BaseOptions):
class EnumMeta(SubclassWithMeta_Meta): class EnumMeta(SubclassWithMeta_Meta):
def __new__(cls, name_, bases, classdict, **options): def __new__(cls, name, bases, classdict, **options):
enum_members = dict(classdict, __eq__=eq_enum, __hash__=hash_enum) enum_members = OrderedDict(classdict, __eq__=eq_enum)
# We remove the Meta attribute from the class to not collide # We remove the Meta attribute from the class to not collide
# with the enum values. # with the enum values.
enum_members.pop("Meta", None) enum_members.pop("Meta", None)
enum = PyEnum(cls.__name__, enum_members) enum = PyEnum(cls.__name__, enum_members)
obj = SubclassWithMeta_Meta.__new__( return SubclassWithMeta_Meta.__new__(
cls, name_, bases, dict(classdict, __enum__=enum), **options cls, name, bases, OrderedDict(classdict, __enum__=enum), **options
) )
globals()[name_] = obj.__enum__
return obj
def get(cls, value): def get(cls, value):
return cls._meta.enum(value) return cls._meta.enum(value)
@ -44,60 +41,27 @@ class EnumMeta(SubclassWithMeta_Meta):
return cls._meta.enum[value] return cls._meta.enum[value]
def __prepare__(name, bases, **kwargs): # noqa: N805 def __prepare__(name, bases, **kwargs): # noqa: N805
return {} return OrderedDict()
def __call__(cls, *args, **kwargs): # noqa: N805 def __call__(cls, *args, **kwargs): # noqa: N805
if cls is Enum: if cls is Enum:
description = kwargs.pop("description", None) description = kwargs.pop("description", None)
deprecation_reason = kwargs.pop("deprecation_reason", None) return cls.from_enum(PyEnum(*args, **kwargs), description=description)
return cls.from_enum(
PyEnum(*args, **kwargs),
description=description,
deprecation_reason=deprecation_reason,
)
return super(EnumMeta, cls).__call__(*args, **kwargs) return super(EnumMeta, cls).__call__(*args, **kwargs)
# return cls._meta.enum(*args, **kwargs) # return cls._meta.enum(*args, **kwargs)
def __iter__(cls): def from_enum(cls, enum, description=None, deprecation_reason=None): # noqa: N805
return cls._meta.enum.__iter__() description = description or enum.__doc__
def from_enum(cls, enum, name=None, description=None, deprecation_reason=None): # noqa: N805
name = name or enum.__name__
description = description or enum.__doc__ or "An enumeration."
meta_dict = { meta_dict = {
"enum": enum, "enum": enum,
"description": description, "description": description,
"deprecation_reason": deprecation_reason, "deprecation_reason": deprecation_reason,
} }
meta_class = type("Meta", (object,), meta_dict) meta_class = type("Meta", (object,), meta_dict)
return type(name, (Enum,), {"Meta": meta_class}) return type(meta_class.enum.__name__, (Enum,), {"Meta": meta_class})
class Enum(UnmountedType, BaseType, metaclass=EnumMeta): class Enum(six.with_metaclass(EnumMeta, UnmountedType, BaseType)):
"""
Enum type definition
Defines a static set of values that can be provided as a Field, Argument or InputField.
.. code:: python
from graphene import Enum
class NameFormat(Enum):
FIRST_LAST = "first_last"
LAST_FIRST = "last_first"
Meta:
enum (optional, Enum): Python enum to use as a base for GraphQL Enum.
name (optional, str): Name of the GraphQL type (must be unique in schema). Defaults to class
name.
description (optional, str): Description of the GraphQL type in the schema. Defaults to class
docstring.
deprecation_reason (optional, str): Setting this value indicates that the enum is
depreciated and may provide instruction or reason on how for clients to proceed.
"""
@classmethod @classmethod
def __init_subclass_with_meta__(cls, enum=None, _meta=None, **options): def __init_subclass_with_meta__(cls, enum=None, _meta=None, **options):
if not _meta: if not _meta:

View File

@ -1,72 +1,27 @@
import inspect import inspect
from collections.abc import Mapping from collections import Mapping, OrderedDict
from functools import partial from functools import partial
from .argument import Argument, to_arguments from .argument import Argument, to_arguments
from .mountedtype import MountedType from .mountedtype import MountedType
from .resolver import default_resolver
from .structures import NonNull from .structures import NonNull
from .unmountedtype import UnmountedType from .unmountedtype import UnmountedType
from .utils import get_type from .utils import get_type
from ..utils.deprecated import warn_deprecation
base_type = type base_type = type
def source_resolver(source, root, info, **args): def source_resolver(source, root, info, **args):
resolved = default_resolver(source, None, root, info, **args) resolved = getattr(root, source, None)
if inspect.isfunction(resolved) or inspect.ismethod(resolved): if inspect.isfunction(resolved) or inspect.ismethod(resolved):
return resolved() return resolved()
return resolved return resolved
class Field(MountedType): class Field(MountedType):
"""
Makes a field available on an ObjectType in the GraphQL schema. Any type can be mounted as a
Field:
- Object Type
- Scalar Type
- Enum
- Interface
- Union
All class attributes of ``graphene.ObjectType`` are implicitly mounted as Field using the below
arguments.
.. code:: python
class Person(ObjectType):
first_name = graphene.String(required=True) # implicitly mounted as Field
last_name = graphene.Field(String, description='Surname') # explicitly mounted as Field
args:
type (class for a graphene.UnmountedType): Must be a class (not an instance) of an
unmounted graphene type (ex. scalar or object) which is used for the type of this
field in the GraphQL schema. You can provide a dotted module import path (string)
to the class instead of the class itself (e.g. to avoid circular import issues).
args (optional, Dict[str, graphene.Argument]): Arguments that can be input to the field.
Prefer to use ``**extra_args``, unless you use an argument name that clashes with one
of the Field arguments presented here (see :ref:`example<ResolverParamGraphQLArguments>`).
resolver (optional, Callable): A function to get the value for a Field from the parent
value object. If not set, the default resolver method for the schema is used.
source (optional, str): attribute name to resolve for this field from the parent value
object. Alternative to resolver (cannot set both source and resolver).
deprecation_reason (optional, str): Setting this value indicates that the field is
depreciated and may provide instruction or reason on how for clients to proceed.
required (optional, bool): indicates this field as not null in the graphql schema. Same behavior as
graphene.NonNull. Default False.
name (optional, str): the name of the GraphQL field (must be unique in a type). Defaults to attribute
name.
description (optional, str): the description of the GraphQL field in the schema.
default_value (optional, Any): Default value to resolve if none set from schema.
**extra_args (optional, Dict[str, Union[graphene.Argument, graphene.UnmountedType]): any
additional arguments to mount on the field.
"""
def __init__( def __init__(
self, self,
type_, type,
args=None, args=None,
resolver=None, resolver=None,
source=None, source=None,
@ -76,21 +31,21 @@ class Field(MountedType):
required=False, required=False,
_creation_counter=None, _creation_counter=None,
default_value=None, default_value=None,
**extra_args, **extra_args
): ):
super(Field, self).__init__(_creation_counter=_creation_counter) super(Field, self).__init__(_creation_counter=_creation_counter)
assert not args or isinstance( assert not args or isinstance(args, Mapping), (
args, Mapping 'Arguments in a field have to be a mapping, received "{}".'
), f'Arguments in a field have to be a mapping, received "{args}".' ).format(args)
assert not ( assert not (
source and resolver source and resolver
), "A Field cannot have a source and a resolver in at the same time." ), "A Field cannot have a source and a resolver in at the same time."
assert not callable( assert not callable(default_value), (
default_value 'The default value can not be a function but received "{}".'
), f'The default value can not be a function but received "{base_type(default_value)}".' ).format(base_type(default_value))
if required: if required:
type_ = NonNull(type_) type = NonNull(type)
# Check if name is actually an argument of the field # Check if name is actually an argument of the field
if isinstance(name, (Argument, UnmountedType)): if isinstance(name, (Argument, UnmountedType)):
@ -103,8 +58,8 @@ class Field(MountedType):
source = None source = None
self.name = name self.name = name
self._type = type_ self._type = type
self.args = to_arguments(args or {}, extra_args) self.args = to_arguments(args or OrderedDict(), extra_args)
if source: if source:
resolver = partial(source_resolver, source) resolver = partial(source_resolver, source)
self.resolver = resolver self.resolver = resolver
@ -116,24 +71,5 @@ class Field(MountedType):
def type(self): def type(self):
return get_type(self._type) return get_type(self._type)
get_resolver = None def get_resolver(self, parent_resolver):
def wrap_resolve(self, parent_resolver):
"""
Wraps a function resolver, using the ObjectType resolve_{FIELD_NAME}
(parent_resolver) if the Field definition has no resolver.
"""
if self.get_resolver is not None:
warn_deprecation(
"The get_resolver method is being deprecated, please rename it to wrap_resolve."
)
return self.get_resolver(parent_resolver)
return self.resolver or parent_resolver return self.resolver or parent_resolver
def wrap_subscribe(self, parent_subscribe):
"""
Wraps a function subscribe, using the ObjectType subscribe_{FIELD_NAME}
(parent_subscribe) if the Field definition has no subscribe.
"""
return parent_subscribe

Some files were not shown because too many files have changed in this diff Show More