Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

DSL does not provide expected argument validation

オープン
#355 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
graphql, python
領域
api, backend

調査の方向性

dsl.dsl_gql から始め、この Issue にある countries API の再現を使って gql.gql と動作を比較します。DSL クエリの引数がどのように処理されるかを追跡し、ステップ 5 の不正なフィルターが、フィルターされていない結果を返すのではなく、同等の GraphQLError を発生させることを確認します。

索引モデルが issue の本文から書いたものです。

説明

type: feature

Describe the bug

  • When creating a query with gql.gql(), the query string is checked for invalid arguments, and raises an exception if any are found. (expected behavior)
  • When creating a query with gql.dsl.dsl_gql(), the arguments are not checked, causing unexpected return values. (unexpected behavior)

I've been able to recreate this using the countries api used in the docs.

To Reproduce

Jump to step 5 to see the actual improper behavior.

  1. Set up the transport/client.

    import json
    
    import gql
    from gql.transport.requests import RequestsHTTPTransport as Transport
    from gql import dsl
    
    url = "https://countries.trevorblades.com/"
    
    transport = Transport(url=url)
    client = gql.Client(transport=transport, fetch_schema_from_transport=True)
    
    # Fetch the schema (lemme know if there's a recommended approach for this).
    client.connect_sync()
    client.close_sync()
    ds = dsl.DSLSchema(client.schema)
    
  2. Run a good query using strings.

    good_query_str = gql.gql(
        """
        query {
            continents (filter:{code:{eq:"AN"}}) {
                code
                name
            }
        }
        """
    )
    result = client.execute(good_query_str)
    print(json.dumps(result, indent=2))
    

    Result:

     {
     "continents": [
         {
         "code": "AN",
         "name": "Antarctica"
         }
     ]
     }
    
  3. Run a bad query using strings. The only change here is using 'AN' directly as an argument to code, instead of providing the eq directive.

    bad_query_str = gql.gql(
        """
        query {
            continents (filter:{code:"AN"}) {
                code
                name
            }
        }
        """
    )
    result = client.execute(bad_query_str)
    print(json.dumps(result, indent=2))
    

    Result:

    GraphQLError: Expected value of type 'StringQueryOperatorInput', found "AN".
    
     GraphQL request:3:34
     2 |     query {
     3 |         continents (filter:{code:"AN"}) {
     |                                  ^
     4 |             code
    
  4. Run a good query using DSL.

    good_query_dsl = dsl.dsl_gql(
        dsl.DSLQuery(
            ds.Query.continents(
                filter={
                    'code': {'eq': 'AN'}
                }
            ).select(
                ds.Continent.code,
                ds.Continent.name,
            )
        )
    )
    result = client.execute(good_query_dsl)
    print(json.dumps(result, indent=2))
    

    Result:

     {
     "continents": [
         {
         "code": "AN",
         "name": "Antarctica"
         }
     ]
     }
    
  5. Run a bad query using DSL. Same deal, just remove the 'eq' level of filter specification. Note that the result is an unfiltered response.

    bad_query_dsl = dsl.dsl_gql(
        dsl.DSLQuery(
            ds.Query.continents(
                filter={
                    'code': 'AN'
                }
            ).select(
                ds.Continent.code,
                ds.Continent.name,
            )
        )
    )
    result = client.execute(bad_query_dsl)
    print(json.dumps(result, indent=2))
    

    Result:

     {
     "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"
         }
     ]
     }
    

Expected behavior

Step 5 should raise an equivalent exception to step 3.

System info (please complete the following information):

  • OS: Wins 10
  • Python version: 3.9.12
  • gql version: 3.4.0
  • graphql-core version: 3.2.1
主要言語
Python
スター
1.7k
フォーク
195
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

graphql-python/gql のほかの issue

graphql-python/gql の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。