在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):youshido-php/GraphQLBundle开源软件地址(OpenSource Url):https://github.com/youshido-php/GraphQLBundle开源编程语言(OpenSource Language):PHP 50.9%开源软件介绍(OpenSource Introduction):Symfony GraphQl BundlePHP GraphQL Server implementationThis is a bundle based on the pureThis bundle provides you with:
There are simple demo application to demonstrate how we build our API, see GraphQLDemoApp. Table of ContentsInstallationWe assume you have
Once you have your composer up and running – you're ready to install the GraphQL Bundle. composer require youshido/graphql-bundle Then enable bundle in your new Youshido\GraphQLBundle\GraphQLBundle(), Add the routing reference to the graphql:
resource: "@GraphQLBundle/Controller/" or graphql:
resource: "@GraphQLBundle/Resources/config/route.xml" If you don't have a web server configured you can use a bundled version, simply run Let's check if you've done everything right so far – try to access url {"errors":[{"message":"Schema class does not exist"}]} That's because there was no GraphQL Schema specified for the processor yet. You need to create a GraphQL Schema class and set it inside your
The fastest way to create a Schema class is to use a generator shipped with this bundle: php bin/console graphql:configure AppBundle Here AppBundle is a name of the bundle where the class will be generated in. After you've added parameters to the config file, try to access the following link in the browser –
Successful response from a test Schema will be displayed: {"data":{"hello":"world!"}} That means you have GraphQL Bundle for the Symfony Framework configured and now can architect your GraphQL Schema: Next step would be to link assets for GraphiQL Explorer by executing: php bin/console assets:install --symlink Now you can access it at Symfony featuresClass AbstractContainerAwareField:AbstractContainerAwareField class used for auto passing container to field, add ability to use container in resolve function: class RootDirField extends AbstractContainerAwareField
{
/**
* @inheritdoc
*/
public function getType()
{
return new StringType();
}
/**
* @inheritdoc
*/
public function resolve($value, array $args, ResolveInfo $info)
{
return $this->container->getParameter('kernel.root_dir');
}
/**
* @inheritdoc
*/
public function getName()
{
return 'rootDir';
} Service method as callable:Ability to pass service method as resolve callable: $config->addField(new Field([
'name' => 'cacheDir',
'type' => new StringType(),
'resolve' => ['@resolve_service', 'getCacheDir']
])) Events:You can use the Symfony Event Dispatcher to get control over specific events which happen when resolving graphql queries. namespace ...\...\..;
use Youshido\GraphQL\Event\ResolveEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MyGraphQLResolveEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'graphql.pre_resolve' => 'onPreResolve',
'graphql.post_resolve' => 'onPostResolve'
];
}
public function onPreResolve(ResolveEvent $event)
{
//$event->getFields / $event->getAstFields()..
}
public function onPostResolve(ResolveEvent $event)
{
//$event->getFields / $event->getAstFields()..
}
} ConfigurationNow configure you subscriber so events will be caught. This can be done in Symfony by either XML, Yaml or PHP. <service id="my_own_bundle.event_subscriber.my_graphql_resolve_event_subscriber" class="...\...\...\MyGraphQLResolveEventSubscriber">
<tag name="graphql.event_subscriber" />
</service> Security:Bundle provides two ways to guard your application: using black/white operation list or using security voter. Black/white listUsed to guard some root operations. To enable it you need to write following in your config.yml file: graphql:
#...
security:
black_list: ['hello'] # or white_list: ['hello']
Using security voter:Used to guard any field resolve and support two types of guards: root operation and any other field resolving (including internal fields, scalar type fields, root operations). To guard root operation with your specified logic you need to enable it in configuration and use
Config example: graphql:
security:
guard:
field: true # for any field security
operation: true # for root level security Voter example (add in to your use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQLBundle\Security\Manager\SecurityManagerInterface;
class GraphQLVoter extends Voter
{
/**
* @inheritdoc
*/
protected function supports($attribute, $subject)
{
return in_array($attribute, [SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE, SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE]);
}
/**
* @inheritdoc
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
// your own validation logic here
if (SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE == $attribute) {
/** @var $subject ResolveInfo */
if ($subject->getField()->getName() == 'hello') {
return false;
}
return true;
} elseif (SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE == $attribute) {
/** @var $subject Query */
if ($subject->getName() == '__schema') {
return true;
}
}
}
} GraphiQL extension:To run graphiql extension just try to access to DocumentationAll detailed documentation is available on the main GraphQL repository – http://github.com/youshido/graphql/. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论