Skip to content

Commit

Permalink
TASK: Added top level GraphQLTools helper functions based on apollo docs
Browse files Browse the repository at this point in the history
  • Loading branch information
johannessteu authored Nov 22, 2018
2 parents 01d8e41 + c4ef693 commit 0d9444d
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 38 deletions.
31 changes: 31 additions & 0 deletions src/GraphQLTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use GraphQL\Language\AST\DocumentNode;
use GraphQL\Type\Schema;
use GraphQLTools\Generate\AddResolveFunctionsToSchema;
use GraphQLTools\Generate\AddSchemaLevelResolveFunction;
use GraphQLTools\Generate\BuildSchemaFromTypeDefinitions;
use GraphQLTools\Stitching\DelegateToSchema;
use GraphQLTools\Stitching\MergeSchemas;
use GraphQLTools\Transforms\Transform;
use GraphQLTools\Transforms\TransformSchema;
Expand All @@ -21,6 +24,34 @@ public static function makeExecutableSchema(array $options) : Schema
return MakeExecutableSchema::invoke($options);
}

/**
* @param Schema|mixed[] $options
* @param mixed[]|null $legacyInputResolvers
* @param mixed[]|null $legacyInputValidationOptions
*/
public static function addResolveFunctionsToSchema(
$options,
$legacyInputResolvers = null,
?array $legacyInputValidationOptions = null
) : Schema {
return AddResolveFunctionsToSchema::invoke($options, $legacyInputResolvers, $legacyInputValidationOptions);
}

public static function addSchemaLevelResolveFunction(Schema $schema, callable $resolveFn) : void
{
AddSchemaLevelResolveFunction::invoke($schema, $resolveFn);
}

/**
* @param mixed[] $options
*
* @return mixed
*/
public static function delegateToSchema(array $options)
{
return DelegateToSchema::invoke($options);
}

/**
* @param mixed[] $options
*/
Expand Down
4 changes: 2 additions & 2 deletions src/SchemaVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ static function (EnumValueDefinition $value) use ($callMethod, $newEnum) {
$visitFields = static function ($type) use ($callMethod) : void {
$fields = $type->getFields();
static::updateEachKey($fields, static function (FieldDefinition $field) use ($callMethod, $type) {
$newField = $callMethod('visitFieldDefinition', $field, ['objectType' => $type]);
$newField = $callMethod('visitFieldDefinition', $field, ['objectType' => $type]);

if ($newField instanceof FieldDefinition) {
static::updateEachKey(
Expand Down Expand Up @@ -385,7 +385,7 @@ public static function healSchema(Schema $schema) : Schema
foreach ($type->getFields() as $field) {
Utils::forceSet($field, 'type', $healType($field->getType()));
}
// phpcs:ignore
// phpcs:ignore
} elseif ($type instanceof ScalarType) {
// nothing to do
} elseif ($type instanceof UnionType) {
Expand Down
3 changes: 1 addition & 2 deletions tests/DelegateToSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use GraphQL\GraphQL;
use GraphQLTools\GraphQLTools;
use GraphQLTools\Stitching\DelegateToSchema;
use PHPUnit\Framework\TestCase;

class DelegateToSchemaTest extends TestCase
Expand Down Expand Up @@ -51,7 +50,7 @@ protected static function proxyResolvers(string $spec) : array
'fragment' => '... on Booking { propertyId }',
'resolve' => static function ($booking, $args, $context, $info) use ($spec) {
$delegateFn = $spec === 'standalone' ? static function (...$args) {
return DelegateToSchema::invoke(...$args);
return GraphQLTools::delegateToSchema(...$args);
} : static function (...$args) use ($info) {
return $info->mergeInfo->delegateToSchema(...$args);
};
Expand Down
27 changes: 13 additions & 14 deletions tests/MockingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use GraphQL\GraphQL;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQLTools\Generate\AddResolveFunctionsToSchema;
use GraphQLTools\GraphQLTools;
use GraphQLTools\Mock;
use GraphQLTools\SimpleLogger;
Expand Down Expand Up @@ -255,7 +254,7 @@ public function testMockServerIsAbleToPreserveResolversOfAPrebuiltSchema() : voi
],
];

AddResolveFunctionsToSchema::invoke(
GraphQLTools::addResolveFunctionsToSchema(
$jsSchema,
$resolvers,
['requireResolversForResolveType' => false]
Expand Down Expand Up @@ -379,7 +378,7 @@ public function testDoesNotMaskResolveTypeFunctionsIfYouTellItNotTo() : void
],
];

AddResolveFunctionsToSchema::invoke(
GraphQLTools::addResolveFunctionsToSchema(
$jsSchema,
$resolvers,
['requireResolversForResolveType' => false]
Expand Down Expand Up @@ -427,7 +426,7 @@ public function testCanMockEnum() : void
public function testCanMockUnions() : void
{
$jsSchema = GraphQLTools::buildSchemaFromTypeDefinitions($this->shorthand);
AddResolveFunctionsToSchema::invoke($jsSchema, $this->resolveFunctions);
GraphQLTools::addResolveFunctionsToSchema($jsSchema, $this->resolveFunctions);
$mockMap = [
'Int' => static function () {
return 10;
Expand Down Expand Up @@ -491,7 +490,7 @@ public function testCanMockUnions() : void
public function testCanMockInterfacesByDefault() : void
{
$jsSchema = GraphQLTools::buildSchemaFromTypeDefinitions($this->shorthand);
AddResolveFunctionsToSchema::invoke($jsSchema, $this->resolveFunctions);
GraphQLTools::addResolveFunctionsToSchema($jsSchema, $this->resolveFunctions);
$mockMap = [
'Int' => static function () {
return 10;
Expand Down Expand Up @@ -562,7 +561,7 @@ public function testCanMockInterfacesByDefault() : void
public function testCanSupportExplicitInterfaceMock() : void
{
$jsSchema = GraphQLTools::buildSchemaFromTypeDefinitions($this->shorthand);
AddResolveFunctionsToSchema::invoke($jsSchema, $this->resolveFunctions);
GraphQLTools::addResolveFunctionsToSchema($jsSchema, $this->resolveFunctions);
$spy = 0;
$mockMap = [
'Bird' => static function ($root, $args) {
Expand Down Expand Up @@ -620,7 +619,7 @@ public function testCanSupportExplicitInterfaceMock() : void
public function testCanSupportExplicitUnionTypeMock() : void
{
$jsSchema = GraphQLTools::buildSchemaFromTypeDefinitions($this->shorthand);
AddResolveFunctionsToSchema::invoke($jsSchema, $this->resolveFunctions);
GraphQLTools::addResolveFunctionsToSchema($jsSchema, $this->resolveFunctions);
$spy = 0;
$mockMap = [

Expand Down Expand Up @@ -679,7 +678,7 @@ public function testCanSupportExplicitUnionTypeMock() : void
public function testThrowsAnErrorWhenTypenameIsNotReturnedWithinAnExplicitInterfaceMock() : void
{
$jsSchema = GraphQLTools::buildSchemaFromTypeDefinitions($this->shorthand);
AddResolveFunctionsToSchema::invoke($jsSchema, $this->resolveFunctions);
GraphQLTools::addResolveFunctionsToSchema($jsSchema, $this->resolveFunctions);
$mockMap = [

'Bird' => static function ($root, $args) {
Expand Down Expand Up @@ -752,7 +751,7 @@ public function testThrowsAnErrorInResolveIfMockTypeIsNotDefinedAndResolverFaile
],
];

AddResolveFunctionsToSchema::invoke(
GraphQLTools::addResolveFunctionsToSchema(
$jsSchema,
$resolvers,
['requireResolversForResolveType' => false]
Expand Down Expand Up @@ -798,7 +797,7 @@ public function testCanPreserveScalarResolvers() : void
],
];

AddResolveFunctionsToSchema::invoke(
GraphQLTools::addResolveFunctionsToSchema(
$jsSchema,
$resolvers,
['requireResolversForResolveType' => false]
Expand Down Expand Up @@ -1084,7 +1083,7 @@ public function testDoesNotMaskResolveFunctionsIfYouTellItNotTo() : void
],
];

AddResolveFunctionsToSchema::invoke(
GraphQLTools::addResolveFunctionsToSchema(
$jsSchema,
$resolvers,
['requireResolversForResolveType' => false]
Expand Down Expand Up @@ -1171,7 +1170,7 @@ public function testLetsYouMockAndResolveNonLeafTypesConcurrently() : void
],
];

AddResolveFunctionsToSchema::invoke(
GraphQLTools::addResolveFunctionsToSchema(
$jsSchema,
$resolvers,
['requireResolversForResolveType' => false]
Expand Down Expand Up @@ -1229,7 +1228,7 @@ public function testLetsYouMockAndResolveNonLeafTypesConcurrentlySupportPromises
],
];

AddResolveFunctionsToSchema::invoke(
GraphQLTools::addResolveFunctionsToSchema(
$jsSchema,
$resolvers,
['requireResolversForResolveType' => false]
Expand Down Expand Up @@ -1465,7 +1464,7 @@ public function testLetYouResolveNullWithMockingAndPreservingResolvers() : void
],
];

AddResolveFunctionsToSchema::invoke(
GraphQLTools::addResolveFunctionsToSchema(
$jsSchema,
$resolvers,
['requireResolversForResolveType' => false]
Expand Down
11 changes: 5 additions & 6 deletions tests/SchemaGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\ScalarType;
use GraphQLTools\Generate\AddSchemaLevelResolveFunction;
use GraphQLTools\Generate\AttachConnectorsToContext;
use GraphQLTools\Generate\ChainResolvers;
use GraphQLTools\Generate\ConcatenateTypeDefs;
Expand Down Expand Up @@ -2029,7 +2028,7 @@ public function testActuallyRuns() : void
return ['species' => 'ROOT'];
};

AddSchemaLevelResolveFunction::invoke($jsSchema, $rootResolver);
GraphQLTools::addSchemaLevelResolveFunction($jsSchema, $rootResolver);

$query = '{
species(name: "strix")
Expand All @@ -2053,7 +2052,7 @@ public function testCanWrapFieldsThatDoNotHaveAResolverDefined() : void
return ['stuff' => 'stuff'];
};

AddSchemaLevelResolveFunction::invoke($jsSchema, $rootResolver);
GraphQLTools::addSchemaLevelResolveFunction($jsSchema, $rootResolver);
$query = '{
stuff
}';
Expand Down Expand Up @@ -2104,7 +2103,7 @@ public function testRunsOnlyOncePerQuery() : void
return ['stuff' => 'EEE', 'species' => 'EEE'];
};

AddSchemaLevelResolveFunction::invoke($jsSchema, $rootResolver);
GraphQLTools::addSchemaLevelResolveFunction($jsSchema, $rootResolver);
$query = '{
species(name: "strix")
stuff
Expand Down Expand Up @@ -2139,7 +2138,7 @@ public function testCanAttachThingsToContext() : void
$ctx->usecontext = 'ABC';
};

AddSchemaLevelResolveFunction::invoke($jsSchema, $rootResolver);
GraphQLTools::addSchemaLevelResolveFunction($jsSchema, $rootResolver);
$query = '{
usecontext
}';
Expand Down Expand Up @@ -2304,7 +2303,7 @@ public function testDoesNotInterfereWithSchemaLevelResolveFunction() : void
];
};

AddSchemaLevelResolveFunction::invoke($jsSchema, $rootResolver);
GraphQLTools::addSchemaLevelResolveFunction($jsSchema, $rootResolver);
AttachConnectorsToContext::invoke($jsSchema, TestHelper::getTestConnectors());

$query = '
Expand Down
4 changes: 2 additions & 2 deletions tests/TransformsTest/FilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use GraphQLTools\GraphQLTools;
use GraphQLTools\Tests\TestingSchemas;
use GraphQLTools\Transforms\FilterTypes;
use GraphQLTools\Transforms\TransformSchema;
use PHPUnit\Framework\TestCase;
use function in_array;

Expand All @@ -31,7 +31,7 @@ static function ($type) use ($typeNames) {
),
];

$this->schema = TransformSchema::invoke(TestingSchemas::bookingSchema(), $transforms);
$this->schema = GraphQLTools::transformSchema(TestingSchemas::bookingSchema(), $transforms);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/TransformsTest/NamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use GraphQLTools\GraphQLTools;
use GraphQLTools\Tests\TestingSchemas;
use GraphQLTools\Transforms\RenameTypes;
use GraphQLTools\Transforms\TransformSchema;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -27,7 +27,7 @@ public function setUp() : void
}),
];

$this->schema = TransformSchema::invoke(TestingSchemas::propertySchema(), $transforms);
$this->schema = GraphQLTools::transformSchema(TestingSchemas::propertySchema(), $transforms);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/TransformsTest/RenameTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use GraphQLTools\GraphQLTools;
use GraphQLTools\Tests\TestingSchemas;
use GraphQLTools\Transforms\RenameTypes;
use GraphQLTools\Transforms\TransformSchema;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -38,7 +38,7 @@ static function ($name) {
),
];

$this->schema = TransformSchema::invoke(TestingSchemas::propertySchema(), $transforms);
$this->schema = GraphQLTools::transformSchema(TestingSchemas::propertySchema(), $transforms);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions tests/TransformsTest/ReplacesFieldWithFragmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use GraphQLTools\GraphQLTools;
use GraphQLTools\Stitching\DelegateToSchema;
use GraphQLTools\Transforms\ReplaceFieldWithFragment;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -70,7 +69,7 @@ public function setUp() : void
'Query' => [
'userById' => function ($parent, $args, $context, $info) {
$id = $args['id'];
return DelegateToSchema::invoke([
return GraphQLTools::delegateToSchema([
'schema' => $this->subSchema,
'operation' => 'query',
'fieldName' => 'userById',
Expand Down
7 changes: 3 additions & 4 deletions tests/TransformsTest/TreeOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use GraphQL\Language\AST\NameNode;
use GraphQL\Type\Schema;
use GraphQLTools\GraphQLTools;
use GraphQLTools\Stitching\DelegateToSchema;
use GraphQLTools\Transforms\ExtractField;
use GraphQLTools\Transforms\WrapQuery;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -135,7 +134,7 @@ public function setUp() : void
'Query' => [
'addressByUser' => function ($parent, $args, $context, $info) {
$id = $args['id'];
return DelegateToSchema::invoke([
return GraphQLTools::delegateToSchema([
'schema' => $this->subSchema,
'operation' => 'query',
'fieldName' => 'userById',
Expand Down Expand Up @@ -168,7 +167,7 @@ static function ($result) {
'Mutation' => [
'setUserAndAddress' => function ($parent, $args, $context, $info) {
$input = $args['input'];
$addressResult = DelegateToSchema::invoke([
$addressResult = GraphQLTools::delegateToSchema([
'schema' => $this->subSchema,
'operation' => 'mutation',
'fieldName' => 'setAddress',
Expand All @@ -191,7 +190,7 @@ static function ($result) {
],
]);

$userResult = DelegateToSchema::invoke([
$userResult = GraphQLTools::delegateToSchema([
'schema' => $this->subSchema,
'operation' => 'mutation',
'fieldName' => 'setUser',
Expand Down
3 changes: 1 addition & 2 deletions tests/TransformsTest/WrapQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Type\Schema;
use GraphQLTools\GraphQLTools;
use GraphQLTools\Stitching\DelegateToSchema;
use GraphQLTools\Transforms\WrapQuery;
use PHPUnit\Framework\TestCase;
use function array_map;
Expand Down Expand Up @@ -77,7 +76,7 @@ public function setUp() : void
'Query' => [
'addressByUser' => function ($parent, $args, $context, $info) {
$id = $args['id'];
return DelegateToSchema::invoke([
return GraphQLTools::delegateToSchema([
'schema' => $this->subSchema,
'operation' => 'query',
'fieldName' => 'userById',
Expand Down

0 comments on commit 0d9444d

Please sign in to comment.