oligus/schema

面向对象GraphQL模式

1.0.0-beta2 2020-02-23 16:02 UTC

This package is auto-updated.

Last update: 2024-09-24 05:30:49 UTC


README

GraphQL模式库。

Build Status License: MIT Codecov.io

安装

$ composer require oligus/schema

内容

快速入门
类型
    类型修饰符
    标量
    内置标量类型
    对象
    接口
    枚举
    输入
字段
    参数
    参数值
开发

快速入门

$schema = new Schema();
// Add directive
$directive = new DirectiveType('upper');
$directive->addLocation(ExecutableDirectiveLocation::FIELD());
$schema->addDirective($directive);

// Add interface
$interface = (new InterfaceType('Entity'))
    ->addField(new Field('id', new IDType(), new TypeModifier(false)))
    ->addField(new Field('name', new StringType()));
$schema->addInterface($interface);

// Add scalar
$scalar = new ScalarType('Url');
$schema->addScalar($scalar);

// Add object
$object = (new ObjectType('User'))
    ->addField(new Field('id', new IDType(), new TypeModifier(false)))
    ->addField(new Field('name', new StringType()))
    ->addField(new Field('age', new IntegerType()))
    ->addField(new Field('balance', new FloatType()))
    ->addField(new Field('isActive', new BooleanType()));

$object->addField(new Field('friends', $object, new TypeModifier(true, true, false)))
    ->addField(new Field('homepage', $scalar))
    ->implements($interface);

$schema->addObject($object);

// Add query object
$query =  (new ObjectType('Query'))
    ->addField(new Field('me', $object, new TypeModifier(true)));
$field = (new Field('friends', $object, new TypeModifier(true, true, false)))
    ->addArgument(new Argument('limit', new IntegerType(), new TypeModifier(), new ValueInteger(10)));
$query->addField($field);

$schema->addObject($query);

// Add input object
$input = (new InputType('ListUsersInput'))
    ->addField(new Field('limit', new IntegerType()))
    ->addField(new Field('since_id', new IDType()));

$schema->addInput($input);

// Add mutation object
$mutation =  new ObjectType('Mutation');
$field = (new Field('users', $object, new TypeModifier(true, true, false)))
    ->addArgument(new Argument('params', $input));
$mutation->addField($field);
$schema->addObject($mutation);

// Add union
$union = (new UnionType('MyUnion'))
    ->addObjectType(new ObjectType('Dog'))
    ->addObjectType(new ObjectType('Cat'))
    ->addObjectType(new ObjectType('Bird'));

$schema->addUnion($union);

// Set root types
$schema->setQuery($query);
$schema->setMutation($mutation);

$serializer = new SchemaSerializer();
$serializer->serialize($schema);

结果

directive @upper on FIELD

interface Entity {
  id: ID!
  name: String
}

scalar Url

union MyUnion = Dog | Cat | Bird

type User implements Entity {
  id: ID!
  name: String
  age: Int
  balance: Float
  isActive: Boolean
  friends: [User]!
  homepage: Url
}

type Query {
  me: User
  friends(limit: Int = 10): [User]!
}

type Mutation {
  users(params: ListUsersInput): [User]!
}

input ListUsersInput {
  limit: Int
  since_id: ID
}

schema {
  query: Query
  mutation: Mutation
}

类型

GraphQL模式的根本单位是类型。GraphQL中有六种命名类型定义,两种包装类型。

GrapQL规范

可用类型

ScalarType

BooleanType
FloatType
IDType
IntegerType
StringType

InterfaceType

类型修饰符

类型修饰符与类型一起使用,向类型添加修饰符以修改相关类型。

定义

TypeModifier(?bool $nullable, ?bool $listable, ?bool $nullableList)

修饰符

示例

$typeModifier = new TypeModifier($nullable = false, $listable = true, $nullableList = false);
$type = new BooleanType($typeModifier);

结果

[bool!]!

标量

标量类型代表GraphQL类型系统中的原始叶值。GraphQL响应采用层次树的形式;这些树上的叶子是GraphQL标量。

GrapQL规范

定义

Scalar(string $name, ?string $description)

示例

$scalar = new ScalarType('Url', 'Url description');

结果

"""
Url description
"""
scalar Url

内置标量类型

GraphQL提供了一套基本且定义良好的标量类型。GraphQL服务器应支持所有这些类型。

内置类型: 布尔型,浮点型,ID,整数,字符串

GrapQL规范

定义

<TYPE>Type(?TypeModifier $modifier)

其中 <TYPE> 是布尔型,浮点型,ID,整数或字符串

示例

$type = new BooleanType();

结果

Boolean

对象

GraphQL查询是层次化和组合的,描述了一个信息树。虽然标量类型描述了这些层次查询的叶子值,但对象描述了中间级别。

GrapQL规范

定义

ObjectType(string $name, ?string $description = null)

示例

$object = new ObjectType('Wine');
$object->addField(new Field('name', new StringType()));
$object->addField(new Field('age', new IntegerType()));
$object->addField(new Field('size', new IntegerType()));

结果

type Wine {
  name: String
  age: Int
  size: Int
}

实现接口

$interface = new InterfaceType('Wine');
$interface->addField(new Field('name', new StringType()));
$object->implements($interface);

结果

type Wine implements Name {
  name: String
  age: Int
  size: Int
}

接口

GraphQL接口代表一组命名字段及其参数。GraphQL对象可以实现这些接口,这要求对象类型将定义这些接口中定义的所有字段。

GrapQL规范

定义

InterfaceType(string $name, ?string $description = null)

示例

$interface = new InterfaceType('Wine');
$interface->addField(new Field('name', new StringType()));
$interface->addField(new Field('age', new IntegerType()));
$interface->addField(new Field('size', new IntegerType()));

结果

interface Wine {
  name: String  
  age: Int
  size: Int
}

联合

GraphQL联合代表一个对象,可能是多个GraphQL对象类型之一,但不提供在这些类型之间的保证字段。它们与接口的不同之处在于,对象类型声明它们实现了哪些接口,但不知道它们被哪些联合包含。

GrapQL规范

定义

UnionType(string $name, ?string $description = null)

添加对象

addObjectType(ObjectType $objectType): void

示例

$union = new UnionType('Animals');
$union->addObjectType(new ObjectType('Dog'));
$union->addObjectType(new ObjectType('Cat'));

结果

union Animals = Dog | Cat

枚举

GraphQL枚举类型,类似于标量类型,也代表GraphQL类型系统中的叶值。然而,枚举类型描述的是可能值的集合。

GrapQL规范

定义

EnumType(string $name, ?string $description = null, array $enums = [])

添加枚举

add(string $enum)

示例

$enum = new EnumType('Direction', 'Different directions', ['SOUTH', 'NORTH']);
$enum->addEnum('EAST');
$enum->addEnum('WEST');

结果

"""
Different directions
"""
enum Direction {
  SOUTH
  NORTH
  EAST
  WEST
}

指令

GraphQL模式描述了指令,用于注释GraphQL文档的各个部分,以指示它们应由验证器、执行器或代码生成器等客户端工具以不同的方式评估。

GrapQL规范

定义

EnumType(string $name, ?string $description = null, array $enums = [])

添加位置

add(ExecutableDirectiveLocation $location)

示例

$directive = new DirectiveType('example', 'Example directive');
$directive->addLocation(ExecutableDirectiveLocation::FIELD());
$directive->addLocation(ExecutableDirectiveLocation::INLINE_FRAGMENT());

结果

"""
Example directive
"""
directive @example on FIELD | FRAGMENT_SPREAD

输入

字段可以接受参数以配置其行为。这些输入通常是标量或枚举,但有时需要表示更复杂的值。

GrapQL规范

定义

InputType(string $name, ?string $description = null)

添加字段

addField(Field $field): void

示例

$object = new InputType('Animal');
$object->addField(new Field('name', new StringType()));
$object->addField(new Field('age', new IntegerType()));
$object->addField(new Field('weight', new IntegerType()));

结果

input Animal {
  name: String
  age: Int
  weight: Int
}

字段

选择集主要由字段组成。字段描述了在选择集中可请求的一个离散信息片段。

GrapQL规范

定义

Field(string $name, Type $type, ?TypeModifier $typeModifier, ?string $description)

示例

$field = new Field('simpleField', new IntegerType());

结果

simpleField: Int

使用类型修饰符

$field = new Field('simpleField', new IntegerType(), new TypeModifier($nullable = false));

结果

simpleField: Int!

使用类型参数

$field = new Field('booleanListArgField', new BooleanType(), new TypeModifier(true, true));

$argument = new Argument('booleanListArg', new BooleanType(), new TypeModifier(true, true, false));
$field->addArgument($argument);

参数

字段在概念上是函数,返回值,并且有时接受参数以改变其行为。这些参数通常直接映射到GraphQL服务器实现中的函数参数。

GrapQL规范

定义

Argument(string $name, Type $type, ?TypeModifier $typeModifier, ?Value $defaultVale)

示例

$argument = new Argument('booleanArg', new BooleanType());

结果

booleanArg: Boolean

使用类型修饰符

$argument = new Argument('intArg', new IntegerType(), new TypeModifier(false));
// intArg: Int! = 0

具有默认值类型

$argument = new Argument('intArg', new IntegerType(), null, new ValueInteger(0));
// intArg: Int = 0

参数值

为参数中的默认值设置简单标量值。

定义

Value(mixed $value)

可用值: ValueBoolean, ValueFloat, ValueInteger, ValueNull, ValueString

示例

$bool = new ValueBoolean(true);
$bool->getValue(); // true
echo $bool; // 'true'

开发

下载和构建Docker容器

$ make

访问Docker镜像

$  make bash