GraphQL Example
This project demonstrates how to set up a GraphQL server in a Go application using the Fiber framework and the graphql-go library.
Prerequisites
Ensure you have the following installed:
- Golang
- Fiber package
- graphql-go package
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.gitcd recipes/graphql -
Install dependencies:
go get
Running the Application
-
Start the application:
go run main.go -
The server listens on port
9090and exposes a single endpoint at/that accepts both GET and POST requests.
Usage
GET request
Pass the GraphQL query as a URL-encoded query parameter:
curl 'http://localhost:9090/?query=query%7Bhello%7D'
POST request
Send the query as a JSON body with Content-Type: application/json:
curl 'http://localhost:9090/' \
--header 'content-type: application/json' \
--data-raw '{"query":"query{hello}"}'
Both return a JSON response like:
{"data":{"hello":"world"}}