2023-03-03 19:35:46 +03:00
# ![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) [![](https://dcbadge.vercel.app/api/server/T6Gp6NFYHe?style=flat)](https://discord.gg/T6Gp6NFYHe)
2020-01-30 19:17:12 +03:00
2023-03-03 19:35:05 +03:00
[💬 Join the community on Discord ](https://discord.gg/T6Gp6NFYHe )
2020-01-30 19:17:12 +03:00
2022-09-06 14:42:38 +03:00
**We are looking for contributors**! Please check the current issues to see how you can help ❤️
2016-09-09 07:30:30 +03:00
2018-10-26 20:30:37 +03:00
## Introduction
2020-01-30 19:17:12 +03:00
[Graphene ](http://graphene-python.org ) is an opinionated Python library for building GraphQL schemas/types fast and easily.
2015-10-30 23:40:36 +03:00
- **Easy to use:** Graphene helps you use GraphQL in Python without effort.
2017-02-22 21:42:12 +03:00
- **Relay:** Graphene has builtin support for Relay.
2022-09-06 14:42:38 +03:00
- **Data agnostic:** Graphene supports any kind of data source: SQL (Django, SQLAlchemy), Mongo, custom Python objects, etc.
2016-09-18 05:39:35 +03:00
We believe that by providing a complete API you could plug Graphene anywhere your data lives and make your data available
through GraphQL.
2015-09-24 12:11:50 +03:00
2016-09-18 05:39:35 +03:00
## Integrations
Graphene has multiple integrations with different frameworks:
2018-08-31 18:47:05 +03:00
| integration | Package |
| ----------------- | --------------------------------------------------------------------------------------- |
| SQLAlchemy | [graphene-sqlalchemy ](https://github.com/graphql-python/graphene-sqlalchemy/ ) |
2022-09-06 14:42:38 +03:00
| Mongo | [graphene-mongo ](https://github.com/graphql-python/graphene-mongo/ ) |
| Apollo Federation | [graphene-federation ](https://github.com/graphql-python/graphene-federation/ ) |
| Django | [graphene-django ](https://github.com/graphql-python/graphene-django/ ) |
2015-10-30 10:01:44 +03:00
2017-02-18 23:50:58 +03:00
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 ).
2015-10-30 10:01:44 +03:00
2015-10-07 06:59:07 +03:00
## Installation
2022-08-17 02:21:29 +03:00
To install `graphene` , just run this command in your shell
2015-10-07 06:59:07 +03:00
```bash
2022-09-06 14:42:38 +03:00
pip install "graphene>=3.1"
2015-10-07 06:59:07 +03:00
```
2015-10-27 09:54:51 +03:00
## Examples
2015-09-24 12:17:56 +03:00
2017-01-22 11:46:31 +03:00
Here is one example for you to get started:
2015-09-24 12:17:56 +03:00
```python
2020-04-14 12:25:10 +03:00
import graphene
2015-10-27 09:54:51 +03:00
class Query(graphene.ObjectType):
2015-11-12 09:15:28 +03:00
hello = graphene.String(description='A typical hello world')
2015-09-24 12:17:56 +03:00
2017-07-27 13:00:21 +03:00
def resolve_hello(self, info):
2015-10-27 09:54:51 +03:00
return 'World'
2015-09-24 12:17:56 +03:00
2015-10-01 12:08:00 +03:00
schema = graphene.Schema(query=Query)
2015-09-24 12:17:56 +03:00
```
2015-10-07 09:13:10 +03:00
Then Querying `graphene.Schema` is as simple as:
2015-09-24 12:17:56 +03:00
```python
query = '''
2015-10-27 09:54:51 +03:00
query SayHello {
hello
2015-09-24 12:17:56 +03:00
}
'''
2015-10-01 12:08:00 +03:00
result = schema.execute(query)
2015-09-24 12:17:56 +03:00
```
2015-10-30 10:01:44 +03:00
If you want to learn even more, you can also check the following [examples ](examples/ ):
2015-09-26 09:31:53 +03:00
2018-08-31 18:47:05 +03:00
- **Basic Schema**: [Starwars example ](examples/starwars )
- **Relay Schema**: [Starwars Relay example ](examples/starwars_relay )
2015-09-26 09:31:53 +03:00
2019-03-10 04:35:15 +03:00
## Documentation
Documentation and links to additional resources are available at
https://docs.graphene-python.org/en/latest/
2015-09-24 12:11:50 +03:00
## Contributing
2018-06-12 23:38:07 +03:00
After cloning this repo, create a [virtualenv ](https://virtualenv.pypa.io/en/stable/ ) and ensure dependencies are installed by running:
2015-09-24 12:11:50 +03:00
```sh
2018-06-12 23:38:07 +03:00
virtualenv venv
source venv/bin/activate
2017-03-05 05:17:53 +03:00
pip install -e ".[test]"
2015-09-24 12:11:50 +03:00
```
2018-05-30 14:53:44 +03:00
Well-written tests and maintaining good test coverage is important to this project. While developing, run new and existing tests with:
2015-09-24 12:11:50 +03:00
```sh
2022-09-06 14:42:38 +03:00
pytest graphene/relay/tests/test_node.py # Single file
pytest graphene/relay # All tests in directory
2017-02-08 08:54:50 +03:00
```
2018-05-30 14:53:44 +03:00
Add the `-s` flag if you have introduced breakpoints into the code for debugging.
2018-06-12 23:38:07 +03:00
Add the `-v` ("verbose") flag to get more detailed test output. For even more detailed output, use `-vv` .
2018-08-31 18:47:05 +03:00
Check out the [pytest documentation ](https://docs.pytest.org/en/latest/ ) for more options and test running controls.
2018-05-30 14:53:44 +03:00
2022-09-06 14:42:38 +03:00
Regularly ensure your `pre-commit` hooks are up to date and enabled:
```sh
pre-commit install
```
2017-02-08 08:54:50 +03:00
You can also run the benchmarks with:
```sh
2022-09-06 14:42:38 +03:00
pytest graphene --benchmark-only
2015-09-24 12:11:50 +03:00
```
2016-11-15 06:38:45 +03:00
2019-05-07 22:16:22 +03:00
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:
2018-08-31 18:47:05 +03:00
2018-05-30 14:53:44 +03:00
```sh
tox
```
2018-08-31 18:47:05 +03:00
2018-06-12 23:38:07 +03:00
If you wish to run against a specific version defined in the `tox.ini` file:
2018-08-31 18:47:05 +03:00
2018-05-30 14:53:44 +03:00
```sh
2022-09-06 14:42:38 +03:00
tox -e py39
2018-05-30 14:53:44 +03:00
```
2018-08-31 18:47:05 +03:00
2022-09-06 14:42:38 +03:00
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!
2016-11-15 06:38:45 +03:00
2019-03-10 04:35:15 +03:00
### Building Documentation
2016-11-15 06:38:45 +03:00
The documentation is generated using the excellent [Sphinx ](http://www.sphinx-doc.org/ ) and a custom theme.
2018-10-13 21:27:57 +03:00
An HTML version of the documentation is produced by running:
2016-11-15 06:38:45 +03:00
```sh
2018-10-13 21:27:57 +03:00
make docs
2016-11-15 06:38:45 +03:00
```