Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 01:50
    frederikhors commented #1867
  • 01:49
    frederikhors labeled #1868
  • 01:48
    frederikhors commented #1868
  • Jan 28 23:20
    mohammadcazi opened #1868
  • Jan 28 23:02
    zoevkay opened #1867
  • Jan 28 17:59
    gf3 commented #1191
  • Jan 28 13:55
    dtrckd commented #1144
  • Jan 28 13:50
    dtrckd commented #1144
  • Jan 28 13:50
    dtrckd commented #1144
  • Jan 28 13:42
    nullism commented #1144
  • Jan 28 13:42
    nullism commented #1144
  • Jan 28 11:18
    frederikhors commented #1191
  • Jan 28 10:43
    frederikhors commented #1865
  • Jan 28 01:50
    theprobugmaker edited #1865
  • Jan 28 01:49
    theprobugmaker edited #1865
  • Jan 28 01:49
    theprobugmaker edited #1865
  • Jan 28 01:48
    theprobugmaker edited #1865
  • Jan 28 01:48
    theprobugmaker edited #1865
  • Jan 28 01:48
    theprobugmaker edited #1865
  • Jan 28 01:48
    theprobugmaker opened #1865
Jon Lundy
@JonLundy
because id is more of a global identifier and the other is specific to the object
Cole Lawrence
@colelawrence

Is there a way to hide a field from the API user? I'm trying to achieve sort of transient data passing without needing to fully reimplement every gql model I need this for. In an optimal world, I might have something like this (see @internal):

// schema.graphql
type User {
    "id is..."
    id ID!
    "dbUser is..."
    dbUser DBUser @internal
}

scalar DBUser @goModel(model: "github.com/my/app/models.User")
// gql_models/generated.go
type User struct {
    // id is...
    id     string      `json:"id"`
    // dbUser is...
    dbUser models.User `json:"-"`
}

// resolvers/user_resolver.go
func (p userResolver) Friends(ctx context.Context, user *gql_models.User) ([]gql_models.User, error) {
    // use internal details of dbUser to resolve
    user.dbUser
    ...
}

The biggest win from this approach is that there is only one source of truth for my gql models (defined in the schema). AND we can spend time writing comprehensive documentation only once (in the schema file)

Cole Lawrence
@colelawrence
This "modelgen-hook" documentation seems relevant https://gqlgen.com/recipes/modelgen-hook/
Cole Lawrence
@colelawrence
I suppose I could do a similar approach to permission directives for now, and just always make requests fail if they request an internal field.
Cole Lawrence
@colelawrence
Is it possible to change the introspection query results based on session details? For example, could I hide fields that the current credentials don't have permission to query?
Found relevant documentation here: https://gqlgen.com/reference/introspection/ but it looks like it's just to blanket enable or disable per session
Adrien D.
@dtrckd
Hi there, is there a way to generate named return values (naked return) in the generated resolvers ?
Also, do we have access to the revolver's templates in order to auto-generate some snippets inside the revolver methods at generation time ?
Jon Lundy
@JonLundy
@dtrckd You might be talking about the modelgen-hook that @colelawrence was asking about
you can modify the generated resolvers to your liking as well. That is just a helper to get started. I imagine it will wipe out implementations if you regen them anyway
Cole Lawrence
@colelawrence
@dtrckd I don't think what you're looking for is in the model-gen hook since it only applies to model generation
Jon Lundy
@JonLundy
ah more of a resolver-gen
Cole Lawrence
@colelawrence
@dtrckd you may have gone this deep already, but this looks like the place that currently handles that return template https://github.com/99designs/gqlgen/blob/14dccc57885df5d5ca4ef347c1b80f5f3648719a/codegen/field.go#L467-L484
Adrien D.
@dtrckd
@JonLundy @colelawrence , thx for your responses !
yes I didn"t found a relevant documentation for modifying code. Typically, I don't want to modify generated models, but the code inside generated resolvers. Instead of the default "panic", I have some code that should apply. (i am doing a bridge between gqlgen and dgraph, using gqlgen as a Business Logic Layer )
Cole Lawrence
@colelawrence
@dtrckd Yeah, so far, I've found it quite difficult to actually change the way gqlgen works without simply copying and pasting large swathes of its source code
But that could also be because I haven't found what I need to apply fine-grained control over what introspection will show versus what I want present on my generated models.
I suppose you could fork gqlgen
Jon Lundy
@JonLundy
... and contribute back to the project ;)
maybe this?
Cole Lawrence
@colelawrence
Nice find, I think you'd still have to figure out how to override ShortResolverDeclaration
https://github.com/99designs/gqlgen/blob/master/plugin/stubgen/stubs.gotpl#L45
Cole Lawrence
@colelawrence

So, I ended up writing a second yml file with configuration for adding arbitrary fields to generated models

# Add transient fields to the generated glq model
models:
  User:
    DBUser: github.com/my/package/models.User

# # Place all added fields into their own embedded struct with the following field name
# field: Internals

Then, I used a very simple plugin built off of the modelgen.Plugin to add these fields to the models during codegen. I will be experimenting to see if there are any "gotchas" that I might have missed with this approach.

cpunekar
@cpunekar
Guys, I'm very new to both graphql and golang, trying to run a simple app using gqlgen but observing below error, any help is appreciated -> User-MacBook-Pro:test-gql user$ go run github.com/99designs/gqlgen
validation failed: packages.Load: /Users/test-gql/schema.resolvers.go:12:73: NewTodo not declared by package models
/Users/test-gql/schema.resolvers.go:12:91: Todo not declared by package models
/Users/test-gql/schema.resolvers.go:16:63: Todo not declared by package models
/Users/test-gql/schema.resolvers.go:21:31: undeclared name: generated
/Users/test-gql/schema.resolvers.go:24:28: undeclared name: generated
exit status 1
User-MacBook-Pro:test-gql user$ go run github.com/99designs/gqlgen
validation failed: packages.Load: /Users/test-gql/schema.resolvers.go:12:73: NewTodo not declared by package models
/Users/test-gql/schema.resolvers.go:12:91: Todo not declared by package models
/Users/test-gql/schema.resolvers.go:16:63: Todo not declared by package models
/Users/test-gql/schema.resolvers.go:21:31: undeclared name: generated
/Users/test-gql/schema.resolvers.go:24:28: undeclared name: generated
exit status 1
I guess it's looking for model called 'NewTodo' which is part of the old model.. I have removed that part
Cole Lawrence
@colelawrence
@cpunekar are you observing that gqlgen is still generating the models file? Because this validation step shouldn't block the generation from happening. What is the expected behavior you're looking for?
cpunekar
@cpunekar
Thanks @colelawrence, I got past this issue, worked after deleting the resolver file. Now stuck on below error
User-MacBook-Pro:test-gql user$ go run server.go resolver.go generated.go
package main: found packages main (server.go) and test_gql (resolver.go) in /Users/test-gql
Jon Lundy
@JonLundy
ah that is because go expects one module per directory
cpunekar
@cpunekar
@JonLundy not sure what that means, is it related to the folder structure? mine looks like this
```
- models
    - person.go
    - pet.go
- generated.go
- resolver.go
- server.go
- schema.graphql
- gqlgen.yml
Jon Lundy
@JonLundy
hmm resolver is marked as package test_gql ?
you should be able to run with just go run .
cpunekar
@cpunekar
yes, resolver's pkg is test_gql.. still getting the same error with go run .
server is marked with pkg "main"
I tried making resolver and generated.go pkg names as main but got some cycle error, guess it's not that way
Jon Lundy
@JonLundy
hmm should only give you cycle if its trying to import its own package i think
is resolver trying to import the generated ?
cpunekar
@cpunekar
nope.. no references of generated in resolver
Jon Lundy
@JonLundy
what about server.go?
or rather what does it say is cycleing
cpunekar
@cpunekar
user-MacBook-Pro:test-gql user$ go run .
import cycle not allowed
package test-gql
    imports test-gql
Jon Lundy
@JonLundy
imports itsself?
try removing that :)
cpunekar
@cpunekar
package name is "main" in resolver, generated and server
ah yes.. I did import "tets-gql" in server.go (sleep deprived).. not sure how should I write below line since the server.go needs to in the folder as generated.go and resolver.go
cpunekar
@cpunekar
srv := handler.NewDefaultServer(test-gql.NewExecutableSchema(test-gql.Config{Resolvers: &test-gql.Resolver{}}))
Jon Lundy
@JonLundy
just remove the test-gql. from everything
its all defined in the same package
importing from the same directory is a python convention.. its not like that in Go
cpunekar
@cpunekar
the joy when your code works.. thanks to you @JonLundy