bscpaz / graphql-go

This is a POC (proof of concept) to understand better the behavior of graphQL with Go Lang.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql-go

This is a POC (proof of concept) to understand better the behavior of graphQL with Go Lang.

Technologies:

How to get stated

Initialize Go module and dependecies

go mod init github.com/bscpaz/graphql-go
go get github.com/99designs/gqlgen

Generete your own models (*.graphql files) into 'graph' folder and then...

go run github.com/99designs/gqlgen init

Concepts of graphQL

File 'resolver.go'

It serves as dependency injection for your app. A kind of a datasource or a repositories provider of your application. In this POC, we just use arrays.

File 'schema.resolvers.go'

This is a kind of service (or controller) implementation which you code queries, inserts, updates functions of your schemas files.

The problem of N+1 in graphQL

GraphQL is able to produce N+1 problem. Pay attention on this and take a look at 'Dataloaders' on gplgen's documentation.

Generating relationships

After splitting models in their own files and adding those models in gqlgen.yml (tag 'models'):

gqlgen generate

This will add more methods in schema.resolvers.go file in which you provide the father and the code is supposed to return his children.

Running the graphQL

Start the gqlgen server

go run server.go

Exemple of inputs

query findCategories {
  categories {
    id
    name
    description
    courses {
      name
    }
  }  
}
mutation createCategory {
  createCategory(input: {
    name:"Programming language",
    description: "Programming languages are one kind of computer language"
  }) {
    id
    name
    description
  }
}
query findCourses {
  courses {
    id
    name
    category {
      name
    }
    chapters {
      name
    }
  }
}
mutation createCourse {
  createCourse(input: {
    name: "Java"
    description: "Bruno's Java course"
    categoryId: 5532800
  }) {
    id
    name
    description
  }
}
query findChapters {
  chapters {
    id
    name
    course {
      name
    }
  }
}
mutation createChapter1 {
  createChapter(input: {
    name: "Primitive types",
    courseId: 5532800
  }) {
    id
    name
    course {
      name
    }
  }
}

mutation createChapter2 {
  createChapter(input: {
    name: "Arrays and Lists",
    courseId: 5532800
  }) {
    id
    name
    course {
      name
    }
  }
}

Known issues

After running "go run github.com/99designs/gqlgen init"...
issue:
  missing go.sum entry for module providing package
solution:
  go mod tidy
  go get github.com/99designs/gqlgen
After running "go run github.com/99designs/gqlgen init"...
issue:
  graph/schema.graphqls already exists
  exit status 1
solution:
  go mod tidy
  go get github.com/99designs/gqlgen@v0.13.0
  go run github.com/99designs/gqlgen init
After running "go run github.com/99designs/gqlgen init"...
issue:
  validation failed: packages.Load: /usr/local/go/src/net/cgo_linux.go:13:8: could not import C (no metadata for C)
solution:
  sudo apt-get install gcc

About

This is a POC (proof of concept) to understand better the behavior of graphQL with Go Lang.


Languages

Language:Go 100.0%