Gqlgen: Questions about nested queries with params, SQL query optimisation and slow performance with Dataloader

Created on 12 Sep 2018  路  6Comments  路  Source: 99designs/gqlgen

Problem

I'm about to start using this library for a project for my company, but I'm not sure about two things..

Questions

  1. How would one write resolvers to query users with the last 5 posts for each users ?
query getUsers {
  users {
    id
    name
    posts(numberOfPosts: 5) {
      id
      title
      text
    }
  }
}
  1. How would one write resolvers to query SQL database with only the specific columns ? Like in the gql query from above. In this case :
query getUsers {
  users {
    id
    name
    posts(numberOfPosts: 5) {
      id
      title
      text
    }
  }
}

One would not need to SELECT *, but SELECT user.id, user.name but also post.id, post.name, post.text.
I think we can get field from context but how could know which field is a scalar and which is not ?

Thanks in advance for answering my questions.

Most helpful comment

This is going to depend a little bit on how you're mapping to your User and Post models, but in general if in your schema you add an argument to a field, you will get given a resolver to implement.

type User {
    posts(numberOfPosts: Int!): [Post!]!
}

The above schema will generate a resolver interface that looks something like:

func (r *userResolver) Posts(ctx context.Context, u *User, numberOfPosts int) ([]Post, error) {
    // TODO retrieve numberOfPosts Posts for User from DB
}

For your second question, it depends on how you're fetching from your database in the resolver. To get access to the set of collected fields, you can call graphql.CollectFieldsCtx() and inspect the returned []CollectedField for the columns you require.

Hi, I just realized it doesn't work like that. The Posts resolver doesn't get generated, are you it is implemented yet ?

All 6 comments

This is going to depend a little bit on how you're mapping to your User and Post models, but in general if in your schema you add an argument to a field, you will get given a resolver to implement.

type User {
    posts(numberOfPosts: Int!): [Post!]!
}

The above schema will generate a resolver interface that looks something like:

func (r *userResolver) Posts(ctx context.Context, u *User, numberOfPosts int) ([]Post, error) {
    // TODO retrieve numberOfPosts Posts for User from DB
}

For your second question, it depends on how you're fetching from your database in the resolver. To get access to the set of collected fields, you can call graphql.CollectFieldsCtx() and inspect the returned []CollectedField for the columns you require.

And also a third question

  1. I tried the Dataloader example as it is. And I get around 20ms for the request duration with this simple query :
query getCustomers {
  customers {
    id
    name
    orders {
      id
      date
      amount
      items {
        name
      }
    }
  }
}

Is this not too much, knowing that there is not a lot of data ?

It's difficult to say whether this is a long time or not without more information on the size of your dataset, how you've implemented your resolvers etc. I would suggest doing some profiling to get a sense of where you're spending your time, ensuring you have your dataloader well configured etc. We do have an open tracing middleware that might be of use.

This is going to depend a little bit on how you're mapping to your User and Post models, but in general if in your schema you add an argument to a field, you will get given a resolver to implement.

type User {
    posts(numberOfPosts: Int!): [Post!]!
}

The above schema will generate a resolver interface that looks something like:

func (r *userResolver) Posts(ctx context.Context, u *User, numberOfPosts int) ([]Post, error) {
    // TODO retrieve numberOfPosts Posts for User from DB
}

For your second question, it depends on how you're fetching from your database in the resolver. To get access to the set of collected fields, you can call graphql.CollectFieldsCtx() and inspect the returned []CollectedField for the columns you require.

Hi, I just realized it doesn't work like that. The Posts resolver doesn't get generated, are you it is implemented yet ?

@Daavidaviid Is right , I have something similar to query using input variables and I dont see a resolver being generated . Although I could see some type generated related to the input variables

@mathewbyrne , i have got similar thing but gqlgen does not generate anything about the Post resolver as your comments. Please confirm this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jszwedko picture jszwedko  路  3Comments

RobertoOrtis picture RobertoOrtis  路  3Comments

cemremengu picture cemremengu  路  3Comments

lynntobing picture lynntobing  路  3Comments

steebchen picture steebchen  路  3Comments