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

Add functionality to pass context while rendering #24

Open
safwanrahman opened this issue Sep 1, 2023 · 2 comments
Open

Add functionality to pass context while rendering #24

safwanrahman opened this issue Sep 1, 2023 · 2 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@safwanrahman
Copy link

safwanrahman commented Sep 1, 2023

Hi,
First of all, Thanks a lot for the package 🚀. It is very nice to have this kind of DSL for GraphQL.

Currently I am working on a project where I need to have one optional argument according to the logic. We need to decide to render the argument only the time when we are rending the whole query.

So it would be nice to pass some context while rendering. Therefore, we can extend the Argument overwrite the template to not render the argument if flag is True in the context.

So it can be like following

In jinja template:

// optional_argument.jinja2
{% if ctx.render_foo_field %}
{{ name }}: {
  {{ value }}
}
{% endif %}
class OptionalArgument(Argument):
    template = "optional_argument.jinja2"
    def render(self, context=None):
        template = template_env.get_template(self.template)
        return template.render(name=self.name, value=self.value, ctx=context)

query = Query(name="bar", arguments=OptionalArgument(name="foo", value="bar")

print(query.render(context={"render_foo_field": True}))
# This query is with the foo field argument

print(query.render(context={"render_foo_field": False}))
# This one will not have foo field as argument

What do you think? If you are interested, I can make a PR

@denisart denisart self-assigned this Sep 1, 2023
@denisart
Copy link
Owner

denisart commented Sep 4, 2023

Hello, thanks for using graphql-query. I'm glad the library is useful.

i think that this is a nice idea. Can you create a PR with general context for each render method?

@safwanrahman
Copy link
Author

@denisart Thanks! I will create a PR for passing context to each render method!

@denisart denisart added the good first issue Good for newcomers label Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants