graphql-python / graphql-core-legacy

GraphQL base implementation for Python (legacy version – see graphql-core for the current one)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: Using a variable in multiple queries only works for first query

hamidfzm opened this issue · comments

I'm using python graphql 2.2.1 with python 3.7. And here is my query:

query allPhotos($page: Int, $perPage: Int, $sortField: String, $sortOrder: String, $filter: PhotoFilter) {
  items: allPhotos(page: $page, perPage: $perPage, sortField: $sortField, sortOrder: $sortOrder, filter: $filter) {
    id
    photographer_id
    event_id
    source
    thumbnail
    created_at
    updated_at
    __typename
  }
  total: _allPhotosMeta(page: $page, perPage: $perPage, filter: $filter) {
    count
    __typename
  }

}

with the following variable:

{
  "filter": {"userId": "1"}
}

So here is what happens:
filter variable is only populated in allPhotos query and _allPhotosMeta filter variable is None. But if I add a new variable like filter2 equal to filter I get the correct result. This only happens in Python. It's ok in NodeJS.

Can you post a complete test case?

Here is what I did trying to reprpduce it, but it works for me:

from graphql import *
from collections import OrderedDict


filter_type = GraphQLInputObjectType(
    "Filter",
    lambda: OrderedDict(
        [
            ("a", GraphQLInputObjectField(GraphQLString)),
            ("b", GraphQLInputObjectField(GraphQLString))
        ]
    )
)

def resolve_items(root, info, page, filter):
    print("Params for items:", page, filter)
    return ['first', 'second']

def resolve_total(root, info, page, filter):
    print("Params for total:", page, filter)
    return 2


query_type = GraphQLObjectType(
    "Query",
    {
        "all": GraphQLField(
            GraphQLList(GraphQLString),
            {
                "page": GraphQLArgument(GraphQLInt),
                "filter": GraphQLArgument(filter_type)
            },
            resolve_items),
        "total": GraphQLField(
            GraphQLInt,
            {
                "page": GraphQLArgument(GraphQLInt),
                "filter": GraphQLArgument(filter_type)
            },
            resolve_total)
    },
)


schema = GraphQLSchema(query_type)

query = '''
query Test($page: Int, $filter: Filter) {
    items: all(page: $page, filter: $filter),
    count: total(page: $page, filter: $filter)
}
'''

variables = {"page": 1, "filter": {"a": 1, "b": 2}}
result = graphql(schema, query, variable_values=variables)
print(result.data)