tfoxy / graphene-django-optimizer

Optimize database access inside graphene queries
MIT License
428 stars 84 forks source link

How to update info.field_nodes[0].selection_set? #71

Closed ldynia closed 3 years ago

ldynia commented 3 years ago

Context

have a GraphQL query that looks like this.

query One {
  library(id: "library_1") {
    name
    book {
      title
      __typename
    }
    __typename
  }
}

When I investigate info object info.field_nodes[0].selection_set.selections I got selection set of FieldNodes that looks like that:

<class 'graphql.language.ast.SelectionSetNode'>
<class 'graphql.pyutils.frozen_list.FrozenList'> [FieldNode at 45:49, FieldNode at 54:95, FieldNode at 100:110]

# FieldNode at 45:49
name

# FieldNode at 54:95
book {
  title
  __typename
}

# FieldNode at 100:110
__typename

Problem

In my second query set I added results field to wrap my data.

query All {
  allLibrary {
    results {
      name
      book {
        title
        __typename
      }
      __typename
    }
    __typename
  }
}

Unfortunately, this causes a problem because query set is resolved from level of allLibrary not library as in the first example. The consequences of this structure is that info.field_nodes[0].selection_set.selections resolves fields incorrectly. Skiping, all the fields that I have interest in (name , book, __typename).

<class 'graphql.language.ast.SelectionSetNode'>
<class 'graphql.pyutils.frozen_list.FrozenList'> [FieldNode at 14:147, FieldNode at 133:143] 

# FieldNode at 31:128
results {
  name
  book {
    title
    __typename
  }
  __typename
}

# FieldNode at 133:143
__typename

Question

How I can fix my info.field_nodes[0].selection_set so it fetches the FieldNode correctly ?

ldynia commented 3 years ago

The answer is here https://github.com/graphql-python/graphql-core/issues/135#issuecomment-877400186