3

I am using https://developer.github.com/v4/

And I have a huge query like this:

query ($login: String!, $first: Int, $after: String) {
  user (login: $login){
    avatarUrl
    login
    name,
    followers(first: $first, after:$after) { 
      edges{
        cursor
        node{
          id
          name
          login
          avatarUrl
        }
      }
      totalCount
    },
    repositories(first: $first) {
      edges{
        cursor
        node{
          id
          name
        }
      }
      totalCount
    }
  }
}

But I think it's bad to query huge data from server.

I have followers and repositories pages. So I think split this huge query to small queries is better.

Here is small queries:

followers query:

query($login: String!, $first: Int, $after: String) {
    user(login: $login) {
      followers(first: $first, after: $after) {
        edges {
          cursor
          node {
            id
            name
            login
            avatarUrl
          }
        }
        totalCount
      }
    }
  }

repositories query:

query($login: String!, $first: Int, $after: String) {
    user(login: $login) {
      repositories(first: $first, after: $after) {
        nodes {
          id
          name
        }
        totalCount
      }
    }
  }

user query:

query($login: String!, $first: Int) {
    user(login: $login) {
      avatarUrl
      login
      name
    }
  }

Am I correctly? Is it necessary to do this? What's the best practice this situation? Is there any documentation for teaching people how to handle this or told people the best practice?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • What is the problem with the "huge" query (actually I think it is quite small)? This is why GraphQL was invented in the first place. Also Github will be happy that you get the data you need - and only the data you need - in a single query. As described in the Apollo docs there are some reasons for splitting up querys (e.g. for speeding up UIs) but especially in server to server communication there are very rare. Please reconsider splitting them up in the first place. – Herku Jul 15 '18 at 20:08
  • Did you ever find an appropriate solution for this? I don't see how fragments help without related smaller queries to refetch that fragmented portion of the larger query. – Jens Bodal Dec 15 '20 at 03:09

1 Answers1

5

You can spit your queries into Fragments and in that way you would still only trigger one request and have smaller "queries". Something like this:

Fragment for followers:

fragment followers on User {
    followers(first: $first, after: $after) { 
      edges{
        cursor
        node{
          id
          name
          login
          avatarUrl
        }
      }
      totalCount
    },
}

Fragment for repositories:

fragment repositories on User {
  repositories(first: $first) {
      edges{
        cursor
        node{
          id
          name
        }
      }
      totalCount
    }
}

Put them all together in the query:

query ($login: String!, $first: Int, $after: String) {
  user (login: $login){
    avatarUrl
    login
    name
    ...followers
    ...repositories
  }
}
Marco Daniel
  • 5,467
  • 5
  • 28
  • 36