Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ldynia opened this issue Jul 9, 2021 · 3 comments
Closed

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

ldynia opened this issue Jul 9, 2021 · 3 comments

Comments

@ldynia
Copy link

ldynia commented Jul 9, 2021

Context

I work with graphql in python (graphql-core-3), and I 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 ?

@Cito
Copy link
Member

Cito commented Jul 9, 2021

You just need to dig one level deeper, like info.field_nodes[0].selection_set.selections[0].selection_set.selections.

@ldynia
Copy link
Author

ldynia commented Jul 12, 2021

Thank @Cito you saved my day :)

By the way is there a simple way to loop down Field Nodes in python, to avoid hard coding indexes such info.field_nodes[0].selection_set.selections[0].selection_set.selections for below example?

cities {
  name
  district {
    name
  }
  state {
    name
    country {
      name
      continent {
        name
      }
    }
  }
}

@Cito
Copy link
Member

Cito commented Jul 13, 2021

Sure, you could do something like this to get to the allLibrary.results in the first example:

node = next(n for n in info.field_nodes if n.name.value == 'allLibrary')
nodes = node.selection_set.selections
node = next(n for n in nodes if n.name.value == 'results')
nodes = node.selection_set.selections

The "next" function is the Pythonic way to find the first field with the specified name.

The function metioned in #136 might be also interesting to you.

Btw, for "normal usage" you don't need to inspect the AST of the query. Maybe you're overcomplicating things?

@Cito Cito closed this as completed Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants