通过id获取结果的graphql php
我是graphql的新手,我从未使用过rest或其他类似的语言 我现在可以用我的等级得到固定值了 我有一个像下面这样的模式文件通过id获取结果的graphql php,php,graphql,graphql-php,Php,Graphql,Graphql Php,我是graphql的新手,我从未使用过rest或其他类似的语言 我现在可以用我的等级得到固定值了 我有一个像下面这样的模式文件 demo.schema schema { query: Query } type Query { id(id: Int): sub } type sub{ name: String age: Int } 下面是demo.php: declare(strict_types=1); require_once 'vendor/autoload.p
demo.schema
schema {
query: Query
}
type Query {
id(id: Int): sub
}
type sub{
name: String
age: Int
}
下面是demo.php:
declare(strict_types=1);
require_once 'vendor/autoload.php';
$typedef = file_get_contents('demo.graphql');
$rootValue = include 'rootvalue.php';
use \GraphQL\GraphQL;
use \GraphQL\Utils\BuildSchema;
// build schema and get query string
$schema = BuildSchema::build($typedef);
$param = file_get_contents("php://input");
$param = json_decode($param, true);
$param = $param['query'];
// get result and print it
$r = GraphQL::executeQuery($schema, $param, $rootValue)->toArray();
print_r(json_encode($r));
下面是rootvalue.php
interface Root
{
public function reslove($root, $args, $context);
}
class age implements Root
{
public function reslove($root, $args, $context)
{
return 30;
}
}
class name implements Root
{
public function reslove($root, $args, $context)
{
return 'Miami';
}
}
return [
'id' => function($root, $args){
return $root;
},
'age' => function($root, $args, $context) {
$new = new age();
return $new->reslove($root, $args, $context);
},
'name' => function($root, $args, $context) {
$new = new name();
return $new->reslove($root, $args, $context);
},
];
现在,我创建了一个名为user
的mysql db表,它有id
,name
,age
字段
我怎样才能得到像user.id=1这样的结果
现在,我可以返回值,比如'30','Miami'
但现在,我需要通过数据库获取数据,所以这将是一个类似于客户端浏览器的查询
query{
id(id: 0){
name
age
}
}
我知道在rootvalue.php
'id'=>function($root, $args){
$args['id'] // it will be get a value '1'
return $root;
},
但这是我能做的极限。我在别的地方找不到身份证怎么办
附言:
我知道如何从数据库中获取数据,只是不知道何时获取数据库,以及如何将这些数据传输到子对象