avanov / graphql-dsl

Compose GraphQL queries by composing Python types!
https://graphql-dsl.rtfd.io/
MIT License
5 stars 2 forks source link

Little help with example please #6

Open brucebookman opened 3 years ago

brucebookman commented 3 years ago

This library looks very useful as writing GraphQL queries is a pain.

Your example / tutorial is not clear to me on exactly how I would specify a specific value for the input.

You give this example

query { hero { name } droid(id: "2000") { name } }

Yet here, there is no value 2000 q = GQL( QUERY | HeroAndDroid | WITH | Input | PASS | Input.droid_id * TO * HeroAndDroid.droid * AS * 'id' )

I think I'm not 100% familiar with the code style you are using. I'm guessing the asterisk means "wild card" and I'd replace that with 2000 to mirror the example.

Is that correct? Or do I do something like this:

`Input.droid_id = 2000

q = GQL( QUERY | HeroAndDroid | WITH | Input | PASS | Input.droid_id TO HeroAndDroid.droid AS 'id' )`

Thanks for the help, and BTW I have seen this coding style a few times, but have no idea what it is called or where to learn about it. The style uses the pipe symbol in a way I'm not familiar with. What is this called and where can I learn more? Thnx!!!!

avanov commented 3 years ago

Hi @brucebookman , I've updated the readme, let me know if there are more things to clarify.

Regarding the pipes | and asterisks *, in the context of this library they are just standard-defined overloaded operators | and *, that can be customised for domain-specific purposes. For the purpose of this library they are used as operators that concatenate query sub-expressions into larger experessions that represent the whole query. This style of encoding a composition of sub-expressions is called combinators (though, they are not the same as combinators in Maths), and this style is often used in cases where sub-expressions are allowed to exist independently from the main query:


i_am_querying           = QUERY | HeroAndDroid
my_input_will_be        = WITH | Input
i_will_bind_it_this_way = PASS | Input.droid_id * TO * HeroAndDroid.droid * AS * 'id'

q = GQL(i_am_querying | my_input_will_be | i_will_bind_it_this_way)

Note that this is exactly the same query as in your snippet, yet the individual sub-expressions are defined and exist separately. This style of API allows for greater flexibility when it comes to composing different query conditions together and makes the whole process akin to composing (combining) Lego blocks to achieve the desired result.

So, the pipes and asterisks are just a convention that I chose for my DSL implementation. As this DSL is a valid python code, it can be called EDSL (embedded-dsl), where the "embedded" part points to the fact that a new "sub-language" is embedded into the main language (Python) to express the domain the library tries to aid (composing graphql queries). The reason why I chose two symbols (| and *) instead of just one of them has to do with operators precedence in Python - * is important because it has a higher binding priority to | and this allows us not to use parentheses in this expression: PASS | Input.droid_id * TO * HeroAndDroid.droid * AS * 'id' - Python interpreter knows that it needs to combine Input.droid_id * TO * HeroAndDroid.droid * AS * 'id' before combining the resulting expression with PASS.

avanov commented 3 years ago

I've published a new release with a helper method https://github.com/avanov/graphql-dsl/pull/7

brucebookman commented 3 years ago

Thanks so much, this is very helpful

On Mon, May 31, 2021 at 4:12 PM Maxim Avanov @.***> wrote:

I've publsihed a new release with a helper method #7 https://github.com/avanov/graphql-dsl/pull/7

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/avanov/graphql-dsl/issues/6#issuecomment-851712880, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASV6NX5JECJVN3X5QI6SSADTQQJXFANCNFSM45ZQHWDQ .