Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A GraphQL client in Python

License

NotificationsYou must be signed in to change notification settings

graphql-python/gql

Repository files navigation

This is a GraphQL client for Python.Plays nicely withgraphene,graphql-core,graphql-js and any other GraphQL implementationcompatible with theGraphQL specification.

GQL architecture is inspired byReact-Relay andApollo-Client.

GitHub-ActionspyversionpypiAnaconda-Server Badgecodecov

Documentation

The complete documentation for GQL can be found atgql.readthedocs.io.

Features

Installation

You can install GQL with all the optional dependencies using pip:

# Quotes may be required on certain shells such as zsh.pip install"gql[all]"

NOTE: See alsothe documentation to install GQL with less extra dependencies depending on the transports you would like to use or for alternative installation methods.

Usage

Sync usage

fromgqlimportClient,gqlfromgql.transport.aiohttpimportAIOHTTPTransport# Select your transport with a defined url endpointtransport=AIOHTTPTransport(url="https://countries.trevorblades.com/")# Create a GraphQL client using the defined transportclient=Client(transport=transport)# Provide a GraphQL queryquery=gql("""    query getContinents {      continents {        code        name      }    }""")# Execute the query on the transportresult=client.execute(query)print(result)

Executing the above code should output the following result:

$ python basic_example.py{'continents': [{'code': 'AF', 'name': 'Africa'}, {'code': 'AN', 'name': 'Antarctica'}, {'code': 'AS', 'name': 'Asia'}, {'code': 'EU', 'name': 'Europe'}, {'code': 'NA', 'name': 'North America'}, {'code': 'OC', 'name': 'Oceania'}, {'code': 'SA', 'name': 'South America'}]}

WARNING: Please note that this basic example won't work if you have an asyncio event loop running. In somepython environments (as with Jupyter which uses IPython) an asyncio event loop is created for you. In that case youshould use instead theasync usage example.

Async usage

importasynciofromgqlimportClient,gqlfromgql.transport.aiohttpimportAIOHTTPTransportasyncdefmain():# Select your transport with a defined url endpointtransport=AIOHTTPTransport(url="https://countries.trevorblades.com/graphql")# Create a GraphQL client using the defined transportclient=Client(transport=transport)# Provide a GraphQL queryquery=gql("""        query getContinents {          continents {            code            name          }        }    """    )# Using `async with` on the client will start a connection on the transport# and provide a `session` variable to execute queries on this connectionasyncwithclientassession:# Execute the queryresult=awaitsession.execute(query)print(result)asyncio.run(main())

Contributing

SeeCONTRIBUTING.md

License

MIT License


[8]ページ先頭

©2009-2025 Movatter.jp