Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ Each error goes to stderr as `file:line message`, with the file relative to the

| Attribute | Goes on | Arguments |
| ---------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| `#[BidirektSchema]` | a class, an enum with string values, or a handler ([without a class](#schemas-without-a-class)) | `name`, `fields` |
| `#[BidirektSchema]` | a class, an enum with string values, or a handler ([without a class](#schemas-without-a-class)) | `name`, `fields`, `graphqlType` |
| `#[BidirektField]` | a property or a promoted constructor parameter, or `new BidirektField(...)` in `fields:` | `name`, `type`, `items`, `values`, `optional`, `nullable`, `description`, `union` |
| `#[BidirektRestProvider]` | a method, a function, a closure or an arrow function | `method`, `path`, `responses`, `request` |
| `#[BidirektRestConsumer]` | a method, a function, a closure or an arrow function | `provider`, `method`, `path`, `responses`, `request` |
Expand All @@ -849,7 +849,7 @@ An argument is read from the code, never run: a literal, an array, `X::class`, a

A promoted constructor parameter is a field like a property. An annotation on a parameter that is not promoted, or on a `static` property, is not read, and `generate` warns.

In a GraphQL contract, `#[BidirektGraphqlField]` takes the place of `#[BidirektField]` on a field that needs GraphQL's own options, and `@bidirekt graphql field` that of `@bidirekt field`. They take the options above, plus `scalar: 'DateTime'`, a custom scalar that replaces the field's type at the bottom of its lists and is written as `type: scalar, name: DateTime`, and `arguments: ReviewsArgs::class`, the schema of the arguments the call passes to the field, as `first` in `reviews(first: 10)`. In a tag, `scalar=DateTime` and `arguments=ReviewsArgs`. A field takes one of the two attributes, never both.
In a GraphQL contract, `#[BidirektGraphqlField]` takes the place of `#[BidirektField]` on a field that needs GraphQL's own options, and `@bidirekt graphql field` that of `@bidirekt field`. They take the options above, plus `scalar: 'DateTime'`, a custom scalar or a built-in one (`ID`, `String`, `Int`, `Float`, `Boolean`) that replaces the field's type at the bottom of its lists and is written as `type: scalar, name: DateTime`, and `arguments: ReviewsArgs::class`, the schema of the arguments the call passes to the field, as `first` in `reviews(first: 10)`. In a tag, `scalar=DateTime` and `arguments=ReviewsArgs`. A field takes one of the two attributes, never both.

### Handlers

Expand Down Expand Up @@ -882,15 +882,25 @@ Without a request (no `request` argument, no `@bidirekt request`), the resource
| an enum | `['enum' => ['AVAILABLE', 'SOLD'], 'name' => 'Status']` | `enum:AVAILABLE\|SOLD as Status` | `Status` |
| a union | `['union' => ['Pet' => Pet::class, 'Owner' => Owner::class], 'name' => 'SearchResult']` | `union={Pet: Pet, Owner: Owner} as SearchResult` | `SearchResult` |

A primitive, a scalar, an enum or a union also takes the place of the schema in a nullable or a list (`['nullable' => 'integer']`, `'string[]'`, `scalar:DateTime?[]`). Each key of a union is the name of one of its types in the provider's GraphQL schema, and its value the schema the call reads from that type. Each form but a schema returned as it is becomes a schema of the contract, with the name above, and `name` in the attribute or `as <name>` in the tag gives it another one. They name only a schema `generate` creates: `@bidirekt returns Pet as MyPet` stops `generate`; to call it `MyPet`, name the schema itself. An enum or a union in a nullable or a list is a schema of its own, under the name it declares, and the nullable or the list references it under the name above: `['nullable' => ['enum' => ['AVAILABLE', 'SOLD'], 'name' => 'Status']]` and `enum:AVAILABLE|SOLD? as Status` both write `Status` and `StatusOrNull`. A scalar in a nullable or a list takes no name of its own: the name goes on the nullable or the list. GraphQL's own names of the built-in scalars, as `Int` or `Boolean`, are not schemas: write `integer` or `boolean`.
A primitive, a scalar, an enum or a union also takes the place of the schema in a nullable or a list (`['nullable' => 'integer']`, `'string[]'`, `scalar:DateTime?[]`). Each key of a union is the name of one of its types in the provider's GraphQL schema, and its value the schema the call reads from that type. Each form but a schema returned as it is becomes a schema of the contract, with the name above, and `name` in the attribute or `as <name>` in the tag gives it another one. They name only a schema `generate` creates: `@bidirekt returns Pet as MyPet` stops `generate`; to call it `MyPet`, name the schema itself. An enum or a union in a nullable or a list is a schema of its own, under the name it declares, and the nullable or the list references it under the name above: `['nullable' => ['enum' => ['AVAILABLE', 'SOLD'], 'name' => 'Status']]` and `enum:AVAILABLE|SOLD? as Status` both write `Status` and `StatusOrNull`. A scalar in a nullable or a list takes no name of its own: the name goes on the nullable or the list. GraphQL's own names of the built-in scalars, as `Int` or `Boolean`, are not schemas: write `integer` or `boolean`, which accept any GraphQL type of that shape, or `['scalar' => 'ID']` and `scalar:ID` to send that exact GraphQL type, written as `type: scalar, name: ID`.

`graphqlType` on `#[BidirektSchema]`, or `graphqlType=PetInput` on `@bidirekt schema`, declares the GraphQL type a class or an enum is sent or read as, written as `name: PetInput` on its schema: the check then breaks when the provider's type has another name. Without it, a type the provider renames with the same shape doesn't break. The broker takes it on an object or an enum that only GraphQL reaches, and never on the schema of `arguments` itself, which is no GraphQL type: put it on the input objects inside.

The same field called by several handlers is written once. A handler that declares other `arguments` or `returns` adds them: one schema is written as its name, several as a list in alphabetical order, with `~` first for a handler that sends no arguments, and the broker checks each one as if each handler were alone:

```yaml
pet:
arguments: [~, PetArgs]
returns: [PetWithName, PetWithStatus]
```

### Tags

A tag is `@bidirekt <keyword>` at the start of a line of a `/** */` docblock, with its arguments separated by spaces:

| Tag | Goes |
| --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| `@bidirekt schema` or `@bidirekt schema name=PetResponse` | above a class or an enum with string values |
| `@bidirekt schema [name=PetResponse] [graphqlType=Pet]` | above a class or an enum with string values |
| `@bidirekt field [key=value ...]` | above a property, or a promoted constructor parameter |
| `@bidirekt schema <Name>` and `@bidirekt field <name> type=...` | in a handler's docblock ([without a class](#schemas-without-a-class)) |
| `@bidirekt rest provides <METHOD> <path>` | above a method, a function, a closure, or a route call like `$app->get(...)` or `Route::get(...)` |
Expand Down
1 change: 1 addition & 0 deletions src/BidirektSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ final class BidirektSchema
public function __construct(
public ?string $name = null,
public array $fields = [],
public ?string $graphqlType = null,
) {}
}
53 changes: 29 additions & 24 deletions src/Model/Protocols/Graphql/BlocksToMerged.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function sideBlocksToMerged(array $sideBlocks): MergedBlock
foreach ($blocks as $own) {
array_push($operations, ...$own->operations);
}
$block->operations = $merger->operationsToMerged($operations, $provider);
$block->operations = self::operationsToMerged($operations);
}

return new MergedBlock($block, array_values($merger->errors));
Expand Down Expand Up @@ -75,48 +75,53 @@ private function graphsToMerged(array $graphs): ?Graph
}

/**
* Joins the operations of every handler that calls the same field: each distinct schema of its `arguments` and
* its `returns` once, in byte order, with a handler that sends no arguments first.
*
* @param list<Operation> $operations
* @return list<Operation>
*/
private function operationsToMerged(array $operations, string $provider): array
private static function operationsToMerged(array $operations): array
{
$groups = [];
foreach ($operations as $operation) {
$groups[serialize([$operation->kind, $operation->name])][] = $operation;
}
$merged = [];
foreach ($groups as $group) {
$first = array_shift($group);
$described = sprintf('%s %s consumed from "%s"', $first->kind, $first->name, $provider);
foreach ($group as $other) {
$this->usesToConflict('arguments', $first->arguments, $other->arguments, $first, $other, $described);
$this->usesToConflict('returns', $first->returns, $other->returns, $first, $other, $described);
$arguments = [];
$returns = [];
foreach ($group as $operation) {
array_push($arguments, ...$operation->arguments);
array_push($returns, ...$operation->returns);
}
$merged[] = $first;
$withoutArguments = in_array(null, $arguments, true) ? [null] : [];
$first = $group[0];
$merged[] = new Operation(
$first->kind,
$first->name,
[...$withoutArguments, ...self::usesToDistinct(array_filter($arguments))],
self::usesToDistinct($returns),
$first->location,
);
}

return $merged;
}

private function usesToConflict(string $use, ?SchemaUse $firstUse, ?SchemaUse $otherUse, Operation $first, Operation $other, string $described): void
/**
* @param array<SchemaUse> $uses
* @return list<SchemaUse>
*/
private static function usesToDistinct(array $uses): array
{
if ($firstUse?->schema === $otherUse?->schema) {
return;
$bySchema = [];
foreach ($uses as $use) {
$bySchema[$use->schema] ??= $use;
}
if ($firstUse === null || $otherUse === null) {
$this->addConflict(
sprintf('%s of %s is declared in one handler and not in another: declare the same %s in every handler', $use, $described, $use),
$firstUse->location ?? $first->location,
$otherUse->location ?? $other->location,
);
ksort($bySchema, SORT_STRING);

return;
}
$this->addConflict(
sprintf('%s of %s is declared as %s and %s: declare the same %s in every handler', $use, $described, $firstUse->schema, $otherUse->schema, $use),
$firstUse->location,
$otherUse->location,
);
return array_values($bySchema);
}

private function addConflict(string $message, SourceLocation $first, SourceLocation $other): void
Expand Down
13 changes: 12 additions & 1 deletion src/Model/Protocols/Graphql/GraphqlExtra.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use Bidirekt\PhpSdk\Model\SchemaNode;

/**
* GraphQL's own options of a field: a custom scalar in place of its type, and the arguments the call passes to it.
* GraphQL's own options of a field: a custom scalar in place of its type, and the arguments the call passes to it;
* and of a schema, the GraphQL type it is sent as.
*/
final class GraphqlExtra implements NodeExtra
{
Expand All @@ -21,8 +22,18 @@ final class GraphqlExtra implements NodeExtra
public function __construct(
public ?string $scalar = null,
public ?string $arguments = null,
public ?string $graphqlType = null,
) {}

public static function nodeToTyped(SchemaNode $node, ?string $graphqlType): SchemaNode
{
if ($graphqlType !== null) {
$node->extra = new self(graphqlType: $graphqlType);
}

return $node;
}

public static function scalarToNode(string $scalar): SchemaNode
{
$node = new PrimitiveNode('string');
Expand Down
9 changes: 6 additions & 3 deletions src/Model/Protocols/Graphql/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
use Bidirekt\PhpSdk\Model\SourceLocation;

/**
* A field of the query or the mutation root that the service calls.
* A field of the query or the mutation root that the service calls, from one handler or joined from every handler
* that calls it.
*/
final class Operation
{
/**
* @param 'query'|'mutation' $kind
* @param list<SchemaUse|null> $arguments null for a handler that sends no arguments
* @param list<SchemaUse> $returns
*/
public function __construct(
public string $kind,
public string $name,
public ?SchemaUse $arguments,
public SchemaUse $returns,
public array $arguments,
public array $returns,
public SourceLocation $location,
) {}
}
4 changes: 2 additions & 2 deletions src/Protocols/GraphqlProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function references(ProtocolBlock $block): array
}
$references = [];
foreach ($block->operations as $operation) {
foreach ([$operation->arguments, $operation->returns] as $use) {
foreach ([...$operation->arguments, ...$operation->returns] as $use) {
if ($use !== null) {
$references[] = new Reference($use->schema, $use->location, self::NAME);
}
Expand Down Expand Up @@ -69,6 +69,6 @@ public function missingSchemaToMessage(string $schema): ?string
return null;
}

return sprintf('%s is GraphQL\'s name: write %s', $schema, $primitive);
return sprintf('%s is GraphQL\'s name: write %s, or [\'scalar\' => \'%s\'] (scalar:%s in a tag) to declare that GraphQL type', $schema, $primitive, $schema, $schema);
}
}
Loading