GithubHelp home page GithubHelp logo

iwconfig / graphql-dsl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from avanov/graphql-dsl

0.0 0.0 0.0 52 KB

Compose GraphQL queries by composing Python types!

Home Page: https://graphql-dsl.rtfd.io/

License: MIT License

Python 86.00% Nix 10.28% Makefile 3.72%

graphql-dsl's Introduction

https://github.com/avanov/graphql-dsl/workflows/GitHub%20CI/badge.svg?branch=develop https://travis-ci.org/avanov/graphql-dsl.svg?branch=develop https://circleci.com/gh/avanov/graphql-dsl/tree/develop.svg?style=svg https://coveralls.io/repos/github/avanov/graphql-dsl/badge.svg?branch=develop Requirements Status Documentation Status Latest PyPI Release

Compose GraphQL queries by composing Python types

pip install graphql-dsl

Let's take a manually written GraphQL query from the official docs:

query {
    hero {
        name
    }
    droid(id: "2000") {
        name
    }
}

With graphql-dsl you can construct a similar query with the following Python snippet:

from typing import NamedTuple
from graphql_dsl import *

class Hero(NamedTuple):
    name: str

class Droid(NamedTuple):
    name: str

class HeroAndDroid(NamedTuple):
    hero: Hero
    droid: Droid

class Input(NamedTuple):
    droid_id: ID

q = GQL( QUERY | HeroAndDroid
       | WITH  | Input
       | PASS  | Input.droid_id * TO * HeroAndDroid.droid * AS * 'id'
       )

print(q.query)

and the output will be:

query HeroAndDroid($droidId:ID!){hero{name}droid(id:$droidId){name}}

The value of q.query is a GraphQL query template that should be used with instances of Input to call servers with GraphQL API

import requests

q = GQL(...)

def call_server(droid_id: ID) -> HeroAndDroid:
    response = requests.post(
        url='https://<graphql-server-url>/',
        json=q.request_payload(Input(droid_id=droid_id)),
        headers={ 'Content-Type': 'application/json'
                , 'Accept': 'application/json'
                }
    )
    response.raise_for_status()
    return q.get_result(response.json())

Note that the query q resides at the top-level module scope, as the query constructor doesn't depend on the query's input values, it only needs to know about the shapes of input and output data.

The query builder supports both NamedTuple and @dataclass types, yet the latter has a slightly different field reference syntax (because dataclasses don't define class-level field getters):

from dataclasses import dataclass
from graphql_dsl import *

@dataclass
class Hero:
    name: str

@dataclass
class Droid:
    name: str

@dataclass
class HeroAndDroid:
    hero: Hero
    droid: Droid

@dataclass
class Input:
    droid_id: ID

q = GQL( QUERY | HeroAndDroid
       | WITH  | Input
       | PASS  | (Input, 'droid_id') * TO * (HeroAndDroid, 'droid') * AS * 'id'
       )

Find out more from Official Documentation.

Test Suite

Test environment is based on Nix.

nix-shell
pytest

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.