Skip to content

Commit ef79ce1

Browse files
authored
Support per-schema scalar overrides
1 parent fd55782 commit ef79ce1

File tree

19 files changed

+723
-51
lines changed

19 files changed

+723
-51
lines changed

.ai/AGENTS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ make bench # Run PHPBench benchmarks
2020
make docs # Generate class reference docs
2121
```
2222

23+
## Public API
24+
25+
Elements marked with `@api` in PHPDoc are part of the stable public API.
26+
27+
Constants listed in the [class-reference docs](https://webonyx.github.io/graphql-php/class-reference/) (generated via `generate-class-reference.php` with `'constants' => true`) are also stable public API, even without an `@api` tag.
28+
2329
## Code and Testing Expectations
2430

2531
- Preserve backward compatibility for public APIs unless explicitly requested.

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
## v15.31.0
13+
14+
### Added
15+
16+
- Support per-schema scalar overrides via `types` config or `typeLoader`, without global side effects https://github.com/webonyx/graphql-php/pull/1869
17+
- Add `Type::builtInScalars()` and `Type::BUILT_IN_SCALAR_NAMES` aligning with GraphQL spec terminology https://github.com/webonyx/graphql-php/pull/1869
18+
- Add `Directive::builtInDirectives()` and `Directive::isBuiltInDirective()` aligning with GraphQL spec terminology https://github.com/webonyx/graphql-php/pull/1869
19+
20+
### Deprecated
21+
22+
- Deprecate `Type::overrideStandardTypes()` in favor of per-schema scalar overrides https://github.com/webonyx/graphql-php/pull/1869
23+
- Deprecate `Type::getStandardTypes()` in favor of `Type::builtInScalars()` https://github.com/webonyx/graphql-php/pull/1869
24+
- Deprecate `Type::STANDARD_TYPE_NAMES` in favor of `Type::BUILT_IN_SCALAR_NAMES` https://github.com/webonyx/graphql-php/pull/1869
25+
- Deprecate `Directive::getInternalDirectives()` in favor of `Directive::builtInDirectives()` https://github.com/webonyx/graphql-php/pull/1869
26+
- Deprecate `Directive::isSpecifiedDirective()` in favor of `Directive::isBuiltInDirective()` https://github.com/webonyx/graphql-php/pull/1869
27+
1228
## v15.30.2
1329

1430
### Fixed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ with a specific README file per example.
3131
This project follows [Semantic Versioning 2.0.0](https://semver.org/spec/v2.0.0.html).
3232

3333
Elements that belong to the public API of this package are marked with the `@api` PHPDoc tag.
34-
Those elements are thus guaranteed to be stable within major versions. All other elements are
35-
not part of this backwards compatibility guarantee and may change between minor or patch versions.
34+
Constants included in the [class-reference docs](https://webonyx.github.io/graphql-php/class-reference) are also part of the public API.
35+
Those elements are thus guaranteed to be stable within major versions.
36+
All other elements are not part of this backwards compatibility guarantee and may change between minor or patch versions.
3637

3738
The most recent version is actively developed on [`master`](https://github.com/webonyx/graphql-php/tree/master).
3839
Older versions are generally no longer supported, although exceptions may be made for [sponsors](#sponsors).

benchmarks/BuildSchemaBench.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
*
1212
* @Warmup(2)
1313
*
14+
* @Sleep(500000)
15+
*
1416
* @Revs(10)
1517
*
16-
* @Iterations(2)
18+
* @Iterations(5)
1719
*/
1820
class BuildSchemaBench
1921
{

benchmarks/HugeSchemaBench.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
*
1717
* @Warmup(2)
1818
*
19+
* @Sleep(500000)
20+
*
1921
* @Revs(10)
2022
*
21-
* @Iterations(3)
23+
* @Iterations(5)
2224
*/
2325
class HugeSchemaBench
2426
{

benchmarks/ScalarOverrideBench.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace GraphQL\Benchmarks;
4+
5+
use GraphQL\GraphQL;
6+
use GraphQL\Type\Definition\CustomScalarType;
7+
use GraphQL\Type\Definition\ObjectType;
8+
use GraphQL\Type\Definition\Type;
9+
use GraphQL\Type\Schema;
10+
11+
/**
12+
* @BeforeMethods({"setUp"})
13+
*
14+
* @OutputTimeUnit("milliseconds", precision=3)
15+
*
16+
* @Warmup(2)
17+
*
18+
* @Revs(100)
19+
*
20+
* @Iterations(5)
21+
*/
22+
class ScalarOverrideBench
23+
{
24+
private Schema $schemaBaseline;
25+
26+
private Schema $schemaTypeLoader;
27+
28+
private Schema $schemaTypes;
29+
30+
public function setUp(): void
31+
{
32+
$uppercaseString = new CustomScalarType([
33+
'name' => Type::STRING,
34+
'serialize' => static fn ($value): string => strtoupper((string) $value),
35+
]);
36+
37+
$queryTypeBaseline = new ObjectType([
38+
'name' => 'Query',
39+
'fields' => [
40+
'greeting' => [
41+
'type' => Type::string(),
42+
'resolve' => static fn (): string => 'hello world',
43+
],
44+
],
45+
]);
46+
$this->schemaBaseline = new Schema([
47+
'query' => $queryTypeBaseline,
48+
]);
49+
50+
$queryTypeLoader = new ObjectType([
51+
'name' => 'Query',
52+
'fields' => [
53+
'greeting' => [
54+
'type' => Type::string(),
55+
'resolve' => static fn (): string => 'hello world',
56+
],
57+
],
58+
]);
59+
$typesForLoader = ['Query' => $queryTypeLoader, 'String' => $uppercaseString];
60+
$this->schemaTypeLoader = new Schema([
61+
'query' => $queryTypeLoader,
62+
'typeLoader' => static fn (string $name): ?Type => $typesForLoader[$name] ?? null,
63+
]);
64+
65+
$queryTypeTypes = new ObjectType([
66+
'name' => 'Query',
67+
'fields' => [
68+
'greeting' => [
69+
'type' => Type::string(),
70+
'resolve' => static fn (): string => 'hello world',
71+
],
72+
],
73+
]);
74+
$this->schemaTypes = new Schema([
75+
'query' => $queryTypeTypes,
76+
'types' => [$uppercaseString],
77+
]);
78+
}
79+
80+
public function benchGetTypeWithoutOverride(): void
81+
{
82+
$this->schemaBaseline->getType('String');
83+
}
84+
85+
public function benchGetTypeWithTypeLoaderOverride(): void
86+
{
87+
$this->schemaTypeLoader->getType('String');
88+
}
89+
90+
public function benchGetTypeWithTypesOverride(): void
91+
{
92+
$this->schemaTypes->getType('String');
93+
}
94+
95+
public function benchExecuteWithoutOverride(): void
96+
{
97+
GraphQL::executeQuery($this->schemaBaseline, '{ greeting }');
98+
}
99+
100+
public function benchExecuteWithTypeLoaderOverride(): void
101+
{
102+
GraphQL::executeQuery($this->schemaTypeLoader, '{ greeting }');
103+
}
104+
105+
public function benchExecuteWithTypesOverride(): void
106+
{
107+
GraphQL::executeQuery($this->schemaTypes, '{ greeting }');
108+
}
109+
}

docs/class-reference.md

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ static function promiseToExecute(
108108
/**
109109
* Returns directives defined in GraphQL spec.
110110
*
111+
* @deprecated use {@see Directive::builtInDirectives()}
112+
*
111113
* @throws InvariantViolation
112114
*
113115
* @return array<string, Directive>
@@ -119,7 +121,9 @@ static function getStandardDirectives(): array
119121

120122
```php
121123
/**
122-
* Returns types defined in GraphQL spec.
124+
* Returns built-in scalar types defined in GraphQL spec.
125+
*
126+
* @deprecated use {@see Type::builtInScalars()}
123127
*
124128
* @throws InvariantViolation
125129
*
@@ -136,6 +140,8 @@ static function getStandardTypes(): array
136140
*
137141
* Standard types not listed here remain untouched.
138142
*
143+
* @deprecated prefer per-schema scalar overrides via {@see \GraphQL\Type\SchemaConfig::$types} or {@see \GraphQL\Type\SchemaConfig::$typeLoader}
144+
*
139145
* @param array<string, ScalarType> $types
140146
*
141147
* @api
@@ -180,13 +186,52 @@ static function setDefaultArgsMapper(callable $fn): void
180186

181187
## GraphQL\Type\Definition\Type
182188

183-
Registry of standard GraphQL types and base class for all other types.
189+
Registry of built-in GraphQL types and base class for all other types.
190+
191+
### GraphQL\Type\Definition\Type Constants
192+
193+
```php
194+
const INT = 'Int';
195+
const FLOAT = 'Float';
196+
const STRING = 'String';
197+
const BOOLEAN = 'Boolean';
198+
const ID = 'ID';
199+
const BUILT_IN_SCALAR_NAMES = [
200+
'Int',
201+
'Float',
202+
'String',
203+
'Boolean',
204+
'ID',
205+
];
206+
const STANDARD_TYPE_NAMES = [
207+
'Int',
208+
'Float',
209+
'String',
210+
'Boolean',
211+
'ID',
212+
];
213+
const BUILT_IN_TYPE_NAMES = [
214+
'Int',
215+
'Float',
216+
'String',
217+
'Boolean',
218+
'ID',
219+
'__Schema',
220+
'__Type',
221+
'__Directive',
222+
'__Field',
223+
'__InputValue',
224+
'__EnumValue',
225+
'__TypeKind',
226+
'__DirectiveLocation',
227+
];
228+
```
184229

185230
### GraphQL\Type\Definition\Type Methods
186231

187232
```php
188233
/**
189-
* Returns the registered or default standard Int type.
234+
* Returns the built-in Int scalar type.
190235
*
191236
* @api
192237
*/
@@ -195,7 +240,7 @@ static function int(): GraphQL\Type\Definition\ScalarType
195240

196241
```php
197242
/**
198-
* Returns the registered or default standard Float type.
243+
* Returns the built-in Float scalar type.
199244
*
200245
* @api
201246
*/
@@ -204,7 +249,7 @@ static function float(): GraphQL\Type\Definition\ScalarType
204249

205250
```php
206251
/**
207-
* Returns the registered or default standard String type.
252+
* Returns the built-in String scalar type.
208253
*
209254
* @api
210255
*/
@@ -213,7 +258,7 @@ static function string(): GraphQL\Type\Definition\ScalarType
213258

214259
```php
215260
/**
216-
* Returns the registered or default standard Boolean type.
261+
* Returns the built-in Boolean scalar type.
217262
*
218263
* @api
219264
*/
@@ -222,7 +267,7 @@ static function boolean(): GraphQL\Type\Definition\ScalarType
222267

223268
```php
224269
/**
225-
* Returns the registered or default standard ID type.
270+
* Returns the built-in ID scalar type.
226271
*
227272
* @api
228273
*/
@@ -255,6 +300,28 @@ static function listOf($type): GraphQL\Type\Definition\ListOfType
255300
static function nonNull($type): GraphQL\Type\Definition\NonNull
256301
```
257302

303+
```php
304+
/**
305+
* Returns all built-in types: built-in scalars and introspection types.
306+
*
307+
* @api
308+
*
309+
* @return array<string, Type&NamedType>
310+
*/
311+
static function builtInTypes(): array
312+
```
313+
314+
```php
315+
/**
316+
* Returns all built-in scalar types.
317+
*
318+
* @api
319+
*
320+
* @return array<string, ScalarType>
321+
*/
322+
static function builtInScalars(): array
323+
```
324+
258325
```php
259326
/**
260327
* Determines if the given type is an input type.

0 commit comments

Comments
 (0)