METADATA 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. Metadata-Version: 2.1
  2. Name: graphql-relay
  3. Version: 3.2.0
  4. Summary: Relay library for graphql-core
  5. Home-page: https://github.com/graphql-python/graphql-relay-py
  6. Author: Syrus Akbary
  7. Author-email: me@syrusakbary.com
  8. License: MIT
  9. Keywords: graphql relay api
  10. Platform: UNKNOWN
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Intended Audience :: Developers
  13. Classifier: Topic :: Software Development :: Libraries
  14. Classifier: License :: OSI Approved :: MIT License
  15. Classifier: Programming Language :: Python :: 3
  16. Classifier: Programming Language :: Python :: 3.6
  17. Classifier: Programming Language :: Python :: 3.7
  18. Classifier: Programming Language :: Python :: 3.8
  19. Classifier: Programming Language :: Python :: 3.9
  20. Classifier: Programming Language :: Python :: 3.10
  21. Classifier: Programming Language :: Python :: Implementation :: PyPy
  22. Requires-Python: >=3.6,<4
  23. Description-Content-Type: text/markdown
  24. License-File: LICENSE
  25. Requires-Dist: graphql-core (<3.3,>=3.2)
  26. Requires-Dist: typing-extensions (<5,>=4.1) ; python_version < "3.8"
  27. # Relay Library for GraphQL Python
  28. GraphQL-relay-py is the [Relay](https://relay.dev/) library for
  29. [GraphQL-core](https://github.com/graphql-python/graphql-core).
  30. It allows the easy creation of Relay-compliant servers using GraphQL-core.
  31. GraphQL-Relay-Py is a Python port of
  32. [graphql-relay-js](https://github.com/graphql/graphql-relay-js),
  33. while GraphQL-Core is a Python port of
  34. [GraphQL.js](https://github.com/graphql/graphql-js),
  35. the reference implementation of GraphQL for JavaScript.
  36. Since version 3, GraphQL-Relay-Py and GraphQL-Core support Python 3.6 and above only.
  37. For older versions of Python, you can use version 2 of these libraries.
  38. [![PyPI version](https://badge.fury.io/py/graphql-relay.svg)](https://badge.fury.io/py/graphql-relay)
  39. ![Test Status](https://github.com/graphql-python/graphql-relay-py/actions/workflows/test.yml/badge.svg)
  40. ![Lint Status](https://github.com/graphql-python/graphql-relay-py/actions/workflows/lint.yml/badge.svg)
  41. [![Dependency Updates](https://pyup.io/repos/github/graphql-python/graphql-relay-py/shield.svg)](https://pyup.io/repos/github/graphql-python/graphql-relay-py/)
  42. [![Python 3 Status](https://pyup.io/repos/github/graphql-python/graphql-relay-py/python-3-shield.svg)](https://pyup.io/repos/github/graphql-python/graphql-relay-py/)
  43. [![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
  44. ## Getting Started
  45. A basic understanding of GraphQL and of the GraphQL Python implementation is needed
  46. to provide context for this library.
  47. An overview of GraphQL in general is available in the
  48. [README](https://github.com/graphql-python/graphql-core/blob/master/README.md) for the
  49. [Specification for GraphQL](https://github.com/graphql-python/graphql-core).
  50. This library is designed to work with the
  51. the [GraphQL-Core](https://github.com/graphql-python/graphql-core)
  52. Python reference implementation of a GraphQL server.
  53. An overview of the functionality that a Relay-compliant GraphQL server should provide
  54. is in the [GraphQL Relay Specification](https://facebook.github.io/relay/docs/graphql-relay-specification.html)
  55. on the [Relay website](https://facebook.github.io/relay/).
  56. That overview describes a simple set of examples that exist
  57. as [tests](tests) in this repository.
  58. A good way to get started with this repository is to walk through that documentation
  59. and the corresponding tests in this library together.
  60. ## Using Relay Library for GraphQL Python (graphql-core)
  61. Install Relay Library for GraphQL Python
  62. ```sh
  63. pip install graphql-core
  64. pip install graphql-relay
  65. ```
  66. When building a schema for [GraphQL](https://github.com/graphql-python/graphql-core),
  67. the provided library functions can be used to simplify the creation of Relay patterns.
  68. All the functions that are explained in the following sections must be
  69. imported from the top level of the `graphql_relay` package, like this:
  70. ```python
  71. from graphql_relay import connection_definitions
  72. ```
  73. ### Connections
  74. Helper functions are provided for both building the GraphQL types
  75. for connections and for implementing the `resolve` method for fields
  76. returning those types.
  77. - `connection_args` returns the arguments that fields should provide when
  78. they return a connection type that supports bidirectional pagination.
  79. - `forward_connection_args` returns the arguments that fields should provide when
  80. they return a connection type that only supports forward pagination.
  81. - `backward_connection_args` returns the arguments that fields should provide when
  82. they return a connection type that only supports backward pagination.
  83. - `connection_definitions` returns a `connection_type` and its associated
  84. `edgeType`, given a name and a node type.
  85. - `connection_from_array` is a helper method that takes an array and the
  86. arguments from `connection_args`, does pagination and filtering, and returns
  87. an object in the shape expected by a `connection_type`'s `resolve` function.
  88. - `cursor_for_object_in_connection` is a helper method that takes an array and a
  89. member object, and returns a cursor for use in the mutation payload.
  90. - `offset_to_cursor` takes the index of a member object in an array
  91. and returns an opaque cursor for use in the mutation payload.
  92. - `cursor_to_offset` takes an opaque cursor (created with `offset_to_cursor`)
  93. and returns the corresponding array index.
  94. An example usage of these methods from the [test schema](tests/star_wars_schema.py):
  95. ```python
  96. ship_edge, ship_connection = connection_definitions(ship_type, "Ship")
  97. faction_type = GraphQLObjectType(
  98. name="Faction",
  99. description="A faction in the Star Wars saga",
  100. fields=lambda: {
  101. "id": global_id_field("Faction"),
  102. "name": GraphQLField(GraphQLString, description="The name of the faction."),
  103. "ships": GraphQLField(
  104. ship_connection,
  105. description="The ships used by the faction.",
  106. args=connection_args,
  107. resolve=lambda faction, _info, **args: connection_from_array(
  108. [get_ship(ship) for ship in faction.ships], args
  109. ),
  110. ),
  111. },
  112. interfaces=[node_interface],
  113. )
  114. ```
  115. This shows adding a `ships` field to the `Faction` object that is a connection.
  116. It uses `connection_definitions(ship_type, "Ship")` to create the connection
  117. type, adds `connection_args` as arguments on this function, and then implements
  118. the resolver function by passing the array of ships and the arguments to
  119. `connection_from_array`.
  120. ### Object Identification
  121. Helper functions are provided for both building the GraphQL types
  122. for nodes and for implementing global IDs around local IDs.
  123. - `node_definitions` returns the `Node` interface that objects can implement,
  124. and returns the `node` root field to include on the query type.
  125. To implement this, it takes a function to resolve an ID to an object,
  126. and to determine the type of a given object.
  127. - `to_global_id` takes a type name and an ID specific to that type name,
  128. and returns a "global ID" that is unique among all types.
  129. - `from_global_id` takes the "global ID" created by `to_global_id`, and
  130. returns the type name and ID used to create it.
  131. - `global_id_field` creates the configuration for an `id` field on a node.
  132. - `plural_identifying_root_field` creates a field that accepts a list of
  133. non-ID identifiers (like a username) and maps then to their corresponding
  134. objects.
  135. An example usage of these methods from the [test schema](tests/star_wars_schema.py):
  136. ```python
  137. def get_node(global_id, _info):
  138. type_, id_ = from_global_id(global_id)
  139. if type_ == "Faction":
  140. return get_faction(id_)
  141. if type_ == "Ship":
  142. return get_ship(id_)
  143. return None # pragma: no cover
  144. def get_node_type(obj, _info, _type):
  145. if isinstance(obj, Faction):
  146. return faction_type.name
  147. return ship_type.name
  148. node_interface, node_field = node_definitions(get_node, get_node_type)[:2]
  149. faction_type = GraphQLObjectType(
  150. name="Faction",
  151. description="A faction in the Star Wars saga",
  152. fields=lambda: {
  153. "id": global_id_field("Faction"),
  154. "name": GraphQLField(GraphQLString, description="The name of the faction."),
  155. "ships": GraphQLField(
  156. ship_connection,
  157. description="The ships used by the faction.",
  158. args=connection_args,
  159. resolve=lambda faction, _info, **args: connection_from_array(
  160. [get_ship(ship) for ship in faction.ships], args
  161. ),
  162. ),
  163. },
  164. interfaces=[node_interface],
  165. )
  166. query_type = GraphQLObjectType(
  167. name="Query",
  168. fields=lambda: {
  169. "rebels": GraphQLField(faction_type, resolve=lambda _obj, _info: get_rebels()),
  170. "empire": GraphQLField(faction_type, resolve=lambda _obj, _info: get_empire()),
  171. "node": node_field,
  172. },
  173. )
  174. ```
  175. This uses `node_definitions` to construct the `Node` interface and the `node`
  176. field; it uses `from_global_id` to resolve the IDs passed in the implementation
  177. of the function mapping ID to object. It then uses the `global_id_field` method to
  178. create the `id` field on `Faction`, which also ensures implements the
  179. `node_interface`. Finally, it adds the `node` field to the query type, using the
  180. `node_field` returned by `node_definitions`.
  181. ### Mutations
  182. A helper function is provided for building mutations with
  183. single inputs and client mutation IDs.
  184. - `mutation_with_client_mutation_id` takes a name, input fields, output fields,
  185. and a mutation method to map from the input fields to the output fields,
  186. performing the mutation along the way. It then creates and returns a field
  187. configuration that can be used as a top-level field on the mutation type.
  188. An example usage of these methods from the [test schema](tests/star_wars_schema.py):
  189. ```python
  190. class IntroduceShipMutation:
  191. def __init__(self, shipId, factionId, clientMutationId=None):
  192. self.shipId = shipId
  193. self.factionId = factionId
  194. self.clientMutationId = clientMutationId
  195. def mutate_and_get_payload(_info, shipName, factionId, **_input):
  196. new_ship = create_ship(shipName, factionId)
  197. return IntroduceShipMutation(shipId=new_ship.id, factionId=factionId)
  198. ship_mutation = mutation_with_client_mutation_id(
  199. "IntroduceShip",
  200. input_fields={
  201. "shipName": GraphQLInputField(GraphQLNonNull(GraphQLString)),
  202. "factionId": GraphQLInputField(GraphQLNonNull(GraphQLID)),
  203. },
  204. output_fields={
  205. "ship": GraphQLField(
  206. ship_type, resolve=lambda payload, _info: get_ship(payload.shipId)
  207. ),
  208. "faction": GraphQLField(
  209. faction_type, resolve=lambda payload, _info: get_faction(payload.factionId)
  210. ),
  211. },
  212. mutate_and_get_payload=mutate_and_get_payload,
  213. )
  214. mutation_type = GraphQLObjectType(
  215. "Mutation", fields=lambda: {"introduceShip": ship_mutation}
  216. )
  217. ```
  218. This code creates a mutation named `IntroduceShip`, which takes a faction
  219. ID and a ship name as input. It outputs the `Faction` and the `Ship` in
  220. question. `mutate_and_get_payload` then gets each input field as keyword
  221. parameter, performs the mutation by constructing the new ship, then returns
  222. an object that will be resolved by the output fields.
  223. Our mutation type then creates the `introduceShip` field using the return
  224. value of `mutation_with_client_mutation_id`.
  225. ## Contributing
  226. After cloning this repository from GitHub,
  227. we recommend using [Poetry](https://poetry.eustace.io/)
  228. to create a test environment. With poetry installed,
  229. you do this with the following command:
  230. ```sh
  231. poetry install
  232. ```
  233. You can then run the complete test suite like this:
  234. ```sh
  235. poetry run pytest
  236. ```
  237. In order to run only a part of the tests with increased verbosity,
  238. you can add pytest options, like this:
  239. ```sh
  240. poetry run pytest tests/node -vv
  241. ```
  242. In order to check the code style with flake8, use this:
  243. ```sh
  244. poetry run flake8
  245. ```
  246. Use the `tox` command to run the test suite with different
  247. Python versions and perform all additional source code checks.
  248. You can also restrict tox to an individual environment, like this:
  249. ```sh
  250. poetry run tox -e py39
  251. ```