Coder Social home page Coder Social logo

denisart / graphql-query Goto Github PK

View Code? Open in Web Editor NEW
51.0 3.0 6.0 108 KB

Complete Domain Specific Language (DSL) for GraphQL query in Python.

Home Page: https://denisart.github.io/graphql-query/

License: MIT License

Python 94.21% Jinja 3.23% Makefile 2.57%
code-generation codegen graphql graphql-query graphql-query-builder python query-builder query-generation query-generator pydantic

graphql-query's Introduction

graphql-query's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

graphql-query's Issues

[Question] How to render query with no fields selection?

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"
  )
}

Cannot render list of strings Argument.

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!

Add functionality to pass context while rendering

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

Quotes are stripped from list of strings in Argument

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):

  • OS: Windows 11 Pro
  • Python 3.10

It would suck having to write code to manually double quote lists of strings.

When using graphql_query with pytest, an error occurs: Deprecated in Pydantic V2.0 to be removed in V3.0.

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/

errorMessage

Comment in query or mutation

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.

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.