Skip to content

Commit 23c672c

Browse files
committed
Macro README
1 parent 39be3c6 commit 23c672c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ To work this around:
133133
- [JSON columns](#json-columns)
134134
- [Field deprecation](#field-deprecation)
135135
- [Default field resolver](#default-field-resolver)
136+
- [Macros](#macros)
136137
- [Guides](#guides)
137138
- [Upgrading from v1 to v2](#upgrading-from-v1-to-v2)
138139
- [Migrating from Folklore](#migrating-from-folklore)
@@ -1944,6 +1945,40 @@ The following is not a bullet-proof list but should serve as a guide. It's not a
19441945
- The trait `ShouldValidate` does not exist anymore; the provided features are baked into `Field`
19451946
- The first argument to the resolve method for queries/mutations is now `null` (previously its default was an empty array)
19461947

1948+
### Macros
1949+
1950+
If you would like to define some helpers that you can re-use in a variety of your
1951+
queries, mutations and types, you may use the macro method on the `GraphQL` facade.
1952+
1953+
For example, from a service provider's boot method:
1954+
1955+
```php
1956+
<?php
1957+
1958+
namespace App\Providers;
1959+
1960+
use GraphQL\Type\Definition\Type;
1961+
use Illuminate\Support\ServiceProvider;
1962+
use Rebing\GraphQL\Support\Facades\GraphQL;
1963+
1964+
class AppServiceProvider extends ServiceProvider
1965+
{
1966+
/**
1967+
* Bootstrap any application services.
1968+
*
1969+
* @return void
1970+
*/
1971+
public function boot()
1972+
{
1973+
GraphQL::macro('listOf', function (string $name) : Type {
1974+
return Type::listOf(GraphQL::type($name));
1975+
});
1976+
}
1977+
}
1978+
```
1979+
1980+
The `macro` function accepts a name as its first argument, and a `Closure` as its second.
1981+
19471982
## Performance considerations
19481983

19491984
### Lazy loading of types

0 commit comments

Comments
 (0)