PHP code example of wappcode / gql-pdss-surveys

1. Go to this page and download the library: Download wappcode/gql-pdss-surveys library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wappcode / gql-pdss-surveys example snippets




return [
        "driver" => [
                "user" => "root",
                "password" => "dbpassword",
                "dbname" => "procesot_survey",
                "driver" => "pdo_mysql",
                "host" => "127.0.0.1",
                "charset" => "utf8mb4"
        ],
        "entities" => [
                "GPDSurvey\\Entities" => __DIR__ . "/../vendor/wappcode/gql-pdss-surveys/GPDSurvey/src/Entities",
        ]
];



use AppModule\AppModule;
use GPDCore\Core\AppConfig;
use GPDCore\Core\Application;
use GPDCore\Factory\EntityManagerFactory;
use GPDSurvey\GPDSurveyModule;
use GraphqlModule\GraphqlModule;

// ... bootstrap

$app
        ->addModule(new GraphqlModule(route: '/api'))
        ->addModule(GPDSurveyModule::class)
        ->addModule(AppModule::class);



namespace AppModule;

use GPDSurvey\GPDSurveyModule;
use GPDCore\Graphql\ResolverPipelineFactory;
use GPDCore\Graphql\ResolverTransactionMiddlewareFactory;

final class SecureSurveyModule extends GPDSurveyModule
{
        public function getResolvers(): array
        {
                $resolvers = parent::getResolvers();

                $authProxy = function (callable $resolver) {
                        return function ($root, array $args, $context, $info) use ($resolver) {
                                if (!$context->getAuthUser()) {
                                        throw new \RuntimeException('Unauthorized');
                                }
                                return $resolver($root, $args, $context, $info);
                        };
                };

                $logProxy = function (callable $resolver) {
                        return function ($root, array $args, $context, $info) use ($resolver) {
                                $start = microtime(true);
                                $result = $resolver($root, $args, $context, $info);
                                $duration = microtime(true) - $start;
                                error_log("{$info->parentType->name}::{$info->fieldName} {$duration}s");
                                return $result;
                        };
                };

                $protectedKeys = [
                        'Mutation::createSurvey',
                        'Mutation::updateSurvey',
                        'Mutation::deleteSurvey',
                        'Mutation::buildSurvey',
                ];

                foreach ($protectedKeys as $key) {
                        $base = $resolvers[$key] ?? null;
                        if (!is_callable($base)) {
                                continue;
                        }

                        $resolvers[$key] = ResolverPipelineFactory::createPipeline($base, [
                                ResolverPipelineFactory::createWrapper($logProxy),
                                ResolverPipelineFactory::createWrapper($authProxy),
                                ResolverTransactionMiddlewareFactory::createMiddleware(),
                        ]);
                }

                return $resolvers;
        }
}



namespace AppModule\Middleware;

use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class AuthMiddleware implements MiddlewareInterface
{
        public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): \Psr\Http\Message\ResponseInterface
        {
                $token = $request->getHeaderLine('Authorization');
                if ($token === '') {
                        return new JsonResponse(['error' => 'Unauthorized'], 401);
                }

                return $handler->handle($request);
        }
}



namespace AppModule;

use GPDSurvey\GPDSurveyModule;
use AppModule\Middleware\AuthMiddleware;

final class SecureSurveyModule extends GPDSurveyModule
{
        public function getMiddlewares(): array
        {
                return [
                        AuthMiddleware::class,
                ];
        }
}
graphql
query GetSurveys($input: ConnectionInput) {
    getSurveys(input: $input) {
        totalCount
        edges {
            node {
                id
                title
                active
            }
        }
        pageInfo {
            hasNextPage
            endCursor
        }
    }
}
graphql
mutation CreateSurvey($input: SurveyInput!) {
    createSurvey(input: $input) {
        id
        title
        active
    }
}
graphql
mutation BuildSurvey($input: BuildSurveyInput!) {
    buildSurvey(input: $input) {
        id
        title
        sections {
            id
            title
            items {
                id
                type
            }
        }
    }
}
graphql
mutation BuildSurvey($input: BuildSurveyInput!) {
    buildSurvey(input: $input) {
        id
        title
        active
    }
}
graphql
mutation BuildSurveyTargetAudience($input: BuildSurveyTargetAudienceInput!) {
    buildSurveyTargetAudience(input: $input) {
        id
        title
        attempts
    }
}
graphql
mutation BuildSurveySection($input: BuildSurveySectionInput!) {
    buildSurveySection(input: $input) {
        id
        title
        order
    }
}
graphql
mutation BuildSurveyQuestion($input: BuildSurveyQuestionInput!) {
    buildSurveyQuestion(input: $input) {
        id
        title
        type
    }
}
graphql
mutation BuildSurveySectionItem($input: BuildSurveySectionItemInput!) {
    buildSurveySectionItem(input: $input) {
        id
        type
        order
    }
}
graphql
mutation CreateSurveyAnswerSession($input: SurveyAnswerSessionInput) {
    createSurveyAnswerSession(input: $input) {
        id
        completed
        score
        scorePercent
    }
}
graphql
mutation UpdateSurveyAnswerSession($id: ID!, $input: SurveyAnswerSessionPartialInput) {
    updateSurveyAnswerSession(id: $id, input: $input) {
        id
        completed
        score
        scorePercent
    }
}