Code Monkey home page Code Monkey logo

Comments (5)

Globegitter avatar Globegitter commented on September 13, 2024

@dwiel
That does of course depend on the actual use-case, but one way is to just group them together into an ObjectType and then you can have something as follows:

http://graphene-python.org/playground/?schema=import%2520graphene%250A%250Aclass%2520HelloPing(graphene.ObjectType)%253A%250A%2520%2520%2520%2520hello%2520%253D%2520graphene.String()%250A%2520%2520%2520%2520ping%2520%253D%2520graphene.String()%250A%250Aclass%2520Query(graphene.ObjectType)%253A%250A%2520%2520%2520%2520hello%2520%253D%2520graphene.String()%250A%2520%2520%2520%2520ping%2520%253D%2520graphene.String(to%253Dgraphene.String())%250A%2520%2520%2520%2520hello_ping%2520%253D%2520graphene.Field(HelloPing%252C%2520to%253Dgraphene.String())%250A%250A%2520%2520%2520%2520def%2520resolve_hello(self%252C%2520args%252C%2520info)%253A%250A%2520%2520%2520%2520%2520%2520%2520%2520return%2520%27World%2520%257B%257D%27.format(id(self))%250A%250A%2520%2520%2520%2520def%2520resolve_ping(self%252C%2520args%252C%2520info)%253A%250A%2520%2520%2520%2520%2520%2520%2520%2520h%2520%253D%2520self.resolve_hello(args%252C%2520info)%250A%2520%2520%2520%2520%2520%2520%2520%2520return%2520%27Pinging%2520%257B%257D%2520%253A%2520%257B%257D%27.format(args.get(%27to%27)%252C%2520h)%250A%2520%2520%2520%2520%250A%2520%2520%2520%2520def%2520resolve_hello_ping(self%252C%2520args%252C%2520info)%253A%250A%2520%2520%2520%2520%2520%2520%2520%2520h%2520%253D%2520%27World%2520%257B%257D%27.format(id(self))%250A%2520%2520%2520%2520%2520%2520%2520%2520return%2520HelloPing(ping%253D%27Pinging%2520%257B%257D%2520%253A%2520%257B%257D%27.format(args.get(%27to%27)%252C%2520h)%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520%2520hello%253Dh)%250A%250Aschema%2520%253D%2520graphene.Schema(query%253DQuery)%250A&query=query%2520%257B%250A%2520%2520hello%250A%2520%2520ping(to%253A%2522Peter%2522)%250A%2520%2520helloPing(to%253A%2522Peter%2522)%2520%257B%250A%2520%2520%2520%2520hello%250A%2520%2520%2520%2520ping%250A%2520%2520%257D%250A%257D%250A

from graphene.

dwiel avatar dwiel commented on September 13, 2024

The problem is that even with this solution, resolve_hello will be run 3 separate times. This can be a problem if that function is slow or depended on by multiple other fields in the query.

from graphene.

syrusakbary avatar syrusakbary commented on September 13, 2024

@dwiel if you wrap your fields with common attributes to a ObjectType, as @Globegitter suggested, the resolver for this ObjectType will be only called once.

So yes, resolving field would be called 3 times.

  1. The helloPing field in the Query.
  2. The hello field in HelloPing
  3. The ping field in HelloPing

However, as GraphQL (and graphene as well) is prepared to run the resolving fields functions asynchronously, this is how the nature of resolving queries should look like (as defined by the Spec, when querying we cannot assure that the fields would be resolved in order, hence we cannot depend on other fields resolve value in the same type level).

from graphene.

dwiel avatar dwiel commented on September 13, 2024

I see that makes sense. Thanks for the detailed answer

On Thu, Feb 4, 2016 at 12:33 PM, Syrus Akbary [email protected]
wrote:

@dwiel https://github.com/dwiel if you wrap your fields with common
attributes to a ObjectType, as @Globegitter
https://github.com/Globegitter suggested, the resolver for this
ObjectType will be only called once.

So yes, resolving field would be called 3 times.

  1. The parent resolver (helloPing)
  2. The hello field in HelloPing
  3. The ping field in HelloPing

However, as GraphQL (and graphene as well) is prepared to run the
resolving fields asynchronously, this is how the nature of resolving
queries should look like (as defined by the Spec, when querying we cannot
assure that the fields would resolved in order, hence we cannot depend on
other fields resolve value in the same type level).


Reply to this email directly or view it on GitHub
#82 (comment)
.

from graphene.

syrusakbary avatar syrusakbary commented on September 13, 2024

No problem! Closing the issue

from graphene.

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.