Объявить переменную в файлах Lighthouse graphql - PullRequest
5 голосов
/ 29 мая 2020

Я создал такой файл 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 Ответ

1 голос
/ 31 мая 2020

EDIT:

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

...