Coder Social home page Coder Social logo

Comments (5)

pleerock avatar pleerock commented on May 19, 2024

I think you can use lazy initialization pattern and store your loaded data inside resolver class once it requested and reuse this data in any resolver method.

Since resolvers are services, and services are scoped (scoped to request) this "cached" data inside resolver class will persist during a single user request.

from vesper.

singingwolfboy avatar singingwolfboy commented on May 19, 2024

@pleerock: Thanks, but that doesn't actually solve the problem. Rather than one database query per field, I want to make one database query that will return all fields that the GraphQL query requested. Caching the data doesn't solve this problem, it just means that every field won't be queried more than once.

from vesper.

pleerock avatar pleerock commented on May 19, 2024

I want to make one database query that will return all fields that the GraphQL query requested.

by "all fields" do you mean data from multiple tables?

from vesper.

singingwolfboy avatar singingwolfboy commented on May 19, 2024

When I run with ORM logging turned on, and I send this query:

query {
  people {
    name
    homeAddress
  }
}

I get this result:

query: SELECT "Person"."id" AS "Person_id" FROM "person" "Person"
query: SELECT "kvs"."id" AS "kvs_id", "kvs"."key" AS "kvs_key", "kvs"."value" AS "kvs_value", "kvs"."start_on" AS "kvs_start_on", "kvs"."end_on" AS "kvs_end_on", "kvs"."personId" AS "kvs_personId", "person"."id" AS "person_id" FROM "person_kv_string" "kvs" INNER JOIN "person" "person" ON "person"."id"="kvs"."personId" AND ("person"."id" IN ($1) AND COALESCE(kvs.start_on, '-infinity') <= $2 AND COALESCE(kvs.end_on, 'infinity') >= $3) WHERE "kvs"."key" = 'name' ORDER BY kvs.start_on DESC NULLS LAST -- PARAMETERS: [1,"today","today"]
query: SELECT "kvs"."id" AS "kvs_id", "kvs"."key" AS "kvs_key", "kvs"."value" AS "kvs_value", "kvs"."start_on" AS "kvs_start_on", "kvs"."end_on" AS "kvs_end_on", "kvs"."personId" AS "kvs_personId", "person"."id" AS "person_id" FROM "person_kv_string" "kvs" INNER JOIN "person" "person" ON "person"."id"="kvs"."personId" AND ("person"."id" IN ($1) AND COALESCE(kvs.start_on, '-infinity') <= $2 AND COALESCE(kvs.end_on, 'infinity') >= $3) WHERE "kvs"."key" = 'homeAddress' ORDER BY kvs.start_on DESC NULLS LAST -- PARAMETERS: [1,"today","today"]

Notice how the database query for fetching PersonKVString is being executed twice -- once for name and once for homeAddress. Ideally, it should be executed only once, and WHERE "kvs"."key" = 'name' would be replaced with WHERE "kvs"."key" IN ('name', 'homeAddress'). Does that make sense?

from vesper.

singingwolfboy avatar singingwolfboy commented on May 19, 2024

Is it possible to write a custom resolver for an entity, like my Person entity, instead of for a field on that entity? That would solve this problem, as well.

from vesper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.