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

Include aliases in query plan #1072

Closed
crissi opened this issue Feb 16, 2022 · 8 comments
Closed

Include aliases in query plan #1072

crissi opened this issue Feb 16, 2022 · 8 comments

Comments

@crissi
Copy link
Contributor

crissi commented Feb 16, 2022

Running lookAhead with queryplan with this query:

$resolveInfo->lookAhead(['group-implementor-fields'])->queryPlan();
query  {
  caseById(id: 7317) {
    default: schemesCount
    closed: schemesCount(status: CLOSED)
    approved: schemesCount(status: APPROVED)
  }
}

Gives us:

{
  "fields": {
    "schemesCount": {
      "type": "Int",
      "fields": [],
      "args": {
        "status": "APPROVED"
      }
    }
  }
}

I suspect all fields should be included somehow in the result!

@spawnia
Copy link
Collaborator

spawnia commented Feb 16, 2022

I think the purpose of the query plan is to allow the server to make decisions on how to resolve the query efficiently. Given aliases are given by the client and transparently handled by the execution engine of this library, they do not seem useful to include.

@crissi
Copy link
Contributor Author

crissi commented Feb 16, 2022

yes, but since each alias can have different arguments like in my example.

Here it just returns the argument of the last schemesCount property.

@spawnia spawnia reopened this Feb 16, 2022
@spawnia
Copy link
Collaborator

spawnia commented Feb 16, 2022

Right, that is misleading. args should really be a list with multiple args passed to the different aliases - same with fields actually. Perhaps a better structure would be:

{
  "fields": {
    "schemesCount": {
      "type": "Int",
      "aliases": [
        {
          "fields": [],
          "args": {}
        },
        {
          "fields": [],
          "args": {
            "status": "APPROVED"
          }
        },
        {
          "fields": [],
          "args": {
            "status": "CLOSED"
          }
        },
      ]
    }
  }
}

@spawnia spawnia changed the title include aliases in query plan Include aliases in query plan Feb 16, 2022
@crissi
Copy link
Contributor Author

crissi commented Feb 16, 2022

maybe include the alias name in there for good measure

@jeroenschieven
Copy link

We are having the same issue. We use the queryPlan to dynamically build an ElasticSearch query. Some fields have quite some possible arguments, therefore we want to be able to alias those fields.

Unfortunately, the only alternative for now is duplicating these fields.

@zephyx
Copy link
Contributor

zephyx commented Nov 29, 2024

I needed this too, so I have modified the lib to get this feature.
I don't know if it will be merged or no, but if needed for your application: #1648

I use it as follow:

=> In My Query Resolver :

$aliasArgs = $info->lookAhead()->aliasArgs();
//I can then create multiple results for each of the aliases like this:
$res[$aliasName] = $myResult;

=> In My Type Resolver :
//I have access to all my results with $root and I have just to access to them with a
return $root[$info->fieldNodes[0]->alias->value];

I did it this way because it was the quickest and easiest way to produce this feature, so don't hesitate if you have a better idea!

@zephyx
Copy link
Contributor

zephyx commented Nov 29, 2024

Ofc, this code is an example and is not enough, you have to manage the no alias case :)

@zephyx
Copy link
Contributor

zephyx commented Dec 5, 2024

Finally, I have modified the feature, and now it can be used with a method of ResolveInfo : getFieldSelectionWithAliases()

Basic example usage for a level 1 field:

  • In Query Resolver:
public function resolve($root, $args, $context, ResolveInfo $info)
{
    $fields = $info->getFieldSelectionWithAliases();
    
    if (isset($fields['myFieldWithAliases'])) {
        foreach ($fields['myFieldWithAliases'] as $aliasName => $selectionSet) {
            $category = $selectionSet['args']['category'];
            $limit = $selectionSet['args']['limit'];
            
            //build my SQL query with args
            $q = ....
            
            $res[$aliasName] = $q->all();
        }
    }

    return $res;
}
  • in Field resolver:
public static function resolveMyFieldWithAliasesField($root, array $args, $context, $info)
{
     if (isset($info->fieldNodes[0]->alias)) {
         return $root[$info->fieldNodes[0]->alias->value];
     }

     return $root['myFieldWithAliases'];
}

It has been merged with tag 15.19.0

@spawnia spawnia closed this as completed Dec 5, 2024
@spawnia spawnia removed the BC break label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants