-
Notifications
You must be signed in to change notification settings - Fork 27
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
Add a more complex example #24
base: master
Are you sure you want to change the base?
Conversation
With this last commit 7465fba, we need this PR merged: graphql-go/handler#33 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a couple of things that I'd like to see improved but overall, everything looks very good. Once the "problems" (most of them are of cosmetic nature) are addressed, I see nothing that blocks the PR from getting merged.
examples/server/main.go
Outdated
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Document struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a local type (to the file), it should be document
, otherwise golint complains:
➜ graphqlws git:(kivutar/master) ✗ golint examples/server/
examples/server/main.go:13:6: exported type Document should have comment or be unexported
examples/server/main.go
Outdated
"document": &graphql.Field{ | ||
Type: documentType, | ||
Args: graphql.FieldConfigArgument{ | ||
"docId": &graphql.ArgumentConfig{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very much a matter of taste but I'd use id
instead of docId
everywhere.
examples/server/main.go
Outdated
}, | ||
}, | ||
Resolve: func(p graphql.ResolveParams) (interface{}, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove empty lines at the beginning of code blocks. Like this one.
examples/server/main.go
Outdated
|
||
for _, subscriptions := range subscriptionManager.Subscriptions() { | ||
for _, subscription := range subscriptions { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also remove this one.
examples/server/main.go
Outdated
Type: graphql.String, | ||
}, | ||
"content": &graphql.ArgumentConfig{ | ||
Type: graphql.String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd wrap this and the docId
and title
types with graphql.NewNonNull()
so that they are required to always be present in the mutation. That way you don't have to check if the type assertions in lines 83-85 are ok.
examples/server/main.go
Outdated
}, | ||
Resolve: func(p graphql.ResolveParams) (interface{}, error) { | ||
|
||
docID := p.Args["docId"].(int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't wrap the argument types in the definitions, you'd have to check whether the type assertions are successful like so
docID, docIDOk := p.Args["docId"].(int)
if !docIDOK {
... return an error or something ...
}
} | ||
|
||
var err error | ||
schema, err = graphql.NewSchema(schemaConfig) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
schema
is only needed in main()
, so I'd remove the var schema graphql.Schema
further towards the top and just do the following here:
schema, err := graphql.NewSchema(schemaConfig)
That way you also don't have to declare err
as a var
before using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
schema
is also used in the mutation resolver
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right, sorry. Ignore this comment then. :)
examples/server/main.go
Outdated
for _, subscription := range subscriptions { | ||
|
||
// JSON interface is float64 | ||
var subdocID int = int(subscription.Variables["docId"].(float64)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is >90 characters. That's the (undocumented) limit we've been sticking to across this repo. Could you break this line into two?
examples/server/main.go
Outdated
|
||
data := graphqlws.DataMessagePayload{ | ||
Data: result.Data, | ||
Errors: graphqlws.ErrorsFromGraphQLErrors(result.Errors), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is also >90 characters but this could easily be solved:
Errors: graphqlws.ErrorsFromGraphQLErrors(
result.Errors,
),
examples/server/main.go
Outdated
), | ||
} | ||
|
||
sub.SendData(sub, &data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a breaking change in master, removing the unnecesary *Subscription
parameter from the SendData
function. So now it's just
sub.SendData(&data)
Pretty: true, | ||
GraphiQL: true, | ||
Endpoint: "http://localhost:8085", | ||
SubscriptionsEndpoint: "ws://localhost:8085/subscriptions", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In github.com/graphql-go/handler
, where handler.Config
is defined, I don't think Endpoint
or SubscriptionsEndpoint
are properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will also need this PR https://github.com/graphql-go/handler/pull/33/files
This example is closer to a real life use of the library. It shows how to send data to subscribers as result of a mutation, as it is often the case in graphql. It also shows how to subscribe to a topic and broadcast only to the subscribers of this topic.