Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Playground #1575

Merged
merged 17 commits into from
Jul 19, 2023
Merged

feat: Playground #1575

merged 17 commits into from
Jul 19, 2023

Conversation

nasdf
Copy link
Member

@nasdf nasdf commented Jun 13, 2023

Relevant issue(s)

Resolves #884

Description

This PR adds a web based playground to DefraDB.

To use the embedded playground run the following.

# build playground
make deps:playground

# enable embedded playground
export BUILD_TAGS=playground

make start
# or 
make build

The playground is served from the same port as the API: http://localhost:9181

  • View & query DefraDB types
  • Create & patch schemas
  • Relational field support
  • Embed playground in binary

localhost_5173_

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

Testing was done manually.

Specify the platform(s) on which this was tested:

  • MacOS

@codecov
Copy link

codecov bot commented Jul 14, 2023

Codecov Report

Patch coverage: 93.62% and project coverage change: -0.02 ⚠️

Comparison is base (49bced3) 74.92% compared to head (fccf857) 74.90%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1575      +/-   ##
===========================================
- Coverage    74.92%   74.90%   -0.02%     
===========================================
  Files          203      203              
  Lines        21072    21072              
===========================================
- Hits         15787    15782       -5     
- Misses        4207     4210       +3     
- Partials      1078     1080       +2     
Flag Coverage Δ
all-tests 74.90% <93.62%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cli/schema_list.go 14.29% <0.00%> (-0.42%) ⬇️
api/http/handler.go 100.00% <100.00%> (ø)
api/http/handlerfuncs.go 70.77% <100.00%> (-0.08%) ⬇️
api/http/router.go 100.00% <100.00%> (ø)
client/descriptions.go 73.68% <100.00%> (+0.71%) ⬆️

... and 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 49bced3...fccf857. Read the comment docs.

@nasdf nasdf marked this pull request as ready for review July 17, 2023 17:31
@nasdf nasdf requested a review from a team July 17, 2023 17:32
Comment on lines 39 to 52
h.Mux = chi.NewRouter()
var router = chi.NewRouter()

// setup CORS
if len(h.options.allowedOrigins) != 0 {
h.Use(cors.Handler(cors.Options{
AllowedOrigins: h.options.allowedOrigins,
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"Content-Type"},
MaxAge: 300,
}))
}

// setup logger middleware
h.Use(loggerMiddleware)

// define routes
h.Get(RootPath, h.handle(rootHandler))
h.Get(PingPath, h.handle(pingHandler))
h.Get(DumpPath, h.handle(dumpHandler))
h.Get(BlocksPath+"/{cid}", h.handle(getBlockHandler))
h.Get(GraphQLPath, h.handle(execGQLHandler))
h.Post(GraphQLPath, h.handle(execGQLHandler))
h.Get(SchemaPath, h.handle(listSchemaHandler))
h.Post(SchemaPath, h.handle(loadSchemaHandler))
h.Patch(SchemaPath, h.handle(patchSchemaHandler))
h.Post(IndexPath, h.handle(createIndexHandler))
h.Delete(IndexPath, h.handle(dropIndexHandler))
h.Get(IndexPath, h.handle(listIndexHandler))
h.Get(PeerIDPath, h.handle(peerIDHandler))

return h
func init() {
router.Get(RootPath, rootHandler)
router.Get(PingPath, pingHandler)
router.Get(DumpPath, dumpHandler)
router.Get(BlocksPath+"/{cid}", getBlockHandler)
router.Get(GraphQLPath, execGQLHandler)
router.Post(GraphQLPath, execGQLHandler)
router.Get(SchemaPath, listSchemaHandler)
router.Post(SchemaPath, loadSchemaHandler)
router.Patch(SchemaPath, patchSchemaHandler)
router.Post(IndexPath, createIndexHandler)
router.Delete(IndexPath, dropIndexHandler)
router.Get(IndexPath, listIndexHandler)
router.Get(PeerIDPath, peerIDHandler)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: I'm not a big fan of a global var router and setting the routes on package init. I think it would be better for future maintenance (and other beneficial reasons) to keep this under the setRoutes function. Although you can simply return the router.

That being said, I like how you moved the handle method to a middleware and that it avoids wrapping the handlers in the handle method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

playground.go:init can also be moved inside the setRoutes function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to keep the setRoutes method but I couldn't think of another way to do build time route registration. I'm using the playground.go:init function because the playground can be enabled/disabled using the build tag. I'm happy to move everything back if we can think of an alternative that works with build tags.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could create a global withPlayground variable in router.go and set in to true in the init inside playground.go. You can then have a if block in the setRoutes function to act accordingly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! My brain was stuck on that for a while. I think I came up with a good solution. The route registration is moved back to setRoutes and the playground handler is set at build time via init. I made sure it works when not including the playground as well.

@@ -0,0 +1,18 @@
//go:build playground
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Should add a copyright header (I am also wondering why the linter didn't complain about the copyright header)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe due to the build tag here? I'm not exactly sure either.

playground/playground.go Show resolved Hide resolved
playground/src/App.tsx Show resolved Hide resolved
playground/src/components/Plugin.tsx Show resolved Hide resolved
playground/src/components/SchemaLoadForm.tsx Show resolved Hide resolved
playground/src/components/SchemaPatchForm.tsx Show resolved Hide resolved
playground/src/index.css Show resolved Hide resolved
@fredcarle fredcarle added feature New feature or request area/api Related to the external API component labels Jul 18, 2023
@fredcarle fredcarle added this to the DefraDB v0.6 milestone Jul 18, 2023
@nasdf nasdf requested a review from a team July 19, 2023 15:50
Copy link
Collaborator

@fredcarle fredcarle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job with this Keenan! I actually played around with the playground this morning and it's working quite well. Specially for a first iteration of it.

@nasdf nasdf merged commit b95c931 into sourcenetwork:develop Jul 19, 2023
10 checks passed
@nasdf nasdf deleted the feature/playground branch July 19, 2023 16:17
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
## Relevant issue(s)

Resolves sourcenetwork#884 

## Description

This PR adds a web based playground to DefraDB.

To use the embedded playground run the following.

```
# build playground
make deps:playground

# enable embedded playground
export BUILD_TAGS=playground

make start
# or 
make build
```

The playground is served from the same port as the API:
`http://localhost:9181`

- [x] View & query DefraDB types
- [x] Create & patch schemas
- [x] Relational field support
- [x] Embed playground in binary


![localhost_5173_](https://github.com/sourcenetwork/defradb/assets/1862560/8ad8bd53-eadf-40bc-9204-6c821b0b13d2)


## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

Testing was done manually.

Specify the platform(s) on which this was tested:
- MacOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api Related to the external API component feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bundled graphql playground in defra binary.
3 participants