Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wildcard path for @inject directive #1206

Open
ghost opened this issue Feb 20, 2020 · 1 comment · May be fixed by #2280
Open

Add wildcard path for @inject directive #1206

ghost opened this issue Feb 20, 2020 · 1 comment · May be fixed by #2280
Labels
enhancement A feature or improvement

Comments

@ghost
Copy link

ghost commented Feb 20, 2020

Considering this example schema

type User {
    id: ID!
    name: String!
    comments: [Comment!]! @hasMany
}

type Post {
    id: ID!
    title: String!
    body: String!
    user: User! @belongsTo
}

type Comment {
    id: ID!
    title: String!
    body: String!
    user: User! @belongsTo
}

input UpdateUserInput {
    comments: UpdateCommentsHasMany
}

input UpdateCommentsHasMany {
    create: [CreateCommentInput!]
}

input CreateCommentInput {
    post_id: ID!
    title: String!
    body: String!
}

type Mutation @guard {
    updateUser(input: UpdateUserInput!): User 
        @update
        @inject(context: "user.id", name: "comments.create.*.user_id")
}

It would be awesome if Lighthouse could inject data in the argument set using wildcard in the dot notation, this would cover the nested mutations in the HasMany case, while ensuring that a comment will be created for the authenticated user or a mutation that uses a list of input types.

Of course, today, I can create something like:

input CreateCommentInput {
    post_id: ID!
    title: String!
    body: String!
}

type Mutation @guard {
    createComment(input: CreateCommentInput!): Comment!
        @create
        @inject(context: "user.id", name: "customer_id")
}

But, well, this way I'm not using the power of nested mutations.

Maybe I'm wrong, but I think the change needed is just use data_fill instead of Illuminate\Support\Arr::add in the Nuwave\Lighthouse\Schema\Directives\InjectDirective class and rewrite the addValue method of Nuwave\Lighthouse\Execution\Arguments\ArgumentSet class to behave like data_fill as actually it is behaving like Illuminate\Support\Arr::add

Or maybe create a new directive and method.

I'm willing to help with a PR when as soon as I have time available.

Lighthouse Version: 4.9
Laravel Version: 6.12.0

@spawnia spawnia added the enhancement A feature or improvement label Mar 15, 2020
@spawnia
Copy link
Collaborator

spawnia commented Mar 15, 2020

Thank you for your contribution. You have a totally valid use case.

Powering up @inject in the way you described seems reasonable, i would be willing to incorporate a PR for this.

Another approach to solving that issue would be to default comments/posts to hold the ID of the currently authenticated user when creating them. We are using model events for that. Helps a great deal when most of the models in your application belong to the current user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature or improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant