phpgraphqlgraphql-php

GraphQL PHP full query path


I'm trying to implement authorization for graphql-php in my project. The idea is to have a user access array, where I can set access for each user group to specific queries and mutations, and implementing a check-access method in graphql controller, before the query is executed.

One way would be to parse the request myself, but i was wondering if anyone knew how to access the full path to the query, as I've seen it is shown in https://webonyx.github.io/graphql-php/error-handling/

<?php
[
    'message' => 'My reported error',
    'category' => 'businessLogic',
    'locations' => [
    ['line' => 10, 'column' => 2]
],
    'path' => [
        'path',
        'to',
        'fieldWithException'
    ]
];

Solution

  • The path can be accessed in resolvers through the ResolveInfo object that every resolver function gets. The docs don't show it, but the fourth argument to every resolver is ResolveInfo (from the webonyx source $resolveFn($source, $args, $context, $info)). Simply get is:

    function ($root, $args, $context, $info) {
        $path = $resolveInfo->path;
        // your decisioning
    
    }
    

    And do you decisioning based on it.