1

I'm learning graphql and I think I've spot one flaw in it. Suppose we have schema like this

type Hero {
  name: String
  friends: [Person]
}

type Person {
  name: String
}

and two queries

{
  hero {
    name
    friends {
      name
    }
  }
}

and this

{
  hero {
    name
  }
}

And a relational database that have two corresponding tables Heros and Persons.

If my understanding is right I can't resolve this queries such that for the first query the resulting sql query would be

select Heros.name, Persons.name
from Heros, Persons
where Hero.name = 'Some' and Persons.heroid = Heros.id

And for the second

select Heros.name, Persons.name from Heros

So that only the fields that are really needed for the query would be loaded from the database.

Am I right about that? Also if graphql would have ability to return only the data that's needed for the query, not the data that's valid for full schema I think this would be possible, right?

3
  • @p0k8_ Why not? Feb 9, 2017 at 10:54
  • @p0k8_ it's not good make own custom batching request to database, because the user may not query for the nested type. If user hasn't queried nested type, there shouldn't be any joins in sql, as I've showed in second query. Either you don't understand what I'm asking or I don't understand what you're saying. Feb 9, 2017 at 11:09
  • This is precisely the problem I intended to solve when I wrote Join Monster. It generates the SQL and dynamically adds joins only when the nested types are actually requested. It can go arbitrarily deep and do pagination with postgres (and soon mariadb). Feb 26, 2017 at 8:27

2 Answers 2

3

Yes, this is definitely possible and encouraged. However, the gist of it is that GraphQL essentially has no understanding of your storage layer until you explicitly explain how to fetch data. The good news about this is that you can use graphql to optimize queries no matter where the data lives.

If you use javascript, there is a package graphql-fields that can simplify your life in terms of understanding the selection set of a query. It looks something like this.

If you had this query

query GetCityEvents {
  getCity(id: "id-for-san-francisco") {
    id
    name
    events {
      edges {
        node {
          id
          name
          date
          sport {
            id
            name
          }
        }
      }
    }
  }
}

then a resolver might look like this

import graphqlFields from 'graphql-fields';

function getCityResolver(parent, args, context, info) {
  const selectionSet = graphqlFields(info);
  /**
    selectionSet = {
      id: {},
      name: {},
      events: {
        edges: {
          node: {
            id: {},
            name: {},
            date: {},
            sport: {
              id: {},
              name: {},
            }
          }
        }
      }
    }
  */
  // .. generate sql from selection set
  return db.query(generatedQuery);
}

There are also higher level tools like join monster that might help with this.

Here is a blog post that covers some of these topics in more detail. https://scaphold.io/community/blog/querying-relational-data-with-graphql/

1

In Scala implementation(Sangria-grahlQL) you can achieve this by following:

Suppose this is the client query:

query BookQuery { 
    Books(id:123) { 
      id 
      title 
      author {
        id
        name
      }
    }
}

And this is your QueryType in Garphql Server.

val BooksDataQuery = ObjectType(
    "data_query",
    "Gets books data",
    fields[Repository, Unit](
      Field("Books", ListType(BookType), arguments = bookId :: Nil, resolve = Projector(2, (context, fields) =>{ c.ctx.getBooks(c.arg(bookId), fields).map(res => res)}))
    )
)
val BookType = ObjectType( ....)
val AuthorType = ObjectType( ....)

Repository class:

def getBooks(id: String, projectionFields: Vector[ProjectedName]) {
/* Here you have the list of fields that client specified in the query. 
    in this cse Book's id, title and author - id, name. 
    The fields are nested, for example author has id and name. In this case author will have sequence of id and name. i.e. above query field will look like:
    Vector(ProjectedName(id,Vector()), ProjectedName(title,Vector()),ProjectedName(author,ProjectedName(id,Vector()),ProjectedName(name,Vector())))

    Now you can put your own logic to read and parse fields the collection and make it appropriate for query in database. */
}

So basically, you can intercept specified fields by client in your QueryType's field resolver.

2
  • Okay, that's nice! Strangely no such examples are in graphql site. Is this some kind of hack that Scala library does... Feb 9, 2017 at 12:49
  • Yes, the official graphql site does not provide much and because of small community, google won't help much either. Btw, there are several serverside frameworks of Graphql link and the above one is Sangria Graphql for scala. You should try checking these frameworks' github and go through demo projects and code.
    – Ra Ka
    Feb 9, 2017 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.