Skip to content

Commit

Permalink
create typeLoader to lazy load fields
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissm79 committed Mar 17, 2018
1 parent 930a835 commit 940ad8e
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 39 deletions.
4 changes: 1 addition & 3 deletions src/Schema/Factories/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class FieldFactory
*/
public function handle(FieldValue $value)
{
$value->setType(function () use ($value) {
return NodeResolver::resolve($value->getField()->type);
});
$value->setType(NodeResolver::resolve($value->getField()->type));

$this->hasResolver($value)
? $this->useResolver($value)
Expand Down
14 changes: 5 additions & 9 deletions src/Schema/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\TypeExtensionDefinitionNode;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use Nuwave\Lighthouse\Schema\Factories\NodeFactory;
use Nuwave\Lighthouse\Schema\Resolvers\FieldTypeResolver;
use Nuwave\Lighthouse\Schema\Values\NodeValue;
use Nuwave\Lighthouse\Support\Traits\CanParseTypes;
use Nuwave\Lighthouse\Support\Traits\HandlesTypes;
Expand Down Expand Up @@ -42,7 +40,11 @@ public function build($schema)
return ! in_array($type->name, ['Query', 'Mutation']);
})->toArray();

return new Schema(compact('query', 'mutation', 'types'));
$typeLoader = function ($name) {
return $this->instance($name);
};

return new Schema(compact('query', 'mutation', 'types', 'typeLoader'));
}

/**
Expand All @@ -61,12 +63,6 @@ public function register($schema)
$this->setTypes($document);
$this->extendTypes($document);

while ($this->hasPackedTypes($this->types)) {
collect($this->types)->each(function ($type) {
$this->unpackType($type);
});
}

return collect($this->types);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Schema/Directives/Fields/BelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function itCanResolveBelongsToRelationship()
';

$type = schema()->register($schema)->first();
$resolver = array_get($type->config['fields'], 'user.resolve');
$resolver = array_get($type->config['fields'](), 'user.resolve');
$user = $resolver($this->task, []);

$this->assertInstanceOf(User::class, $user);
Expand All @@ -74,7 +74,7 @@ public function itCanResolveBelongsToWithCustomName()
';

$type = schema()->register($schema)->first();
$resolver = array_get($type->config['fields'], 'bar.resolve');
$resolver = array_get($type->config['fields'](), 'bar.resolve');
$user = $resolver($this->task, []);

$this->assertInstanceOf(User::class, $user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function itCanQueryHasManyRelationship()
';

$type = schema()->register($schema)->first();
$resolver = array_get($type->config['fields'], 'tasks.resolve');
$resolver = array_get($type->config['fields'](), 'tasks.resolve');
$tasks = $resolver($this->user, []);

$this->assertCount(3, $tasks);
Expand All @@ -83,23 +83,25 @@ public function itCanQueryHasManyPaginator()
$root = $types->first(function ($root) {
return 'User' === $root->name;
});
$paginator = $types->first(function ($type) {
$root->config['fields']();

$paginator = collect(schema()->types())->first(function ($type) {
return 'UserTaskPaginator' === $type->name;
});

$resolver = array_get($root->config['fields'], 'tasks.resolve');
$resolver = array_get($root->config['fields'](), 'tasks.resolve');
$tasks = $resolver($this->user, ['count' => 2]);

$this->assertInstanceOf(LengthAwarePaginator::class, $tasks);
$this->assertEquals(2, $tasks->count());
$this->assertEquals(3, $tasks->total());
$this->assertTrue($tasks->hasMorePages());

$resolver = array_get($paginator->config['fields'], 'data.resolve');
$resolver = array_get($paginator->config['fields'](), 'data.resolve');
$data = $resolver($tasks, []);
$this->assertCount(2, $data);

$resolver = array_get($paginator->config['fields'], 'paginatorInfo.resolve');
$resolver = array_get($paginator->config['fields'](), 'paginatorInfo.resolve');
$pageInfo = $resolver($tasks, []);
$this->assertTrue($pageInfo['hasMorePages']);
$this->assertEquals(1, $pageInfo['currentPage']);
Expand All @@ -125,23 +127,25 @@ public function itCanQueryHasManyRelayConnection()
$root = $types->first(function ($root) {
return 'User' === $root->name;
});
$connection = $types->first(function ($type) {
$root->config['fields']();

$connection = collect(schema()->types())->first(function ($type) {
return 'UserTaskConnection' === $type->name;
});

$resolver = array_get($root->config['fields'], 'tasks.resolve');
$resolver = array_get($root->config['fields'](), 'tasks.resolve');
$tasks = $resolver($this->user, ['first' => 2]);

$this->assertInstanceOf(LengthAwarePaginator::class, $tasks);
$this->assertEquals(2, $tasks->count());
$this->assertEquals(3, $tasks->total());
$this->assertTrue($tasks->hasMorePages());

$resolver = array_get($connection->config['fields'], 'edges.resolve');
$resolver = array_get($connection->config['fields'](), 'edges.resolve');
$edges = $resolver($tasks, []);
$this->assertCount(2, $edges);

$resolver = array_get($connection->config['fields'], 'pageInfo.resolve');
$resolver = array_get($connection->config['fields'](), 'pageInfo.resolve');
$pageInfo = $resolver($tasks, []);
$this->assertEquals($edges->first()['cursor'], $pageInfo['startCursor']);
$this->assertEquals($edges->last()['cursor'], $pageInfo['endCursor']);
Expand All @@ -165,5 +169,6 @@ public function itThrowsErrorWithUnknownTypeArg()

$this->expectException(DirectiveException::class);
$type = schema()->register($schema)->first();
$type->config['fields']();
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Schema/Directives/Fields/AuthDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function itCanResolveAuthenticatedUser()
';

$query = schema()->register($schema)->firstWhere('name', 'Query');
$resolver = array_get($query->config['fields'], 'user.resolve');
$resolver = array_get($query->config['fields'](), 'user.resolve');
$this->assertEquals('bar', data_get($resolver(), 'foo'));
}
}
8 changes: 4 additions & 4 deletions tests/Unit/Schema/Directives/Fields/FieldDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function itCanResolveFieldWithAssignedClass()
';

$type = schema()->register($schema)->first();
$fields = $type->config['fields'];
$fields = $type->config['fields']();
$resolve = array_get($fields, 'bar.resolve');

$this->assertEquals('foo.bar', $resolve(null, []));
Expand All @@ -37,7 +37,7 @@ public function itCanResolveFieldWithMergedArgs()
';

$type = schema()->register($schema)->first();
$fields = $type->config['fields'];
$fields = $type->config['fields']();
$resolve = array_get($fields, 'bar.resolve');

$this->assertEquals('foo.baz', $resolve(null, []));
Expand All @@ -56,7 +56,7 @@ public function itThrowsAnErrorIfNoClassIsDefined()

$this->expectException(DirectiveException::class);
$type = schema()->register($schema)->first();
$type->config['fields'];
$type->config['fields']();
}

/**
Expand All @@ -72,6 +72,6 @@ public function itThrowsAnErrorIfNoMethodIsDefined()

$this->expectException(DirectiveException::class);
$type = schema()->register($schema)->first();
$type->config['fields'];
$type->config['fields']();
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Schema/Directives/Fields/MethodDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function foobar()
';

$type = schema()->register($schema)->first();
$fields = $type->config['fields'];
$fields = $type->config['fields']();
$resolver = array_get($fields, 'bar.resolve');
$this->assertEquals('baz', $resolver($root, []));
}
Expand All @@ -49,7 +49,7 @@ public function bar(array $args)
';

$type = schema()->register($schema)->first();
$fields = $type->config['fields'];
$fields = $type->config['fields']();
$resolver = array_get($fields, 'bar.resolve');
$this->assertEquals('foo', $resolver($root, ['baz' => 'foo']));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function itCanRegisterMiddleware()
}
');

collect(schema()->types())->each(function ($type) {
$type->config['fields']();
});

$query = 'query FooQuery { foo }';
$middleware = graphql()->middleware()->forRequest($query);
$this->assertCount(2, $middleware);
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Schema/Directives/Fields/RenameDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function itCanRenameAField()
';

$type = schema()->register($schema)->first();
$fields = $type->config['fields'];
$fields = $type->config['fields']();
$resolver = array_get($fields, 'fooBar.resolve');
$this->assertEquals('bar', $resolver(['foo_bar' => 'bar', 'fooBar' => 'baz'], []));
}
Expand All @@ -37,5 +37,6 @@ public function itThrowsAnExceptionIfNoAttributeDefined()

$this->expectException(DirectiveException::class);
$type = schema()->register($schema)->first();
$type->config['fields']();
}
}
17 changes: 9 additions & 8 deletions tests/Unit/Schema/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function itCanResolveObjectTypes()

$config = $types->firstWhere('name', 'Foo')->config;
$this->assertEquals('Foo', data_get($config, 'name'));
$this->assertEquals('bar attribute of Foo', array_get($config, 'fields.bar.description'));
$this->assertEquals('bar attribute of Foo', array_get($config['fields'](), 'bar.description'));
}

/**
Expand All @@ -118,9 +118,10 @@ public function itCanResolveInputObjectTypes()
$this->assertInstanceOf(InputType::class, $types->firstWhere('name', 'CreateFoo'));

$config = $types->firstWhere('name', 'CreateFoo')->config;
$fields = $config['fields']();
$this->assertEquals('CreateFoo', data_get($config, 'name'));
$this->assertArrayHasKey('foo', data_get($config, 'fields'));
$this->assertArrayHasKey('bar', data_get($config, 'fields'));
$this->assertArrayHasKey('foo', $fields);
$this->assertArrayHasKey('bar', $fields);
}

/**
Expand All @@ -135,7 +136,7 @@ public function itCanResolveMutations()
';

$type = schema()->register($schema)->firstWhere('name', 'Mutation');
$mutation = $type->config['fields']['foo'];
$mutation = $type->config['fields']()['foo'];

$this->assertArrayHasKey('args', $mutation);
$this->assertArrayHasKey('type', $mutation);
Expand All @@ -156,7 +157,7 @@ public function itCanResolveQueries()
';

$type = schema()->register($schema)->firstWhere('name', 'Query');
$query = $type->config['fields']['foo'];
$query = $type->config['fields']()['foo'];

$this->assertArrayHasKey('args', $query);
$this->assertArrayHasKey('type', $query);
Expand All @@ -180,7 +181,7 @@ public function itCanExtendObjectTypes()
';

$type = schema()->register($schema)->first();
$fields = $type->config['fields'];
$fields = $type->config['fields']();
$this->assertArrayHasKey('baz', $fields);
}

Expand All @@ -199,7 +200,7 @@ public function itCanExtendQuery()
';

$type = schema()->register($schema)->firstWhere('name', 'Query');
$fields = $type->config['fields'];
$fields = $type->config['fields']();
$this->assertArrayHasKey('bar', $fields);
}

Expand All @@ -218,7 +219,7 @@ public function itCanExtendMutation()
';

$type = schema()->register($schema)->firstWhere('name', 'Mutation');
$fields = $type->config['fields'];
$fields = $type->config['fields']();
$this->assertArrayHasKey('bar', $fields);
}

Expand Down

0 comments on commit 940ad8e

Please sign in to comment.