🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[toc] #### 定义 schema 是你的类型层次结构的容器,它的构造函数接收根类型(root-level type),并且向内部GraphQL工具提供接收有关类型信息的方法。 schema 是 `GraphQL\Schema` ( 或其子类 )的实例,它的构造函数接收数组配制参数。 ~~~ $schema = new Schema([ 'query' => $queryType, 'mutation' => $mutationType, ]); ~~~ #### Query and Mutation types Schema由两个` root types` 组成。 * Query type 是 read API 的接口 * Mutation type(可选) 是你应用中定义的所有可能的操作的公共的 write API 查询和更改类型是包含你的API的`root-level fields`的常规 `object types `。 ~~~ use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use GraphQL\Schema; $queryType = new ObjectType([ 'name' => 'Query', 'fields' => [ 'hello' => [ 'type' => Type::string(), 'resolve' => function() { return 'Hello World!'; } ] 'hero' => [ 'type' => $characterInterface, 'args' => [ 'episode' => [ 'type' => $episodeEnum ] ], 'resolve' => function ($rootValue, $args) { return StarWarsData::getHero(isset($args['episode']) ? $args['episode'] : null); }, ] ] ]); $mutationType = new ObjectType([ 'name' => 'Mutation', 'fields' => [ 'createReviewForEpisode' => [ 'type' => $createReviewForEpisodeMutation, 'args' => [ 'episode' => $episodeEnum, 'review' => $reviewInputObject ], 'resolve' => function($val, $args) { // TODOC } ] ] ]); ~~~ 请注意:除了是声明你的API接口的表层外,这些类型和其他的`object type`是一样的,并且字段的工作也是一样的。 `Mutation` 类型同样仅仅是常规对象类型,区别在于主义,`Mutation`类型的字段名字通常是动词 并且 他们几乎总是有参数 - 一些常常是复杂输入的值(`Input Types`,如新增一条数据) #### 配制项 | 名字 | 类型 | 描述 | | --- | --- | --- | | query | `ObjectType` | 包含 read API 的 root-level 字段的对象类型 (通常命名 "Query") | | mutation | `ObjectType` | 包含 write API 的 root-level 字段的对象类型 (通过命名 "Mutation")。 | | subscription | `ObjectType` | 预留供将来订阅实现,当前提供是为了兼容 graphql-js 的内省查询。 | | directives | `Directive[] ` | 你的schema支持的指令的完整列表。默认包含内建的 @skip and @include。<br>如是你要传递自己的指令并且仍然想使用内建的指定 :array_merge(GraphQL::getInternalDirectives(), [$myCustomDirective]) | | types | `ObjectType[]` | 在 静态模式分析 (static schema analysis)期间无法由graphql检测的对象类型列表。通常发生在对象类型没有被字段直接引用,但是仍然是schema的一部分,这是因为它实现了接口(union type因为有types字段,可以被分析器检测),接口在resolveType 回调中解析为这个对象类型。 |