denisart / graphql-query Goto Github PK
View Code? Open in Web Editor NEWComplete Domain Specific Language (DSL) for GraphQL query in Python.
Home Page: https://denisart.github.io/graphql-query/
License: MIT License
Complete Domain Specific Language (DSL) for GraphQL query in Python.
Home Page: https://denisart.github.io/graphql-query/
License: MIT License
How to render a valid GraphQL query with no fields
kwarg?
query = Query(
name='resetPassword',
arguments=[
Argument(
name='userId',
value=user_id,
),
Argument(
name='confirmationId',
value=confirmation_id,
),
Argument(
name='password',
value=password,
),
Argument(
name='passwordConfirm',
value=password_confirm,
),
],
)
operation = Operation(
type='mutation',
name='ResetPassword',
queries=[query],
)
print(operation.render())
Actual query:
mutation ResetPassword {
resetPassword(
userId: "69fa0d2e-f7e8-410c-be50-24a03804cb4c"
confirmationId: "63f65921-66cf-400a-aa05-71446b3621d2"
password: "87654321"
passwordConfirm: "87654321"
) {
}
}
Expected query:
mutation ResetPassword {
resetPassword(
userId: "69fa0d2e-f7e8-410c-be50-24a03804cb4c"
confirmationId: "63f65921-66cf-400a-aa05-71446b3621d2"
password: "87654321"
passwordConfirm: "87654321"
)
}
Hi! Nice library. I'm willing to use it in my project. But I have an issue:
input SomeInput {
someListArgument: [String!]!
}
Given list of strings, I'd like to generate string someListArgument: ["value"]
.
The following raises pydantic validation error:
graphql_query.Argument(name='someArgument', value=['value'])
E pydantic.error_wrappers.ValidationError: 5 validation errors for Argument
E value
E str type expected (type=type_error.str)
E value
E value is not a valid dict (type=type_error.dict)
E value -> 0
E value is not a valid dict (type=type_error.dict)
E value -> 0
E value is not a valid list (type=type_error.list)
E value
E value is not a valid dict (type=type_error.dict)
And this generates object instead of array:
graphql_query.Argument(name='someArgument', value=[
graphql_query.Argument(name='someArgumentItem', value=v)
for v in ['value']
])
There doesn't seem to be a nice way to generate an array argument. (I have to explicitly render it as string right now)
I havent tried variables. But even if they work, I'd like to keep my query explicit.
Thanks in advance!
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
To Reproduce
print(Argument(name='someArgument', value=['value']).render())
someArgument: [value]
print(Argument(name='someArgument', value=["value"]).render())
someArgument: [value]
Expected behavior
print(Argument(name='someArgument', value=['value']).render())
someArgument: ['value']
print(Argument(name='someArgument', value=["value"]).render())
someArgument: ["value"]
Desktop (please complete the following information):
It would suck having to write code to manually double quote lists of strings.
The Argument type allows many types, but the float type is missing.
The scalar types as defined in the spec.
Describe the bug
when I use graphql_query with pytest, here is the error message. What I am confused about is that if graphql_query is used alone, this error will not be reported. Please pay attention to this issue. Thank you.
from graphql_query import Argument, Directive, Field, Operation, Query, Variable
../../../../venv/lib/python3.11/site-packages/graphql_query/__init__.py:5: in <module>
from .types import Argument, Directive, Field, Fragment, InlineFragment, Operation, Query, Variable
../../../../venv/lib/python3.11/site-packages/graphql_query/types.py:41: in <module>
class _GraphQL2PythonQuery(PydanticBaseModel):
../../../../venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py:90: in __new__
config_wrapper = ConfigWrapper.for_model(bases, namespace, kwargs)
../../../../venv/lib/python3.11/site-packages/pydantic/_internal/_config.py:120: in for_model
config_from_namespace = config_dict_from_namespace or prepare_config(config_class_from_namespace)
../../../../venv/lib/python3.11/site-packages/pydantic/_internal/_config.py:271: in prepare_config
warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)
E pydantic.warnings.PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead.
Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/
Hello, thanks for this package.
I need to add comments to rendered query as like that
{
hero {
name
# Queries can have comments!
friends {
name
}
}
}
I can adding this to mater.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.