railt/laravel-provider

Railt 的 Laravel 框架服务提供者

dev-master / 2.x-dev 2023-10-17 23:25 UTC

This package is auto-updated.

Last update: 2024-09-18 01:38:59 UTC


README

Railt

PHP 8.1+ railt.org Discord Latest Stable Version Total Downloads License MIT

Testing

此 Laravel 提供者提供了使用 GraphQL 的集成,通过 Railt GraphQL

要求

  • php: ^8.1
  • laravel: ^9.0|^10.0
  • railt/railt: ^2.0

安装

库作为 composer 仓库可用,可以在项目的根目录中使用以下命令安装。

$ composer require railt/laravel-provider

然后,将提供者添加到 config/app.php 文件中的 providers 列表

    'providers' => [
        // ...
        /*
         * Package Service Providers...
         */
         Railt\LaravelProvider\RailtServiceProvider::class,
         
         /*
          * Application Service Providers...
          */
         // ...
    ],

要发布资产(配置文件、示例文件等),请使用以下命令

$ php artisan vendor:publish --tag=railt

配置

所有应用程序配置都位于 config/railt.php 文件中。

主应用程序的默认路由

  • /graphql - 用于 GraphQL 请求。
  • /graphiql - 用于 GraphQL 演示场(GraphQL Web IDE)。

所有配置选项

以下是一个包含所有可能选项的提供者配置示例。

<?php

return [
    /*
    |---------------------------------------------------------------------------
    | List of Compilers
    |---------------------------------------------------------------------------
    |
    | ```
    | 'compilers' => [
    |   <name-1> => [
    |     'option' => 'value-1',
    |   ],
    |   <name-2> => [
    |     'option' => 'value-2',
    |   ],
    | ],
    | ```
    */
    'compilers' => [
        'default' => [
            /*
            | Reference to the cache store.
            |
            | See "cache.stores" in /config/cache.php.
            |
            | default: null
            */
            'cache' => null,

            /*
            | Compiler's specification version.
            |
            | Should be one of:
            | - "railt" - Modern extended version of specification.
            | - "draft" - See https://spec.graphql.net.cn/draft/
            | - "october-2021" - See https://spec.graphql.net.cn/October2021/
            | - "june-2018" - See https://spec.graphql.net.cn/June2018/
            | - "october-2016" - See https://spec.graphql.net.cn/October2016/
            | - "april-2016" - See https://spec.graphql.net.cn/April2016/
            | - "october-2015" - See https://spec.graphql.net.cn/October2015/
            | - "july-2015" - See https://spec.graphql.net.cn/July2015/
            |
            | default: "railt"
            */
            'spec' => 'railt',

            /*
            | Reference to predefined types service.
            |
            | Should be instance of `Railt\TypeSystem\DictionaryInterface`.
            |
            | default: null
            */
            'types' => null,

            /*
            | Autogenerated root types stubs.
            */
            'generate' => [
                /*
                | Generated root object type name for queries.
                |
                | default: "Query"
                */
                'query' => 'Query',

                /*
                | Generated root object type name for mutations.
                |
                | default: null
                */
                'mutation' => null,

                /*
                | Generated root object type name for subscriptions.
                |
                | default: null
                */
                'subscription' => null,
            ],

            /*
            | Auto casting types compiler's options.
            */
            'cast' => [
                /*
                | Allow to cast integer values as floats.
                |
                | ```
                | input Example {
                |
                |   "Allow Int(1) as default of Float"
                |   inCaseOfEnabled(arg: Float = 1): Any
                |
                |   "Allow only Float(1.0) as default of Float"
                |   inCaseOfDisabled(arg: Float = 1.0): Any
                |
                | }
                | ```
                |
                | default: true
                */
                'int_to_float' => true,

                /*
                | Allow to cast scalar values as strings.
                |
                | ```
                | input Example {
                |
                |   "Allow Float(1.0) as default of String"
                |   inCaseOfEnabled(arg: String = 1.0): Any
                |
                |   "Allow only String("1.0") as default of String"
                |   inCaseOfDisabled(arg: String = "1.0"): Any
                |
                | }
                | ```
                |
                | default: true
                */
                'scalar_to_string' => true,
            ],

            /*
            | Default values extraction logic.
            */
            'extract' => [
                /*
                | Allow to extract nullable types as default values.
                |
                | ```
                | input Example {
                |
                |   "Allow nullables as default values"
                |   inCaseOfEnabled(arg: String): Any
                |
                |   "In case of disabled the default value must be defined explicitly"
                |   inCaseOfDisabled(arg: String = null): Any
                |
                | }
                | ```
                |
                | default: true
                */
                'nullable' => true,

                /*
                | Allow to extract list types as default values.
                |
                | ```
                | input Example {
                |
                |   "Allow lists as default values"
                |   inCaseOfEnabled(arg: [String]!): Any
                |
                |   "In case of disabled the default value must be defined explicitly"
                |   inCaseOfDisabled(arg: [String]! = []): Any
                |
                | }
                | ```
                |
                | default: true
                */
                'list' => true,
            ],

            /*
            | List of directories from which GraphQL files should be loaded.
            |
            | In the case that a "resource_path('graphql')" directory is
            | specified, then in case when assembling the schema, type "Example" is
            | required (for example: `field(arg: Example): String`) then
            | "/resources/graphql/Example.graphqls" or
            | "/resources/graphql/Example.graphql" will be loaded (if exists).
            |
            | default: []
            */
            'autoload' => [
                \resource_path('graphql'),
            ],
        ],
    ],

    /*
    |---------------------------------------------------------------------------
    | List of public GraphQL endpoints.
    |---------------------------------------------------------------------------
    |
    | ```
    | 'endpoints' => [
    |   <name-1> => [
    |     'option' => 'value-1',
    |   ],
    |   <name-2> => [
    |     'option' => 'value-2',
    |   ],
    | ],
    | ```
    */
    'endpoints' => [
        'default' => [
            /*
            | URI pathname to the GraphQL endpoint.
            |
            | required
            */
            'route' => '/graphql',

            /*
            | List or available route methods.
            |
            | default: ['post']
            */
            'methods' => ['get', 'post', 'put', 'patch'],

            /*
            | Pathname to the GraphQL schema file.
            |
            | required
            */
            'schema' => \resource_path('graphql/schema.graphqls'),

            /*
            | List of variables passed to the schema file.
            |
            | You can use these values inside the schema file:
            |
            | ```
            | variables:
            |   exampleController: "Path\To\ExampleController"
            | ```
            |
            | ```
            | type UserList {
            |     get(count: Int! = 100): [User!]
            |         @route(action: $exampleController)
            | }
            | ```
            |
            | default: []
            */
            'variables' => [
                'isDebug' => env('APP_DEBUG'),
            ],

            /*
            | Reference to defined compiler (from "compilers" section) name or
            | reference to Symfony's DI service.
            |
            | default: null
            */
            'compiler' => 'default',

            /*
            | List of Laravel middleware.
            |
            | default: []
            */
            'middleware' => [],

            /*
            | List of Railt GraphQL extensions (plugins).
            |
            | Should be instance of `Railt\Foundation\Extension\ExtensionInterface`.
            |
            | default: []
            */
            'extensions' => [
                Railt\Extension\Router\RouterExtension::class,
                Railt\Extension\DefaultValue\DefaultValueExtension::class,
            ],
        ],
    ],

    /*
    |---------------------------------------------------------------------------
    | GraphQL Playground
    |---------------------------------------------------------------------------

    |
    | List of GraphQL playground (GraphiQL) endpoints.
    | See: https://github.com/graphql/graphiql
    |
    | ```
    | 'playground' => [
    |   <name-1> => [
    |     'option' => 'value-1',
    |   ],
    |   <name-2> => [
    |     'option' => 'value-2',
    |   ],
    | ],
    | ```
    */
    'playground' => [
        'default' => [
            /*
            | Reference to "endpoints" section for which this
            | playground will be used.
            |
            | required
            */
            'endpoint' => 'default',

            /*
            | URI pathname of playground.
            |
            | required
            */
            'route' => '/graphiql',

            /*
             | List or available route methods.
             |
             | default: ['get']
             */
            'methods' => ['get', 'head', 'options'],

            /*
            | List of route Laravel middleware.
            |
            | default: []
            */
            'middleware' => [],

            /*
            | List of additional optional headers that be used for each request.
            |
            | default: []
            */
            'headers' => [
                'X-Api-Playground' => 'GraphiQL',
            ],
        ],
    ],
];