Skip to main content

GraphQL Example

Github StackBlitz

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:

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/graphql
  2. Install dependencies:

    go get

Running the Application

  1. Start the application:

    go run main.go
  2. The server listens on port 9090 and 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"}}

References