Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-query

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-query

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

  • 1.4.0
  • Source
  • PyPI
  • Socket score

Maintainers
1

graphql-query

tag last-commit PyPI - Python Version license


graphql_query is a complete Domain Specific Language (DSL) for GraphQL query in Python. With this package you can to

  • generate a correct GraphQL query string from a python classes;
  • use and share similar Arguments, Variables and e.t.c between different queries;
  • easily add new fields to your query;
  • add Fragments and Directives to queries;
  • generate graphql_query classes from pydantic data-model;

The documentation for graphql_query can be found at https://denisart.github.io/graphql-query/.

Quickstart

Install with pip

pip install graphql_query

Simple query

Code for the simple query

{
  hero {
    name
  }
}

it is

from graphql_query import Operation, Query

hero = Query(name="hero", fields=["name"])
operation = Operation(type="query", queries=[hero])

print(operation.render())
"""
query {
  hero {
    name
  }
}
"""

The render method for the graphql_query.Operation object just returns the final string with a query. Inside the fields array of the graphql_query.Query object you can use

  • str (a field name);
  • object of graphql_query.Field type;
  • graphql_query.Fragment and graphql_query.InlineFragment.

Arguments, Variables and Directives

For generation of the following query

query Hero($episode: Episode, $withFriends: Boolean!) {
  hero(episode: $episode) {
    name
    friends @include(if: $withFriends) {
      name
    }
  }
}

we can use the following python code

from graphql_query import Argument, Directive, Field, Operation, Query, Variable

episode = Variable(name="episode", type="Episode")
withFriends = Variable(name="withFriends", type="Boolean!")

arg_episode = Argument(name="episode", value=episode)
arg_if = Argument(name="if", value=withFriends)

hero = Query(
    name="hero",
    arguments=[arg_episode],
    fields=[
        "name",
        Field(
            name="friends",
            fields=["name"],
            directives=[Directive(name="include", arguments=[arg_if])]
        )
    ]
)
operation = Operation(
    type="query",
    name="Hero",
    variables=[episode, withFriends],
    queries=[hero]
)
print(operation.render())
"""
query Hero(
  $episode: Episode
  $withFriends: Boolean!
) {
  hero(
    episode: $episode
  ) {
    name
    friends @include(
      if: $withFriends
    ) {
      name
    }
  }
}
"""

You can find other examples in the documentation.

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc