Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 00:17
    landrade commented #1048
  • 00:12
    landrade commented #1048
  • 00:11
    ianling opened #2220
  • 00:07
    landrade commented #1048
  • May 26 23:51
    ianling closed #2218
  • May 26 23:20
    ianling commented #2218
  • May 26 23:13
    coveralls commented #2175
  • May 26 23:09
    StevenACoffman commented #2219
  • May 26 23:09
    StevenACoffman closed #2219
  • May 26 23:08
    StevenACoffman edited #2219
  • May 26 23:05
    coveralls commented #2219
  • May 26 23:03
    MoofMonkey edited #2219
  • May 26 23:02
    MoofMonkey opened #2219
  • May 26 22:45
    MoofMonkey commented #2209
  • May 26 22:30
    ianling opened #2218
  • May 26 19:12
    StevenACoffman labeled #2168
  • May 26 19:11
    StevenACoffman commented #2214
  • May 26 19:10
    StevenACoffman commented #2209
  • May 26 19:08
    StevenACoffman labeled #2209
  • May 26 19:03
    StevenACoffman closed #2216
Julien
@juleur
?
Andreas Backx
@AndreasBackx
Does anyone know whether the context from the federated gateway is propagated in gqlgen? I cannot find any documentation on it and following what a request goes through is a bit hard. https://www.apollographql.com/docs/apollo-server/federation/implementing/#sharing-context-across-services
Andreas Backx
@AndreasBackx
Oh wow, I looked straight over the fact that you need to do that manually. My apologies.
sateeshpnv
@sateeshpnv
Any suggestions on splitting generated.go into smaller files? I have a repo with a 256kB size restriction per file.
sateeshpnv
@sateeshpnv

Any suggestions on splitting generated.go into smaller files? I have a repo with a 256kB size restriction per file.

Render() function at https://github.com/99designs/gqlgen/blob/master/codegen/templates/templates.go#L61 looked like the code that can be refactored to split a single generated.go into multiple files. However, looking at a generated.go file for one query, biggest region is field.topl and this region alone can cross 256KB.

1: 21 // region ** generated!.gotpl **
2: 1666 // region * args.gotpl *
3: 1726 // region ** directives.gotpl **
4: 1730 // region ** field.gotpl *
5: 7009 // region ** input.gotpl *
6: 7031 // region ** interface.gotpl *
7: 7035 // region ** object.gotpl **
8: 8348 // region * type.gotpl *

Goran Popovic
@gpopovic
Hey guys..Is it possible to pass somehow through context from parent resolver to child? generated model is passed, but i would like to pass something else
an example viewer pattern... i would like all resolvers that are generated on top of viewer to have (full internal user) available
Jon Lundy
@JonLundy
so .. you want the user for the current request to be passed along?
yes. if you set the user in the context it should be available to the other resolvers. As long as it takes place first.
I usually set it in the context before it is passed on to the graphql handler
Ngure Nyaga
@ngurenyaga

I have a bit of a mystery here:

App 1

The generated ResolverRoot interface looks like this:

type ResolverRoot interface {
    Query() QueryResolver
    Mutation() MutationResolver
}

App 2

In addition to the Mutation() MutationResolver and Query() QueryResolver that I expected, there are several more interfaces e.g

type ResolverRoot interface {
    // a bunch of interfaces elided
    Workstation() WorkstationResolver
}

These extra interfaces define some of the behaviour of a Relay "node" (generation of object identifiers) e.g

type WorkstationResolver interface {
    ID(ctx context.Context, obj *Workstation) (string, error)
}

Both apps' GraphQL schemata look similar. For both, I was trying to implement a Relay spec-compliant API.

The first app (the one that did not generate the extra interfaces and behaved as I expected) has a schema that looks like this:

interface Node {
  id : ID!
}

enum SortOrder {
  ASC
  DESC
}

type PageInfo {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  startCursor: String
  endCursor: String
}

type Organization implements Node {
  id: ID!
  name: String!
  sladeCode: String!
}

type OrganizationEdge {
  cursor: String
  node: Organization
}

type OrganizationConnection {
  edges: [OrganizationEdge]
  pageInfo: PageInfo!
}

input OrganizationFilterInput {
  search : String
  name: String
  sladeCode: String
}

input OrganizationSortInput {
  name: SortOrder
  sladeCode: SortOrder
}

# a bunch of similar types have been elided

type Query {

  listOrganizations(
    first: Int, after: String, last: Int, before: String,
    filter: OrganizationFilterInput, sort: OrganizationSortInput): OrganizationConnection!

  findOrganization(id: ID!): Organization!

  # A bunch of fields elided here too
}

The schema in the second app follows the same patterns.

The mystery is - what caused the second app to generate all these extra interfaces?

I am on gqlgen v0.11.2. What should I look at?

Goran Popovic
@gpopovic
@JonLundy but im talking about adding something in a resolver to the context ..since you're not returning context..how can other resolver get it? It doesnt make sense
Jon Lundy
@JonLundy
the context is a pointer. so changes to it will exist upstream
@gpopovic
Jon Lundy
@JonLundy
@ngurenyaga The only thing i can think of is that the underlying struct for Workstation has a different gotype. if Workstation.ID is not a string and say an int value it will need a way to convert to string
@ngurenyaga can you paste the struct definitions? like:
type Workstation struct{
  ID uint64
  ...
}

// vs

type Workstation struct{
  ID string
  ...
}
Goran Popovic
@gpopovic

hey guys.. im using @goField(forceResolver: true) directive to generate resolvers for relations and i have a problem
Lets say that user has a country.

In graphql its not idiomatic to have countryId, in the object, only Country.

This means that in generated resolver function (I get *generated.User) who does not have country id.

This means that i cannot easily reuse my service GetCountryById

What would be the best solution for this?
Jon Lundy
@JonLundy
in mine i usually will have somethingId along side something
i also include for Something both the id and somethingId
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