This is a POC (proof of concept) to understand better the behavior of graphQL with Go Lang.
- Go (https://golang.org/);
- gqlgen - v0.13.0
go mod init github.com/bscpaz/graphql-go
go get github.com/99designs/gqlgen
go run github.com/99designs/gqlgen init
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.
This is a kind of service (or controller) implementation which you code queries, inserts, updates functions of your schemas files.
GraphQL is able to produce N+1 problem. Pay attention on this and take a look at 'Dataloaders' on gplgen's documentation.
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.
go run server.go
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
}
}
}
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