Skip to main content

Ariadne-Relay provides a toolset for implementing GraphQL servers in Python that conform to the Relay specification, using the Ariadne library.

Project description

Build Status Codecov


Ariadne-Relay

Ariadne-Relay provides a toolset for implementing GraphQL servers in Python that conform to the Relay specification, using the Ariadne library.

The goals of Ariadne-Relay are to:

  • Make building Relay features feel as close as possible to core Ariadne
  • Minimize boilerplate for common cases
  • Make it as easy as possible to fully customize and optimize a Relay deployment

Installation

Ariadne-Relay can be installed with pip:

pip install ariadne-relay

Quickstart

If you are not familiar with Ariadne usage in general, the Araidne docs are the best place to start.

Here's a variation of the Ariadne quickstart as a Relay implementation:

from dataclasses import dataclass

from ariadne import gql, InterfaceType, make_executable_schema
from ariadne.asgi import GraphQL

from ariadne_relay import NodeObjectType, RelayQueryType, resolve_node_query


# Using a dataclass for Person rather than a dict,
# since it works better with a Node implementation
@dataclass
class Person:
    id: int
    firstName: str
    lastName: str
    age: int


type_defs = gql(
    """
    type Query {
        node(id: ID!): Node
        people(
            after: String
            before: String
            first: Int
            last: Int
        ): PeopleConnection!
    }

    interface Node {
        id: ID!
    }

    type PageInfo {
        hasNextPage: Boolean!
        hasPreviousPage: Boolean!
        startCursor: String
        endCursor: String
    }

    type Person implements Node {
        id: ID!
        firstName: String
        lastName: String
        age: Int
        fullName: String
    }

    type PersonEdge {
        cursor: String!
        node: Person
    }

    type PeopleConnection {
        pageInfo: PageInfo!
        edges: [PersonEdge]!
    }
"""
)

# A mock data store of people
people_data = {
    "1": Person(id=1, firstName="John", lastName="Doe", age=21),
    "2": Person(id=2, firstName="Bob", lastName="Boberson", age=24),
}

# Instead of using Ariadne's QueryType, use the Relay-enabled
# RelayQueryType class
query = RelayQueryType()

# resolve_node_query is provided as a resolver for Query.node()
query.set_field("node", resolve_node_query)

# Connection resolvers work exactly like standard Ariadne resolvers,
# except they convert the returned value to a connection structure
@query.connection("people")
def resolve_people(*_):
    return list(people_data.values())


# Define the Node interface
node = InterfaceType("Node")

# Add a Node type resolver
@node.type_resolver
def resolve_node_type(obj, *_):
    return obj.__class__.__name__


# Instead of Ariadne's ObjectType, use the Relay-enabled
# NodeObjectType class for types that implement Node
person = NodeObjectType("Person")


# Add an instance_resolver to define how an instance of
# this type is retrieved, given an id
@person.instance_resolver
def resolve_person_instance(id, *_):
    return people_data.get(id)


@person.field("fullName")
def resolve_person_fullname(person, *_):
    return "%s %s" % (person.firstName, person.lastName)


# Create executable GraphQL schema
schema = make_executable_schema(type_defs, node, query, person)

# Create an ASGI app using the schema, running in debug mode
app = GraphQL(schema, debug=True)

Connection Factories

The heavy lifting of generating a connection structure in a RelayObjectType.connection() is performed by the chosen factory. It is possible to specify a factory of your chosing by passing it in the call to connection():

@query.connection("people", factory=CustomConnection)

ReferenceConnection

The default that is used when factory is not overridden is ReferenceConnection. This implementation wraps graphql_relay.connection_from_array_slice() and provides the expected behavior of the Relay reference implementation.

SnakeCaseConnection

The SnakeCaseConnection factory provides equivalent functionality to ReferenceConnection, but returns a connection structure with snake-case field names. This is useful in conjunction with ariadne.snake_case_fallback_resolvers.

ConnectionProxy

The ConnectionProxy factory can be used to proxy an already-formed connection structure, for example a payload that was produced by an external GraphQL endpoint. It simply passes through the data untouched.

Custom Factories

Many deployments will benefit from customizing the connection factory. One example would be properly integrating a given ORM like Djano. Other examples might be extending the functionality of connections, or customizing how cursors are formed. The BaseConnection and SnakeCaseBaseConnection classes can be useful for this purpose.

Contributing

Please see CONTRIBUTING.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ariadne-relay-0.1.0a8.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

ariadne_relay-0.1.0a8-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file ariadne-relay-0.1.0a8.tar.gz.

File metadata

  • Download URL: ariadne-relay-0.1.0a8.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for ariadne-relay-0.1.0a8.tar.gz
Algorithm Hash digest
SHA256 eaf6fb7e8f6f52d78671ddf643552577c7ad2db95e86ad455899360ba7e0ca4a
MD5 86d7cf1dca8b21002ec1aa64f5e3a2dd
BLAKE2b-256 a6959057511f518dfe8af7ac441afd001757be61abce6452853f674b2fc3ee5b

See more details on using hashes here.

File details

Details for the file ariadne_relay-0.1.0a8-py3-none-any.whl.

File metadata

File hashes

Hashes for ariadne_relay-0.1.0a8-py3-none-any.whl
Algorithm Hash digest
SHA256 ed47549ee6cb494b3aa938d5bb47aba9eacb3c5dab3aaef05feb8705ffe605dd
MD5 60b055b08e29624bb12a7dcbaef217e9
BLAKE2b-256 534cb22f1289108e640c18bf21bebe8d0de3a9c36122f0123a4e93a7e83ad342

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page