Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare variable in lighthouse graphql files

I've created a graphql file like this:

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")
}

Every time I want to refer to Field model, I have to type the whole App\\Models\\CustomerManagement\\BusinessInformation\\Field. I was wondering if I could declare a variable like model_namespace and use it instead of the big model namespace.

like image 722
Alireza A2F Avatar asked Nov 06 '22 07:11

Alireza A2F


1 Answers

EDIT:

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

You should use the @namespace directive

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

You have a couple of options: You can basically load a default namespace array using namespace configuration: https://github.com/nuwave/lighthouse/pull/525/files

Or alternatively use a group directive: https://lighthouse-php.com/3.0/api-reference/directives.html#group

like image 86
TonyPepperoni Avatar answered Nov 22 '22 03:11

TonyPepperoni