Queries
A bit of GraphQL
Alambic is fully compliant with the facebook/graphql specification. A GraphQL query is a string interpreted by a server that returns data in a specified format.
Here is an example query we can make against our schema:
Give me the field “name” from the user with id “1”
{
user(id: "1") {
name
}
}
The server should respond with:
{
"data": {
"user": {
"name": "Luke"
}
}
}
Another more complex query:
Give me the list of users names along with their posts
{
users {
name
posts {
text
}
}
}
The server should respond with:
{
"data": {
"users": [
{
"name": "Luke",
"posts": [
{
"text": "May the force be with you"
}
]
},
{
"name": "Dark Vador",
"posts": [
{
"text": "I am your father"
}
]
}
]
}
}
Create an HTTP endpoint
We’re almost finished. In order to query your Alambic server we now need to create an endpoint.
Alambic is “transport-agnostic”, meaning that we could grab data over HTTP like normal or request data via a non-HTTP wire protocol. For this example, we will simply expose our Alambic server over a single HTTP endpoint with a dead simple php script.
Create a alambic.php file at your website’s root directory
Congratulations! You’ve set up your first GraphQL Alambic server. You can now start sending requests to your server:
- http://localhost/alambic.php?query={user(id:”1”){name}}
- http://localhost/alambic.php?query={users{name posts {text}}}
There’s plenty of resources to explore from now:
- datasources to use real databases (MySQL, MongoDB, …) instead of json files
- middlewares if you want to add application logic and cool features like caching, versionning,… and so more