Skip to content

Custom query variable names #1

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

Closed
rebing opened this issue Sep 26, 2016 · 2 comments
Closed

Custom query variable names #1

rebing opened this issue Sep 26, 2016 · 2 comments
Labels

Comments

@rebing
Copy link
Owner

rebing commented Sep 26, 2016

Currently the variable names have to exactly match the argument names.

Valid: query($id:Int){user(id:$id){...
Invalid: query($userId:Int){user(id:$userId){...

@rebing rebing added the bug label Sep 26, 2016
@Towerful
Copy link

Towerful commented Jan 17, 2017

When I was trying to get an eager loading graphql library working, I came up with the following:

public function getFieldSelection(ResolveInfo $info, $depth = 0)
{
    $fields = [];

    /** @var Field $fieldAST */
    foreach ($info->fieldASTs as $fieldAST) {
        $fields = array_merge_recursive($fields, $this->foldSelectionSet($fieldAST->selectionSet, $info, $depth));
    }

    return $fields;
}

private function foldSelectionSet(SelectionSet $selectionSet, ResolveInfo $info, $descend)
{
    $fields = [];

    foreach ($selectionSet->selections as $selectionAST) {
        if ($selectionAST instanceof Field) {
            $name = $this->nameOf($selectionAST)
            if($descend > 0 && !empty($selectionAST->selectionSet)) {
                $selection = $this->foldSelectionSet($selectionAST->selectionSet, $descend - 1);
                $selection['__args'] = $this->getArguments($selectionAST, $info);
                $fields[$name] = $field;
            } else {
                $fields[$name] = true;
            }
        } else if ($selectionAST instanceof FragmentSpread) {
            $spreadName = $this->nameOf($selectionAST);
            if (isset($info->fragments[$spreadName])) {
                /** @var FragmentDefinition $fragment */
                $fragment = $info->fragments[$spreadName];
                $fields += $this->foldSelectionSet($fragment->selectionSet, $descend);
            }
        }
    }

    return $fields;
}

//helps tidy up the code
private function nameOf($nv) 
{
    return $nv->name->value;
}


private function getArguments(Field $selectionAST, ResolveInfo $info) 
{
    //get the arguments for the current AST from the query.
    $args = [];
    foreach ($selectionAST->arguments as $argument) {
        $graphName = $this->nameOf($argument);
        $queryName = $this->nameOf($argument->value);
        $args[$graphName] = $info->variableValues[$queryName];
    }
    return $args;
}

This is the whats happening inside ResolveInfo->getFieldSelection() method.
This version of the methods appends an __args index to each depth of the array, and pulls the appropriate argument from the ResolveInfo.

Slightly altered it to fit your needs, so I havent tested it.
But you should be able to drop these into \Support\SelectFields class, and use it instead of the ResolveInfo->getFieldSelection();
Altho, it might be worth wrapping ResolveInfo into a decorator that provides the functionality.

Anyway, then its just a matter of adapting the rest to plumb the arguments in...

I'm going to play around tomorrow & see what I can do!

@rebing
Copy link
Owner Author

rebing commented Jan 17, 2017

Hi, it seems I've already fixed the problem, but forgot to close the issue. Thanks for trying to help though :)

@rebing rebing closed this as completed Jan 17, 2017
mfn added a commit that referenced this issue Jun 18, 2019
Reported by phpstan:
Parameter #1 $schema of method Rebing\GraphQL\GraphQL::getSchemaConfiguration() expects array|string, array|string|null given
mfn added a commit that referenced this issue Jun 18, 2019
…elds

Reported by phpstan:
Parameter #1 $field of static method Rebing\GraphQL\Support\SelectFields::addFieldToSelect() expects string, int|string given.
mfn added a commit that referenced this issue Jun 19, 2019
…leMap

Reported by larastan:
- Parameter #1 $path of method Illuminate\Filesystem\Filesystem::copy() expects string, int|string given.
- Parameter #1 $from of method Rebing\GraphQL\Console\PublishCommand::status() expects string, int|string given.
mfn added a commit that referenced this issue Jun 19, 2019
Reported by larastan:
Parameter #1 $name of method Rebing\GraphQL\Support\Type::getFieldResolver() expects string, int|string given
mfn added a commit that referenced this issue Jun 19, 2019
…leMap

Reported by larastan:
- Parameter #1 $path of method Illuminate\Filesystem\Filesystem::copy() expects string, int|string given.
- Parameter #1 $from of method Rebing\GraphQL\Console\PublishCommand::status() expects string, int|string given.
mfn added a commit that referenced this issue Jun 19, 2019
Reported by larastan:
Parameter #1 $name of method Rebing\GraphQL\Support\Type::getFieldResolver() expects string, int|string given
mfn pushed a commit that referenced this issue Jun 19, 2019
mfn added a commit that referenced this issue Jan 6, 2023
…Command::setLaravel() expects Illuminate\Contracts\Container\Container, Illuminate\Foundation\Application|null given"
mfn added a commit that referenced this issue Mar 31, 2025
This was run to fix https://github.com/rebing/graphql-laravel/actions/runs/14163339317/job/39672300001
```
  Line   src/GraphQL.php
 ------ -------------------------------------------------------------------------------------------------------------------------------------------
  259    Parameter #1 $type of static method
         GraphQL\Type\Definition\Type::nonNull() expects (callable():
         (GraphQL\Type\Definition\NullableType&GraphQL\Type\Definition\Type))|(GraphQL\Type\Definition\NullableType&GraphQL\Type\Definition\Type),
         GraphQL\Type\Definition\Type given.
         🪪  argument.type
```
but it seems phpstan also changed the internal format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants