在灯塔graphql文件中声明变量

时间:2020-05-29 13:04:43

标签: laravel graphql lighthouse laravel-lighthouse

我已经创建了一个graphql文件,如下所示:

type Field {
    id: ID!
    name_en: String!
    name_fa: String!
    description: String!
    img_url: String!
    created_at: DateTime!
    updated_at: DateTime!

    subFields: [SubField] @hasMany
}

extend type Query {
    fields(input: ListInput @spread): [Field] @paginate(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field" defaultCount: 10)
    field(id: ID! @eq): Field @find(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")
}

extend type Mutation {
    createField(
        name_en: String! @rules(apply: ["required"])
        name_fa: String
        description: String
        img_url: String
    ): Field @create(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")

    updateField(
        id: ID! @rules(apply: ["required", "int"])
        name_en: String @rules(apply: ["required"])
        name_fa: String
        description: String
        img_url: String
    ): Field @update(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")

    deleteField(
        id: ID! @rules(apply: ["required", "int"])
    ): Field @delete(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")
}

每次我要引用Field模型时,都必须键入整个App\\Models\\CustomerManagement\\BusinessInformation\\Field。我想知道是否可以声明一个像model_namespace这样的变量,并使用它代替大型模型名称空间。

1 个答案:

答案 0 :(得分:2)

编辑:

https://lighthouse-php.com/4.4/api-reference/directives.html#namespace

您应该使用@namespace指令

extend type Query @namespace(field: "App\\Blog") {
  posts: [Post!]! @field(resolver: "Post@resolveAll")
}

您有两种选择: 您基本上可以使用名称空间配置来加载默认名称空间数组: https://github.com/nuwave/lighthouse/pull/525/files

或者使用组指令: https://lighthouse-php.com/3.0/api-reference/directives.html#group