GraphQL
The lighthouse package turns a schema with directives into a GraphQL server: @all,
@paginate and @find read the demo's tables, @belongsTo follows a
relation, and one field is a Go resolver. The endpoint is /graphql (route group
none, so no session and no CSRF check). Each example below was sent to it as a POST with a
JSON body when this page rendered.
lighthouse
A page of posts
Query
{
postsPage(first: 3, orderBy: [{column: VIEWS, order: DESC}]) {
paginatorInfo { total currentPage lastPage }
data { id title views }
}
}
curl
curl -s -X POST https://godemo.ozkanozcan.com/graphql \
-H 'Content-Type: application/json' \
-d '{"query":"{\n postsPage(first: 3, orderBy: [{column: VIEWS, order: DESC}]) {\n paginatorInfo { total currentPage lastPage }\n data { id title views }\n }\n}"}'
Result
{
"data": {
"postsPage": {
"paginatorInfo": {
"total": 24,
"currentPage": 1,
"lastPage": 8
},
"data": [
{
"id": "11",
"title": "Caching Query Results",
"views": 481
},
{
"id": "24",
"title": "Son Yazı",
"views": 462
},
{
"id": "10",
"title": "Öğle Yemeği Önerileri",
"views": 444
}
]
}
}
}
One post and its category
Query
{
post(slug: "hello-from-laravel-go") {
title
locale
excerpt
category { name slug }
}
}
curl
curl -s -X POST https://godemo.ozkanozcan.com/graphql \
-H 'Content-Type: application/json' \
-d '{"query":"{\n post(slug: \"hello-from-laravel-go\") {\n title\n locale\n excerpt\n category { name slug }\n }\n}"}'
Result
{
"data": {
"post": {
"title": "Hello from Laravel Go",
"locale": "en",
"excerpt": "Hello from Laravel Go. A demo post of the OzSuite showcase. ...",
"category": {
"name": "Go",
"slug": "go"
}
}
}
}
Variables and a filter
Query
query TurkishPosts($locale: String) {
posts(locale: $locale, orderBy: [{column: ID, order: ASC}]) {
id
title
}
}
Variables
locale: tr
curl
curl -s -X POST https://godemo.ozkanozcan.com/graphql \
-H 'Content-Type: application/json' \
-d '{"query":"query TurkishPosts($locale: String) {\n posts(locale: $locale, orderBy: [{column: ID, order: ASC}]) {\n id\n title\n }\n}","variables":{"locale":"tr"}}'
Result
{
"data": {
"posts": [
{
"id": "2",
"title": "Çalışma Şekli"
},
{
"id": "4",
"title": "İstanbul'da Bir Gün"
},
{
"id": "6",
"title": "Ağaç Yapıları ve İç İçe Kümeler"
},
{
"id": "8",
"title": "Güzel Bir Sabah"
},
{
"id": "10",
"title": "Öğle Yemeği Önerileri"
},
{
"id": "12",
"title": "Kapadokya Gezisi"
},
{
"id": "14",
"title": "Şehirler ve İlçeler"
},
{
"id": "16",
"title": "Haftalık Notlar"
},
{
"id": "18",
"title": "Kış Tatili"
},
{
"id": "20",
"title": "Yeni Yıl Planları"
},
{
"id": "22",
"title": "Taslak Yazı"
},
{
"id": "24",
"title": "Son Yazı"
}
]
}
}
Users and a count
Query
{
postCount
users { id name email }
}
curl
curl -s -X POST https://godemo.ozkanozcan.com/graphql \
-H 'Content-Type: application/json' \
-d '{"query":"{\n postCount\n users { id name email }\n}"}'
Result
{
"data": {
"postCount": 24,
"users": [
{
"id": "1",
"name": "Ada Admin",
"email": "admin@example.com"
},
{
"id": "2",
"name": "Emre Editor",
"email": "editor@example.com"
},
{
"id": "3",
"name": "Rita Reader",
"email": "reader@example.com"
}
]
}
}
The schema (embedded in the binary)
# Posts, categories and users of the demo, served by the lighthouse package.
"A blog post."
type Post {
id: ID!
title: String!
slug: String!
locale: String!
status: String!
views: Int!
published_at: String
"The category the post belongs to."
category: Category @belongsTo
"The first 60 characters of the body, computed by a Go resolver."
excerpt: String! @field(resolver: "PostExcerpt")
}
"A blog category."
type Category {
id: ID!
name: String!
slug: String!
"Posts of this category."
posts: [Post!]! @hasMany
}
"A user of the demo. The password is not part of the schema."
type User {
id: ID!
name: String!
email: String!
}
type Query {
"Every post, optionally of one locale or status."
posts(locale: String @eq, status: String @eq, orderBy: _ @orderBy(columns: ["id", "views", "title"])): [Post!]! @all
"A page of posts."
postsPage(status: String @eq, orderBy: _ @orderBy(columns: ["id", "views", "title"])): [Post!]!
@paginate(defaultCount: 5, maxCount: 20)
"One post by id or slug."
post(
id: ID @eq @rules(apply: ["prohibits:slug", "required_without:slug"])
slug: String @eq @rules(apply: ["prohibits:id", "required_without:id"])
): Post @find
"Every category."
categories: [Category!]! @all
"The seeded users."
users: [User!]! @all
"How many posts exist."
postCount: Int! @count(model: "Post")
}