티스토리 뷰
# Composer 설치
# composer 설치해준다. php5.6 이상에서 사용할 수 있음.
composer require webonyx/graphql-php
# Core/Controller.php
class Graphql_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function response($schema=null) {
try{
$input = json_decode(file_get_contents('php://input'), true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;
$result = GraphQL\GraphQL::executeQuery(
$schema,
$query,
$this->rootValue(),
$context = null,
$variableValues = null,
$operationName = null,
$fieldResolver = null,
$myValiationRules
);
$output = $result->jsonSerialize();
} catch (Exception $e) {
$output = [
'errors' => [
[
'message' => $e->getMessage(),
'code' => $e->getCode()
]
]
];
} finally {
$this
->output
->set_content_type('json')
->set_output(json_encode($output));
}
}
public function rootValue() {
return [];
}
}
# Controller.php
//ci config 설정안되었을 떄 autoload 추가.
//require_once 'vendor/autoload.php';
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Language\Parser;
use GraphQL\Utils\BuildSchema;
use GraphQL\Utils\AST;
class Index extends Graphql_Controller
{
public function __construct() {
parent::__construct();
}
public function index(){
//none
}
/*
* 스키마들을 추가해준다.
*/
public function rootValue() {
return [
'echo' => function($root, $args) {
if (isset($args['message'])) {
return $args['message'];
}
return "no message";
},
'hello' => function($root, $args) {
return "hello graphqlworld";
}
];
}
public function graphql() {
$document = Parser::parse(file_get_contents("schema.graphql"));
$schema = BuildSchema::build($document);
$this->response($schema);
}
}
# schema.graphql
schema {
query: Query,
mutation: Mutation,
}
type Mutation {
echo(message: String!): String,
...
}
type Query {
hello: String,
...
}
# request
Query {
mutation { echo(message: "This is Graphql") }
query { hello }
}
참고: https://banjubu.tistory.com/117
참고: https://github.com/webonyx/graphql-php
참고: https://webonyx.github.io/graphql-php/
참고 : https://graphql.org
'공부합시다 > php' 카테고리의 다른 글
라라벨 EloquantModel Multiple PrimaryKey 이슈 (0) | 2023.07.05 |
---|---|
PHP L5-Swagger 예시 (0) | 2023.06.15 |
array_count_values (0) | 2021.03.10 |
PHPRedis 설치하기 (0) | 2021.02.16 |
Array를 Object로 변경하기 (0) | 2020.05.20 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Python
- 테스트_다중트랜잭션
- laravel-test
- exception-test
- redis
- l5-swagger-response
- 메일
- 라라벨
- eloquent-observer
- POP3
- graphql-php
- addMonthWithoutOverflow
- aaa패턴
- addMonth
- l5-swagger
- php
- django
- MySQL
- graphql
- vim
- password-manager
- 자바스크립트
- laravel-kafka
- Laravel
- session+token authorize
- bitwarden-cli
- 정의
- php-laravel
- observer 매개변수 전달하기
- 정규식
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
글 보관함