|
4 | 4 |
|
5 | 5 | Adds GraphQL support to your WebOb (Pyramid, Pylons, ...) application.
|
6 | 6 |
|
7 |
| -## Installation |
8 |
| - |
9 |
| -For instaling WebOb-GraphQL, just run this command in your shell |
10 |
| - |
11 |
| -```bash |
12 |
| -pip install "webob-graphql>=1.0.dev" |
13 |
| -``` |
14 |
| - |
15 |
| - |
16 | 7 | ## Usage
|
17 | 8 |
|
18 |
| -Just use the `serve_graphql_request` function from `webob_graphql` |
19 |
| - |
| 9 | +Use the `GraphQLView` view from `webob_graphql` |
20 | 10 |
|
21 | 11 | ### Pyramid
|
22 | 12 |
|
23 | 13 | ```python
|
| 14 | +from wsgiref.simple_server import make_server |
24 | 15 | from pyramid.view import view_config
|
25 | 16 |
|
26 | 17 | from webob_graphql import serve_graphql_request
|
27 | 18 |
|
| 19 | +from schema import schema |
28 | 20 |
|
29 |
| -@view_config( |
30 |
| - route_name='graphql', |
31 |
| - # The serve_graphql_request method will detect what's the best renderer |
32 |
| - # to use, so it will do the json render automatically. |
33 |
| - # In summary, don't use the renderer='json' here :) |
34 |
| -) |
35 | 21 | def graphql_view(request):
|
36 |
| - context = {'session': request.session} |
37 |
| - return serve_graphql_request(request, schema, context_value=context) |
38 |
| - |
39 |
| - # Optional, for adding batch query support (used in Apollo-Client) |
40 |
| - return serve_graphql_request(request, schema, batch_enabled=True, context_value=context) |
| 22 | + return GraphQLView(request=request, schema=schema, graphiql=True).dispatch_request(request) |
| 23 | + |
| 24 | +if __name__ == '__main__': |
| 25 | + with Configurator() as config: |
| 26 | + config.add_route('graphql', '/graphql') |
| 27 | + config.add_view(graphql_view, route_name='graphql') |
| 28 | + app = config.make_wsgi_app() |
| 29 | + server = make_server('0.0.0.0', 6543, app) |
| 30 | + server.serve_forever() |
41 | 31 | ```
|
| 32 | +This will add `/graphql` endpoint to your app and enable the GraphiQL IDE. |
| 33 | + |
| 34 | +### Supported options for GraphQLView |
42 | 35 |
|
43 |
| -### Supported options |
44 | 36 | * `schema`: The `GraphQLSchema` object that you want the view to execute when it gets a valid request.
|
45 |
| - * `context_value`: A value to pass as the `context` to the `graphql()` function. |
46 |
| - * `root_value`: The `root_value` you want to provide to `executor.execute`. |
47 |
| - * `format_error`: If you want to use a custom error formatter. |
| 37 | + * `context`: A value to pass as the `context_value` to graphql `execute` function. By default is set to `dict` with request object at key `request`. |
| 38 | + * `root_value`: The `root_value` you want to provide to graphql `execute`. |
48 | 39 | * `pretty`: Whether or not you want the response to be pretty printed JSON.
|
49 |
| - * `executor`: The `Executor` that you want to use to execute queries. |
50 |
| - * `graphiql_enabled`: If `True` (default), may present [GraphiQL](https://github.com/graphql/graphiql) when loaded directly from a browser (a useful tool for debugging and exploration). |
51 |
| - * `render_graphiql`: A custom function for rendering GraphiQL (this function should have the arguments `result` and `params`). |
52 |
| - * `batch_enabled`: Enable batch support (for using in [Apollo-Client](http://dev.apollodata.com/core/network.html#query-batching) or [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer)) |
| 40 | + * `graphiql`: If `True`, may present [GraphiQL](https://github.com/graphql/graphiql) when loaded directly from a browser (a useful tool for debugging and exploration). |
| 41 | + * `graphiql_version`: The graphiql version to load. Defaults to **"1.0.3"**. |
| 42 | + * `graphiql_template`: Inject a Jinja template string to customize GraphiQL. |
| 43 | + * `graphiql_html_title`: The graphiql title to display. Defaults to **"GraphiQL"**. |
| 44 | + * `batch`: Set the GraphQL view as batch (for using in [Apollo-Client](http://dev.apollodata.com/core/network.html#query-batching) or [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer)) |
| 45 | + * `middleware`: A list of graphql [middlewares](http://docs.graphene-python.org/en/latest/execution/middleware/). |
| 46 | + * `encode`: the encoder to use for responses (sensibly defaults to `graphql_server.json_encode`). |
| 47 | + * `format_error`: the error formatter to use for responses (sensibly defaults to `graphql_server.default_format_error`. |
| 48 | + * `enable_async`: whether `async` mode will be enabled. |
| 49 | + * `subscriptions`: The GraphiQL socket endpoint for using subscriptions in graphql-ws. |
| 50 | + * `headers`: An optional GraphQL string to use as the initial displayed request headers, if not provided, the stored headers will be used. |
| 51 | + * `default_query`: An optional GraphQL string to use when no query is provided and no stored query exists from a previous session. If not provided, GraphiQL will use its own default query. |
| 52 | +* `header_editor_enabled`: An optional boolean which enables the header editor when true. Defaults to **false**. |
| 53 | +* `should_persist_headers`: An optional boolean which enables to persist headers to storage when true. Defaults to **false**. |
| 54 | + |
| 55 | +## Contributing |
| 56 | +Since v3, `webob-graphql` code lives at [graphql-server](https://github.com/graphql-python/graphql-server) repository to keep any breaking change on the base package on sync with all other integrations. In order to contribute, please take a look at [CONTRIBUTING.md](https://github.com/graphql-python/graphql-server/blob/master/CONTRIBUTING.md). |
0 commit comments