我使用以下代碼在 PHP 中創建了 schema.graphql 檔案:
$data = SchemaPrinter::doPrint($schema);
file_put_contents('/var/cache/Graphql/schema.graphql', $data);
現在我想使用這個檔案內容創建新的模式。如何做到這一點?
uj5u.com熱心網友回復:
我已經使用 BuildSchema::build() 從 schema.graphql 創建了模式。
默認情況下,這樣的模式是在沒有任何決議器的情況下創建的。所以我們需要定義我們的自定義決議器,如下所示:
$contents = file_get_contents($this->projectDir.'/config/schema.graphql');
$typeConfigDecorator = function($typeConfig, $typeDefinitionNode) {
$name = $typeConfig['name'];
if ($name === 'Query') {
$typeConfig['resolveField'] =
function ($source, $args, $context, ResolveInfo $info) {
if ($info->fieldDefinition->name == 'login') {
if ($args['userName'] === 'test' && $args['password'] === '1234') {
return "Valid User.";
} else {
return "Invalid User";
}
} elseif ($info->fieldDefinition->name == 'validateUser') {
if ($args['age'] < 18) {
return ['userId' => $args['userId'], 'category' => 'Not eligible for voting'];
}
}
}
}
;
}
return $typeConfig;
};
$schema = BuildSchema::build($contents, $typeConfigDecorator);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507534.html
上一篇:使用api-platform獲取Symfony5中用戶角色的子資源
下一篇:更新物件陣列中的鍵值php
