Skip to content

Commit

Permalink
Merge pull request #7 from mjarkk/relay-example
Browse files Browse the repository at this point in the history
examples: add Relay
  • Loading branch information
mjarkk authored Oct 19, 2021
2 parents 293d1ec + b8217d2 commit 3072be1
Show file tree
Hide file tree
Showing 29 changed files with 39,822 additions and 20 deletions.
26 changes: 26 additions & 0 deletions examples/relay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Relay example

# Backend

```sh
cd backend
go run .
```

# Frontend

```sh
cd frontend
npm install

# Generate the schema.graphql file
# Note backend needs to be running for this step
npm run get-schema

# Run the relay compiler
npm run relay

# Start the frontend
# Note to view the website the backend needs to be running
npm run start
```
1 change: 1 addition & 0 deletions examples/relay/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go-graphql-relay-example
10 changes: 10 additions & 0 deletions examples/relay/backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/mjarkk/go-graphql-relay-example

go 1.16

require (
github.com/gofiber/fiber/v2 v2.11.0
github.com/mjarkk/go-graphql v0.0.0-20210604095247-1a621d53c91c
)

replace github.com/mjarkk/go-graphql => ../../../
36 changes: 36 additions & 0 deletions examples/relay/backend/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E=
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v2 v2.11.0 h1:97PoVZI3JLlJyfMHFhKZoEHQEfTwOXvhQs2+YoLr9jk=
github.com/gofiber/fiber/v2 v2.11.0/go.mod h1:oZTLWqYnqpMMuF922SjGbsYZsdpE1MCfh416HNdweIM=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/klauspost/compress v1.12.2 h1:2KCfW3I9M7nSc5wOqXAlW2v2U6v+w6cbjvbfp+OykW8=
github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.26.0 h1:k5Tooi31zPG/g8yS6o2RffRO2C9B9Kah9SY8j/S7058=
github.com/valyala/fasthttp v1.26.0/go.mod h1:cmWIqlu99AO/RKcp1HWaViTqc57FswJOfYYdPJBl8BA=
github.com/valyala/fastjson v1.6.3 h1:tAKFnnwmeMGPbwJ7IwxcTPCNr3uIzoIj3/Fh90ra4xc=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41 changes: 41 additions & 0 deletions examples/relay/backend/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"log"
"mime/multipart"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/mjarkk/go-graphql"
)

func main() {
app := fiber.New()

app.Use(cors.New())

graphqlSchema := graphql.NewSchema()
err := graphqlSchema.Parse(QueryRoot{}, MethodRoot{}, nil)
if err != nil {
log.Fatal(err)
}

app.All("/graphql", func(c *fiber.Ctx) error {
res, _ := graphqlSchema.HandleRequest(
c.Method(),
func(key string) string { return c.Query(key) },
func(key string) (string, error) { return c.FormValue(key), nil },
func() []byte { return c.Body() },
string(c.Request().Header.ContentType()),
&graphql.RequestOptions{
GetFormFile: func(key string) (*multipart.FileHeader, error) { return c.FormFile(key) },
Tracing: true,
},
)

c.Response().Header.Set("Content-Type", "application/json")
return c.Send(res)
})

app.Listen(":5000")
}
113 changes: 113 additions & 0 deletions examples/relay/backend/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"fmt"

"github.com/mjarkk/go-graphql"
)

type QueryRoot struct{}

type MethodRoot struct{}

type Node interface {
ResolveId() (uint, graphql.AttrIsID)
}

type Todo struct {
ID uint `gq:"-"` // ignored because of (Todo).ResolveId()
Title string
Done bool
}

var _ = graphql.Implements((*Node)(nil), Todo{})

// ResolveId implements the Node interface
func (u Todo) ResolveId() (uint, graphql.AttrIsID) {
return u.ID, 0
}

var todoIdx = uint(3)

var todos = []Todo{
{ID: 1, Title: "Get groceries", Done: false},
{ID: 2, Title: "Make TODO app", Done: true},
}

func (QueryRoot) ResolveTodos() []Todo {
return todos
}

type GetTodoArgs struct {
ID uint `gq:"id,id"` // rename field to id and label field to have ID type
}

func (q QueryRoot) ResolveTodo(args GetTodoArgs) *Todo {
for _, todo := range todos {
if todo.ID == args.ID {
return &todo
}
}
return nil
}

type CreateTodoArgs struct {
Title string
}

func (m MethodRoot) ResolveCreateTodo(args CreateTodoArgs) Todo {
todo := Todo{
ID: todoIdx,
Title: fmt.Sprint(args.Title), // Copy title
Done: false,
}
todos = append(todos, todo)
todoIdx++
return todo
}

type UpdateTodoArgs struct {
ID uint `gq:"id,id"` // rename field to id and label field to have ID type
Title *string
Done *bool
}

func (m MethodRoot) ResolveUpdateTodo(args UpdateTodoArgs) (Todo, error) {
idx := -1
for i, todo := range todos {
if todo.ID == args.ID {
idx = i
break
}
}
if idx == -1 {
return Todo{}, fmt.Errorf("todo with id %d not found", args.ID)
}

todo := todos[idx]
if args.Title != nil {
todo.Title = *args.Title
}
if args.Done != nil {
todo.Done = *args.Done
}
todos[idx] = todo

return todo, nil
}

func (m MethodRoot) ResolveDeleteTodo(args GetTodoArgs) ([]Todo, error) {
idx := -1
for i, todo := range todos {
if todo.ID == args.ID {
idx = i
break
}
}
if idx == -1 {
return nil, fmt.Errorf("todo with id %d not found", args.ID)
}

todos = append(todos[:idx], todos[idx+1:]...)
return todos, nil
}
23 changes: 23 additions & 0 deletions examples/relay/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
70 changes: 70 additions & 0 deletions examples/relay/frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
Loading

0 comments on commit 3072be1

Please sign in to comment.