我是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.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);
},
];
现在 la mysql db 表名为user
, 它有id
, name
,age
字段
我怎样才能得到这样的结果user.id=1
现在,我可以只返回像“30”、“迈阿密”这样的值。
但是现在,我需要通过数据库来获取数据,所以它将是来自客户端浏览器的这样的查询
query{
id(id: 0){
name
age
}
}
我知道 l cat 得到 arg 的 id 值rootvalue.php
'id'=>function($root, $args){
$args['id'] // it will be get a value '1'
return $root;
},
但这是我能做的极限。我在其他地方拿不到身份证,怎么办?
PS:我知道如何从数据库中获取数据,只是不知道获取数据库的时间,以及如何将这些数据传输到子对象