2

I'm trying to set up a nice Python client for a GraphQL API managed by a separate team (in a different language): We want to provide useful type hints + autocomplete without introducing overly onerous maintenance requirements for our Python layer.

I saw that gql looks popular and offers a Python DSL for building queries against schemas... So was thinking to take that and:

  1. Use introspection (e.g. here in graphql-core) on the endpoint to fetch a GraphQL Schema Definition Language description
  2. Build that schema (or some transformation of it) into our Python library in an automated pipeline, so we can easily release new versions if/when the schema updates.
  3. Ideally still offer flexibility to escape from the typing system if the library goes stale and there's an urgent need

But just storing the SDL is not enough, because building the gql DSLSchema from this is still dynamic so (at least my, VSCode) IDE has no idea what's going on:

from gql.dsl import DSLQuery, DSLSchema, dsl_gql
from graphql import build_schema

# (File saved with `graphql.print_schema()`)
with open("schema.gql") as f:
    ds = DSLSchema(build_schema(f.read()))

    # ⚠️ IDE autocomplete's not aware of `.me`, `User`, or `.id`:
    query = dsl_gql(
        DSLQuery(
            ds.Query.me.select(ds.User.id)
        )
    )

Is there some common way to tackle this that I'm missing? Or a code generation tool to go from GraphQL SDL to gql.dsl.DSLSchema? We're not necessarily tied to gql yet if another library makes more sense (sgqlc? ariadne-codegen?)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.