recipes/graphql
GraphQL
Setting up a GraphQL server.
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.git cd recipes/graphqlInstall dependencies:
go get
Running the Application
Start the application:
go run main.goThe 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"}}