Skip to content

Commit

Permalink
feat(api): add GQL me query
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Mar 18, 2019
1 parent f80aad3 commit 6217318
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 62 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 // indirect
github.com/getsentry/raven-go v0.2.0
github.com/graphql-go/graphql v0.7.7
github.com/graphql-go/handler v0.2.3
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.0.0
github.com/pkg/errors v0.8.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JY
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/graphql-go/graphql v0.7.7 h1:nwEsJGwPq9N6cElOO+NYyoWuELAQZ4GuJks0Rlco5og=
github.com/graphql-go/graphql v0.7.7/go.mod h1:k6yrAYQaSP59DC5UVxbgxESlmVyojThKdORUqGDGmrI=
github.com/graphql-go/handler v0.2.3 h1:CANh8WPnl5M9uA25c2GBhPqJhE53Fg0Iue/fRNla71E=
github.com/graphql-go/handler v0.2.3/go.mod h1:leLF6RpV5uZMN1CdImAxuiayrYYhOk33bZciaUGaXeU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
228 changes: 228 additions & 0 deletions pkg/api/graphiql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package api

// Copied from https://github.com/graphql-go/handler/blob/master/graphiql.go
// Copyright (c) 2015 Hafiz Ismail

import (
"encoding/json"
"html/template"
"net/http"

"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
"github.com/ncarlier/reader/pkg/config"
"github.com/ncarlier/reader/pkg/schema"
)

// graphiqlData is the page data structure of the rendered GraphiQL page
type graphiqlData struct {
GraphiqlVersion string
QueryString string
VariablesString string
OperationName string
ResultString string
}

// graphiqlHandler is the handler for GraphiQL interface.
func graphiqlHandler(conf *config.Config) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

opts := handler.NewRequestOptions(r)
params := graphql.Params{
Schema: schema.Root,
RequestString: opts.Query,
VariableValues: opts.Variables,
OperationName: opts.OperationName,
Context: ctx,
}
renderGraphiQL(w, params)
})
}

// renderGraphiQL renders the GraphiQL GUI
func renderGraphiQL(w http.ResponseWriter, params graphql.Params) {
t := template.New("GraphiQL")
t, err := t.Parse(graphiqlTemplate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Create variables string
vars, err := json.MarshalIndent(params.VariableValues, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
varsString := string(vars)
if varsString == "null" {
varsString = ""
}

// Create result string
var resString string
if params.RequestString == "" {
resString = ""
} else {
result, err := json.MarshalIndent(graphql.Do(params), "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resString = string(result)
}

d := graphiqlData{
GraphiqlVersion: graphiqlVersion,
QueryString: params.RequestString,
ResultString: resString,
VariablesString: varsString,
OperationName: params.OperationName,
}
err = t.ExecuteTemplate(w, "index", d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

return
}

// graphiqlVersion is the current version of GraphiQL
const graphiqlVersion = "0.11.11"

// tmpl is the page template to render GraphiQL
const graphiqlTemplate = `
{{ define "index" }}
<!--
The request to this GraphQL server provided the header "Accept: text/html"
and as a result has been presented GraphiQL - an in-browser IDE for
exploring GraphQL.
If you wish to receive JSON, provide the header "Accept: application/json" or
add "&raw" to the end of the URL within a browser.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>GraphiQL</title>
<meta name="robots" content="noindex" />
<meta name="referrer" content="origin">
<style>
body {
height: 100%;
margin: 0;
overflow: hidden;
width: 100%;
}
#graphiql {
height: 100vh;
}
</style>
<link href="//cdn.jsdelivr.net/npm/graphiql@{{ .GraphiqlVersion }}/graphiql.css" rel="stylesheet" />
<script src="//cdn.jsdelivr.net/es6-promise/4.0.5/es6-promise.auto.min.js"></script>
<script src="//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js"></script>
<script src="//cdn.jsdelivr.net/react/15.4.2/react.min.js"></script>
<script src="//cdn.jsdelivr.net/react/15.4.2/react-dom.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/graphiql@{{ .GraphiqlVersion }}/graphiql.min.js"></script>
</head>
<body>
<div id="graphiql">Loading...</div>
<script>
// Collect the URL parameters
var parameters = {};
window.location.search.substr(1).split('&').forEach(function (entry) {
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] =
decodeURIComponent(entry.slice(eq + 1));
}
});
// Produce a Location query string from a parameter object.
function locationQuery(params) {
return '/graphql?' + Object.keys(params).filter(function (key) {
return Boolean(params[key]);
}).map(function (key) {
return encodeURIComponent(key) + '=' +
encodeURIComponent(params[key]);
}).join('&');
}
// Derive a fetch URL from the current URL, sans the GraphQL parameters.
var graphqlParamNames = {
query: true,
variables: true,
operationName: true
};
var otherParams = {};
for (var k in parameters) {
if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) {
otherParams[k] = parameters[k];
}
}
var fetchURL = locationQuery(otherParams);
// Defines a GraphQL fetcher using the fetch API.
function graphQLFetcher(graphQLParams) {
return fetch(fetchURL, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(graphQLParams),
credentials: 'include',
}).then(function (response) {
return response.text();
}).then(function (responseBody) {
try {
return JSON.parse(responseBody);
} catch (error) {
return responseBody;
}
});
}
// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
function onEditQuery(newQuery) {
parameters.query = newQuery;
updateURL();
}
function onEditVariables(newVariables) {
parameters.variables = newVariables;
updateURL();
}
function onEditOperationName(newOperationName) {
parameters.operationName = newOperationName;
updateURL();
}
function updateURL() {
history.replaceState(null, null, locationQuery(parameters));
}
// Render <GraphiQL /> into the body.
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: graphQLFetcher,
onEditQuery: onEditQuery,
onEditVariables: onEditVariables,
onEditOperationName: onEditOperationName,
query: {{ .QueryString }},
response: {{ .ResultString }},
variables: {{ .VariablesString }},
operationName: {{ .OperationName }},
}),
document.getElementById('graphiql')
);
</script>
</body>
</html>
{{ end }}
`
19 changes: 10 additions & 9 deletions pkg/api/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import (
"net/http"

"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
"github.com/ncarlier/reader/pkg/config"
"github.com/ncarlier/reader/pkg/schema"
"github.com/rs/zerolog/log"
)

// graphqlHandler is the handler for GraphQL requets.
func graphqlHandler(conf *config.Config) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

query := r.URL.Query().Get("query")
log.Debug().Str("query", query).Msg("GraphQL request")

result := graphql.Do(graphql.Params{
Schema: schema.Schema,
RequestString: query,
Context: ctx,
})
opts := handler.NewRequestOptions(r)
params := graphql.Params{
Schema: schema.Root,
RequestString: opts.Query,
VariableValues: opts.Variables,
OperationName: opts.OperationName,
Context: ctx,
}

result := graphql.Do(params)
if len(result.Errors) > 0 {
http.Error(w, result.Errors[0].Error(), http.StatusBadRequest)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewRouter(conf *config.Config) *http.ServeMux {

handler = route.HandlerFunc(conf)
if route.AuthNRequired {
handler = middleware.Auth(handler)
handler = middleware.MockAuth(handler)
}
handler = middleware.Method(handler, route.Methods)
handler = middleware.Cors(handler)
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ var routes = Routes{
true,
graphqlHandler,
},
Route{
[]string{"GET", "POST"},
"/graphiql",
false,
graphiqlHandler,
},
Route{
[]string{"GET"},
"/healtz",
Expand Down
34 changes: 34 additions & 0 deletions pkg/db/postgres/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,40 @@ func (pg *DB) CreateOrUpdateUser(user model.User) (*model.User, error) {
return pg.createUser(user)
}

// GetUserByID returns a user by its ID from DB
func (pg *DB) GetUserByID(id uint32) (*model.User, error) {
row := pg.db.QueryRow(`
SELECT
id,
username,
enabled,
last_login_at,
created_at,
updated_at
FROM users
WHERE id = $1`,
id,
)

result := model.User{}

err := row.Scan(
&result.ID,
&result.Username,
&result.Enabled,
&result.LastLoginAt,
&result.CreatedAt,
&result.UpdatedAt,
)

if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, err
}
return &result, nil
}

// GetUserByUsername returns user by its username from DB
func (pg *DB) GetUserByUsername(username string) (*model.User, error) {
row := pg.db.QueryRow(`
Expand Down
1 change: 1 addition & 0 deletions pkg/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "github.com/ncarlier/reader/pkg/model"

// UserRepository is the repository interface to manage Users
type UserRepository interface {
GetUserByID(id uint32) (*model.User, error)
GetUserByUsername(username string) (*model.User, error)
CreateOrUpdateUser(user model.User) (*model.User, error)
DeleteUser(user model.User) error
Expand Down
24 changes: 0 additions & 24 deletions pkg/middleware/auth.go

This file was deleted.

Loading

0 comments on commit 6217318

Please sign in to comment.