개요
이전에 Laravel, hasura, postgresql을 연동하여 세팅 하였다.
코드로 graphql schema를 관리하기 위해서 lighthouse와 playground를 추가적으로 설정해 보자.
구현
먼저 lighthouse를 설치한다.
lighthoust는 laravel 에서 QueryQL을 사용하기 위한 프레임워크이다.
$ composer require nuwave/lighthouse
이후, schema를 추출하기 위하여 아래와 같은 명령어를 실행한다.
$ php artisan vendor:publish --tag=lighthouse-schema
위 명령어를 실행한다면 graphql/schema.graphql
파일이 app
dir 하위에 들어오게 된다.
다음 컴포져를 이용하여 playground를 설치한다.
설치 이후에 /graphql-playground
로 접속하게 되면 playground 화면이 보이게 된다.
$ composer require mll-lab/laravel-graphql-playground
playground postgresql 연결
다음은, playground에 postgresql을 연결해야 한다.
현재 laravel과 postgre가 docker로 띄워져 있기 때문에 laravel 상에서 localhost로는 접근이 되질 않는다.
기존 env 에서는 localhost로 설정이 잡혀 있기 때문에, 연결 설정값을 변경 해 줘야 한다.
url 부분을 다음과 같이 바꿔주자.
'pgsql' => [
...
'url'=>'postgres://'.env('DB_USERNAME').':'.env('DB_PASSWORD').'@pgsql:'.env('DB_PORT').'/'.env('DB_DATABASE'),
...
],
schema.graphql 편집 및 확인
간단한 테스트를 위하여, schema.graphql
파일에 다음과 같은 코드 내용을 추가해 준다.
type Query {
posts: [Post!]! @all
post(id: ID @eq): Post @find
}
type Post {
id: ID!
title: String!
content: String!
created_at: DateTime!
updated_at: DateTime!
comments: [Comment!]!
}
type Comment {
id: ID!
post_id: Int!
content: String!
created_at: DateTime!
updated_at: DateTime!
}
이후 playground 에서 정상적으로 동작하는지 확인
참조 - https://hasura.io/blog/building-graphql-api-laravel-postgresql-hasura-remote-joins/
'Programming' 카테고리의 다른 글
Laravel + Hasura +Protgresql - Environment 관리 (0) | 2023.12.06 |
---|---|
Laravel + hasura + postgresql - 기본 설정 (0) | 2023.12.05 |
Oauth2.0 + API Server(Laravel Sanctum) + 통합인증 구현 (1) | 2023.08.30 |
[PHP Laravel] 대용량 트래픽 관련하여.. + Chat GPT (0) | 2023.04.02 |
[Next.js] localstorage is not defined (0) | 2023.03.25 |