Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

methods do not know what info to provide #6

Open
MARTIMM opened this issue Dec 10, 2017 · 5 comments
Open

methods do not know what info to provide #6

MARTIMM opened this issue Dec 10, 2017 · 5 comments

Comments

@MARTIMM
Copy link

MARTIMM commented Dec 10, 2017

Users can ask things via a query but can also leave out items they do not need in the query. The methods, on the other hand, do not know what is left out from the query and must provide all data. This could involve expensive calculations or database lookups.

@CurtTilmes
Copy link
Owner

Each method can just provide the information it is asked for. If something is expensive to calculate or look up, just don't do it until explicitly asked for that item.

For example, if you have an "Item" with method foo and bar which are expensive:

class Item
{
    method foo(--> Str) {...}
    method bar(--> Str) {...}
}
class Query
{
    method item(--> Item) { Item.new }
}

my $schema = GraphQL::Schema.new(Item, Query);

with a query like this:

{
    item { foo }
}

It will call Item.foo, but won't call Item.bar.

@MARTIMM
Copy link
Author

MARTIMM commented Dec 13, 2017

Hi Curt,
Thanks for your answer. I just misunderstood an example from the docs where you showed an example of a User class with only some attributes. The object from this class was filled in by some other method and returned the object. It was a mistake of mine not to think a bit further about how the attribute is handled by perl6. E.g.

class User {
  has $.id is rw;
...
}

is something like

class User {
  has $!id;
  method id( ) is rw { ...; $!id; }
}

which provides for reading as well as writing.

Regards,
Marcel

@MARTIMM
Copy link
Author

MARTIMM commented Dec 14, 2017

btw;
How does a method know that it was used for reading(query {...}) or for writing (mutation {...})? Do I need to specify different methods?

@CurtTilmes
Copy link
Owner

It is really up to you to organize things that way. If you do a mutation type activity within a query method, nothing will stop you.

I've just been making a 'top-level' class Query {} with all my queries and class Mutation {} with all my mutations. That fits with the conventions, works well with GraphiQL and makes it very clear.

@MARTIMM
Copy link
Author

MARTIMM commented Dec 15, 2017

ah, another overlooked thing. I knew that it was possible to do modifications in a query but that it wasn't recommended. Studying your code I see that the schema must be set using :query and/or :mutation named attributes, of which you told me about the first one before. It might be an idea to have a pod document for at least the GraphQL.pm6 module.

Anyway, thanks for your help,
Marcel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants