arthem/graphql-mapper

此包已被弃用且不再维护。未建议替代包。

GraphQL 模型映射器

0.0.2 2016-05-20 21:52 UTC

This package is auto-updated.

Last update: 2022-02-01 12:54:25 UTC


README

Build Status SensioLabsInsight Scrutinizer Code Quality

此库允许您根据模型构建 GraphQL 模式。它依赖于 GraphQL PHP 实现

查看 graphql-mapper-demo 以获取完整的示例!

安装

可以通过 Composer 作为 arthem/graphql-mapper 安装

composer require arthem/graphql-mapper

设置/配置

创建您的模式

# /path/to/your/mapping/file.yml

interfaces:
    Character:
        resolve:
            handler: doctrine
        model: AppBundle\Entity\Character
        description: A character in the Star Wars Trilogy
        fields:
            id:
                type: Int!
                description: The id of the character.
            name:
                type: String!
                description: The name of the character.
            friends:
                type: "[Character]"
                description: The friends of the character, or an empty list if they have none.
            appearsIn:
                type: "[Episode]"
                description: Which movies they appear in.

types:
    Episode:
        description: One of the films in the Star Wars Trilogy
        values:
            NEWHOPE:
                value: 4
                description: Released in 1977.
            EMPIRE:
                value: 5
                description: Released in 1980.
            JEDI:
                value: 6
                description: Released in 1983.

    Human:
        resolve:
            handler: doctrine
        model: AppBundle\Entity\Human
        description: A humanoid creature in the Star Wars universe.
        interfaces: Character
        fields:
            id:
                description: The id of the human.
            name:
                description: The name of the human.
            friends:
                type: "[Character]"
                description: The friends of the human, or an empty list if they have none.
            appearsIn:
                type: "[Episode]"
                description: Which movies they appear in.
            homePlanet:
                description: The home planet of the human, or null if unknown.

    Droid:
        resolve:
            handler: doctrine
        model: AppBundle\Entity\Droid
        description: A mechanical creature in the Star Wars universe.
        interfaces: Character
        fields:
            id:
                description: The id of the droid.
            name:
                description: The name of the droid.
            friends:
                type: "[Character]"
                description: The friends of the droid, or an empty list if they have none.
            appearsIn:
                type: "[Episode]"
                description: Which movies they appear in.
            primaryFunction:
                description: The primary function of the droid.

query:
    fields:
        hero:
            resolve:
                method: getHero
            type: Character
            args:
                episode:
                    description: If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.
                    type: Episode
        human:
            type: Human
            args:
                id:
                    description: id of the human
                    type: String!
        droid:
            type: Droid
            args:
                id:
                    description: id of the droid
                    type: String!
        date:
            type: "[String]"
            description: The current time
            resolve:
                function: getdate
                no_args: true

mutation:
    fields:
        createDroid:
            type: Droid
            resolve:
                method: createDroid
            args:
                id:
                    type: Int!
                    description: The id of the droid.
                name:
                    type: String!
                    description: The name of the droid.
                primaryFunction:
                    type: String
                    description: The primary function of the droid.
                appearsIn:
                    type: "[Episode]"
                    description: Which movies they appear in.

注意:类型列表必须用引号括起来 type: "[User]"

用法

// entry.php
use Arthem\GraphQLMapper\GraphQLManager;
use Arthem\GraphQLMapper\SchemaSetup;
use Arthem\GraphQLMapper\Exception\QueryException;

// bootstrap.php
require_once '../vendor/autoload.php';

// replace with mechanism to retrieve Doctrine EntityManager in your app
$entityManager = getEntityManager();

// GraphQL part
$paths          = ['/path/to/your/mapping/file.yml'];
$schemaFactory  = SchemaSetup::createDoctrineYamlSchemaFactory($paths, $entityManager);
$graphQLManager = new GraphQLManager($schemaFactory);

try {
    $data = $graphQLManager->query($_POST['query']);
    echo json_encode($data);
} catch (QueryException $e) {
    echo json_encode($e);
}

准备查询

curl -XPOST 'http://localhost/entry.php' -d 'query=query FooBar {
    luke: hero(episode: EMPIRE) {
        id,
        name,
        friends {
            id, name
        }
    },
    droid(id: "2001") {
        primaryFunction
    }
}'

自定义解析器

解析器负责创建用于解析数据的函数(闭包)。使用特定工厂的方式是在 resolve 节点中定义 handler 键。内部处理程序包括: propertycallabledoctrine

但您可以定义自己的!

创建您的 CustomResolver,它实现了 Arthem\GraphQLMapper\Schema\Resolve\ResolverInterface

然后将它注册到 SchemaFactory

$schemaFactory  = SchemaSetup::createDoctrineYamlSchemaFactory($paths, $entityManager);
$schemaFactory->addResolver(new CustomResolver());

自定义猜测器

待定

许可证

MIT 许可证 下发布。