|
4 | 4 |
|
5 | 5 | # GraphQLite bundle |
6 | 6 |
|
7 | | -Symfony bundle for the `thecodingmachine/graphqlite` package. |
8 | | -It discovers your annotated controllers and types, builds the schema, exposes the `/graphql` endpoint through a PSR-7 |
9 | | -bridge (with optional upload handling), and keeps the Symfony request available as the GraphQL context. |
| 7 | +Symfony bundle for [GraphQLite](https://graphqlite.thecodingmachine.io): build a GraphQL API by adding PHP attributes |
| 8 | +(`#[Query]`, `#[Mutation]`, `#[Type]`, `#[Field]`...) to your classes. The bundle exposes the `/graphql` endpoint and |
| 9 | +integrates GraphQLite with Symfony services, Security, Validator, file uploads and cache. |
10 | 10 |
|
11 | | -Part of the bundle docs: https://graphqlite.thecodingmachine.io/docs/symfony-bundle |
12 | | - |
13 | | -See [thecodingmachine/graphqlite](https://github.com/thecodingmachine/graphqlite). |
| 11 | +**Documentation: [docs/index.rst](docs/index.rst)** |
14 | 12 |
|
15 | 13 | ## Requirements |
16 | 14 |
|
17 | 15 | - PHP 8.1+ |
18 | | -- Supports: |
19 | | - - Symfony 6.4/7.0/8.0 |
20 | | - - GraphQLite ^8 |
21 | | - |
22 | | -## Installation |
23 | | - |
24 | | -```bash |
25 | | -composer require thecodingmachine/graphqlite-bundle |
26 | | -``` |
27 | | - |
28 | | -Ensure the bundle is enabled (Symfony Flex does this automatically via `config/bundles.php` after `composer require`). |
29 | | - |
30 | | -### Configure routes |
31 | | - |
32 | | -Import the bundle routes to expose `/graphql`: |
33 | | - |
34 | | -```yaml |
35 | | -# config/routes/graphqlite.yaml |
36 | | -graphqlite_bundle: |
37 | | - resource: '@GraphQLiteBundle/Resources/config/routes.php' |
38 | | -``` |
39 | | -
|
40 | | -### Configure namespaces |
41 | | -
|
42 | | -Tell GraphQLite where to look for controllers and types: |
43 | | -
|
44 | | -```yaml |
45 | | -# config/packages/graphqlite.yaml |
46 | | -graphqlite: |
47 | | - namespace: |
48 | | - controllers: App\\GraphQL\\Controller |
49 | | - types: |
50 | | - - App\\GraphQL\\Type |
51 | | - - App\\Entity |
52 | | -``` |
53 | | -
|
54 | | -## Quickstart |
55 | | -
|
56 | | -Create a controller with GraphQLite attributes: |
57 | | -
|
58 | | -```php |
59 | | -<?php |
60 | | -// src/GraphQL/Controller/HelloController.php |
61 | | -namespace App\GraphQL\Controller; |
62 | | - |
63 | | -use TheCodingMachine\GraphQLite\Annotations\Query; |
| 16 | +- Symfony 6.4, 7.x or 8.x |
| 17 | +- GraphQLite ^8 |
64 | 18 |
|
65 | | -final class HelloController |
66 | | -{ |
67 | | - #[Query] |
68 | | - public function hello(string $name = 'world'): string |
69 | | - { |
70 | | - return sprintf('Hello %s', $name); |
71 | | - } |
72 | | -} |
73 | | -``` |
| 19 | +## Quick start |
74 | 20 |
|
75 | | -## Features |
| 21 | +1. Install the bundle (Symfony Flex enables it and creates its configuration): |
76 | 22 |
|
77 | | -- Auto-discovers controllers and types from configured namespaces and registers GraphQLite services, query providers, |
78 | | - type mappers, and middleware through Symfony autoconfiguration |
79 | | -- Ships a `/graphql` route that converts Symfony requests to PSR-7 and keeps the Symfony request in the GraphQL context |
80 | | -- Passes the Symfony request as context to allow using them in queries/mutations |
81 | | -- Supports multipart uploads when `graphql-upload` is installed |
82 | | -- Integrates with Symfony Security for `#[Logged]`/`#[Right]` checks |
83 | | -- Expose `login`/`logout` mutations plus a `me` query (opt-out) |
84 | | -- Symfony Validator-based user input validation |
85 | | -- Lets you cap introspection, query depth, and query complexity via configuration |
86 | | -- Uses Symfony cache (APCu or PHP files) for schema caching |
87 | | -- Includes a `graphqlite:dump-schema` console command to export GraphQL SDL |
| 23 | + ```bash |
| 24 | + composer require thecodingmachine/graphqlite-bundle |
| 25 | + ``` |
88 | 26 |
|
89 | | -## Schema caching |
| 27 | +2. Tell GraphQLite where your GraphQL classes live: |
90 | 28 |
|
91 | | -Building the schema means reading every controller and type, so the result is cached. The |
92 | | -`schema.auto_reload` option decides whether that cache is checked against your files on each request. |
93 | | -It defaults to `'%kernel.debug%'`, so you see your changes while you work and deployed environments do |
94 | | -not pay for the check. |
| 29 | + ```yaml |
| 30 | + # config/packages/graphqlite.yaml |
| 31 | + graphqlite: |
| 32 | + namespace: |
| 33 | + controllers: App\GraphQL\Controller\ |
| 34 | + types: |
| 35 | + - App\Entity\ |
| 36 | + - App\GraphQL\Input\ |
| 37 | + ``` |
95 | 38 |
|
96 | | -Set it yourself when an environment does not fit that rule, such as a staging server that runs with |
97 | | -debug on but should still serve a cached schema: |
| 39 | +3. Write your first query and call `/graphql`: |
98 | 40 |
|
99 | | -```yaml |
100 | | -# config/packages/graphqlite.yaml |
101 | | -graphqlite: |
102 | | - schema: |
103 | | - auto_reload: false |
104 | | -``` |
| 41 | + ```php |
| 42 | + // src/GraphQL/Controller/HelloController.php |
| 43 | + namespace App\GraphQL\Controller; |
105 | 44 |
|
106 | | -With `auto_reload: true` an edited controller or type shows up on the next request. With |
107 | | -`auto_reload: false` the cached schema is trusted until the cache is cleared, so a deploy that changes |
108 | | -a controller, a type or one of their attributes has to clear it. `cache:clear` does that. Note that |
109 | | -with the APCu adapter the console and the web server each keep their own copy, so the PHP processes |
110 | | -serving requests need restarting as well. |
| 45 | + use TheCodingMachine\GraphQLite\Annotations\Query; |
111 | 46 |
|
112 | | -## GraphiQL (playground) |
| 47 | + class HelloController |
| 48 | + { |
| 49 | + #[Query] |
| 50 | + public function hello(string $name = 'world'): string |
| 51 | + { |
| 52 | + return sprintf('Hello %s', $name); |
| 53 | + } |
| 54 | + } |
| 55 | + ``` |
113 | 56 |
|
114 | | -The bundle wires Overblog’s GraphiQL bundle if it is installed. See https://github.com/overblog/GraphiQLBundle for |
115 | | -enabling the UI alongside the `/graphql` endpoint. |
| 57 | +Continue with the [getting started tutorial](docs/getting-started.rst). |
116 | 58 |
|
117 | 59 | ## Development |
118 | 60 |
|
119 | | -- Tests: `vendor/bin/phpunit` |
| 61 | +- Tests: `vendor/bin/simple-phpunit` |
120 | 62 | - Static analysis: `composer phpstan` |
| 63 | +- The code examples of `docs/` are tested: they must stay identical to the classes of `tests/DocsFixtures/` |
| 64 | + (see `tests/DocsExamplesTest.php`). |
0 commit comments