2

我已经使用 PHP 和 mysql 的图形创建了一个基本的 api。当我使用邮递员进行火灾查询时。我是新的 grapql

{"query": "query { getPeople{name,pet{isDog,sound}} }" } 然后出现错误

在此处输入图像描述

但这个查询工作正常 {"query": "query { getPeople{name} }" } 在此处输入图像描述 任何人解释是什么问题?架构文件

  type Query {
      getPeople: [Person]
    }

    type Person {
      name: String
      pet: Pet
    }

    type Pet {
      isDog: Boolean
      sound: String
    }

我的终点代码

require_once FCPATH . 'vendor/autoload.php';
use GraphQL\Type\Definition\ObjectType;
use Siler\Graphql;
use GraphQL\GraphQL as WGraphql;
use Siler\Http\Request;
use Siler\Http\Response;
use Overblog\DataLoader\DataLoader;
use Overblog\DataLoader\Promise\Adapter\Webonyx\GraphQL\SyncPromiseAdapter;
use Overblog\PromiseAdapter\Adapter\WebonyxGraphQLSyncPromiseAdapter;
use GraphQL\Utils\BuildSchema;

Response\header('Access-Control-Allow-Origin', '*');
        Response\header('Access-Control-Allow-Headers', 'content-type');

        $MyDB = new mysqli("localhost", "root", "Oneclick1@", "test");

        if ($MyDB->connect_errno) {
            error_log("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
        }

        function sql($query) {
            $MyDB = new mysqli("localhost", "root", "Oneclick1@", "test");
            //echo $query;
            //print_r([$MyDB, $query]);exit;
            $result = mysqli_query($MyDB, $query);
            $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);  

            return $rows;
        }

        $graphQLSyncPromiseAdapter = new SyncPromiseAdapter();
        $promiseAdapter = new WebonyxGraphQLSyncPromiseAdapter($graphQLSyncPromiseAdapter);

        $petLoader = new DataLoader(function ($keys) use ($promiseAdapter ) {

            $ids = join(',', $keys);
            $idMap = array_flip($keys);
            $rows = sql("SELECT owner, isDog, sound FROM pets WHERE owner in ({$ids});");
            // echo "<pre>";
            // print_r($rows);
            // exit;
            foreach ($rows as $r) {
                $idMap[$r['owner']] = $r;
            }
            //print_r($promiseAdapter->createAll(array_values($idMap)));exit;
            return $promiseAdapter->createAll(array_values($idMap));
        }, $promiseAdapter);
         //print_r($promiseAdapter);exit;
        WGraphQL::setPromiseAdapter($graphQLSyncPromiseAdapter);

        $context = [
            'petLoader' => $petLoader,
            'sql' => function ($query) {
                return sql($query);
            }
        ];

        if (Request\method_is('post')) {

            $schema = include  FCPATH . '/php-graphql-example-master/schema.php';;

            Graphql\init($schema, null, $context);
        }

解析器.php

使用 Overblog\DataLoader\DataLoader;

return [
    'Person' => [
        'pet' =>  function($root, $args, $context) {
           // print_r($context);exit;
            // echo "33";
            // print_r($root['id']);exit;
            return $context['petLoader']->load($root['id']);
        },
    ],
    'Query' => [
        'getPeople' => function($root, $args, $context) {
            return $context['sql']("SELECT name, id FROM people");
        }
    ]
];

架构.php

use Siler\Graphql;

$typeDefs = file_get_contents(__DIR__.'/schema.graphql');
$resolvers = include __DIR__.'/resolvers.php';

return Graphql\schema($typeDefs, $resolvers);
4

0 回答 0