question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting selectionSet from fieldASTs with external fragment

See original GitHub issue

How can I get fields requested in external fragment from fieldASTs?

Consider following query

`
  query QueryWithFragment {
    todo(_id: "55a624bad009804e552eeea8") {
      ...TextFragment
    }
  }

  fragment TextFragment on Todo {
    text
  }
`

This query results in following, so there is no way to get those fields without directly parsing query string.

{
  "kind": "Field",
  "alias": null,
  "name": {
    "kind": "Name",
    "value": "todo",
    "loc": {
      "start": 27,
      "end": 31,
      "source": {
        "body": "\n  query UseFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ...TextFragment\n    }\n  }\n\n  fragment TextFragment on Todo {\n    text\n  }\n",
        "name": "GraphQL request"
      }
    }
  },
  "arguments": [{
    "kind": "Argument",
    "name": {
      "kind": "Name",
      "value": "_id",
      "loc": {
        "start": 32,
        "end": 35,
        "source": {
          "body": "\n  query UseFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ...TextFragment\n    }\n  }\n\n  fragment TextFragment on Todo {\n    text\n  }\n",
          "name": "GraphQL request"
        }
      }
    },
    "value": {
      "kind": "StringValue",
      "value": "55a624bad009804e552eeea8",
      "loc": {
        "start": 37,
        "end": 63,
        "source": {
          "body": "\n  query UseFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ...TextFragment\n    }\n  }\n\n  fragment TextFragment on Todo {\n    text\n  }\n",
          "name": "GraphQL request"
        }
      }
    },
    "loc": {
      "start": 32,
      "end": 63,
      "source": {
        "body": "\n  query UseFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ...TextFragment\n    }\n  }\n\n  fragment TextFragment on Todo {\n    text\n  }\n",
        "name": "GraphQL request"
      }
    }
  }],
  "directives": [],
  "selectionSet": {
    "kind": "SelectionSet",
    "selections": [{
      "kind": "FragmentSpread",
      "name": {
        "kind": "Name",
        "value": "TextFragment",
        "loc": {
          "start": 76,
          "end": 88,
          "source": {
            "body": "\n  query UseFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ...TextFragment\n    }\n  }\n\n  fragment TextFragment on Todo {\n    text\n  }\n",
            "name": "GraphQL request"
          }
        }
      },
      "directives": [],
      "loc": {
        "start": 73,
        "end": 88,
        "source": {
          "body": "\n  query UseFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ...TextFragment\n    }\n  }\n\n  fragment TextFragment on Todo {\n    text\n  }\n",
          "name": "GraphQL request"
        }
      }
    }],
    "loc": {
      "start": 65,
      "end": 94,
      "source": {
        "body": "\n  query UseFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ...TextFragment\n    }\n  }\n\n  fragment TextFragment on Todo {\n    text\n  }\n",
        "name": "GraphQL request"
      }
    }
  },
  "loc": {
    "start": 27,
    "end": 94,
    "source": {
      "body": "\n  query UseFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ...TextFragment\n    }\n  }\n\n  fragment TextFragment on Todo {\n    text\n  }\n",
      "name": "GraphQL request"
    }
  }
}

Now lets take a look in InlineFragment version

`
  query QueryWithoutFragment {
    todo(_id: "55a624bad009804e552eeea8") {
      ... on Todo {
        text
      }
    }
  }
`

We can easily access requested fields for fragment in selectionSet

{
  "kind": "Field",
  "alias": null,
  "name": {
    "kind": "Name",
    "value": "todo",
    "loc": {
      "start": 36,
      "end": 40,
      "source": {
        "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
        "name": "GraphQL request"
      }
    }
  },
  "arguments": [{
    "kind": "Argument",
    "name": {
      "kind": "Name",
      "value": "_id",
      "loc": {
        "start": 41,
        "end": 44,
        "source": {
          "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
          "name": "GraphQL request"
        }
      }
    },
    "value": {
      "kind": "StringValue",
      "value": "55a624bad009804e552eeea8",
      "loc": {
        "start": 46,
        "end": 72,
        "source": {
          "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
          "name": "GraphQL request"
        }
      }
    },
    "loc": {
      "start": 41,
      "end": 72,
      "source": {
        "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
        "name": "GraphQL request"
      }
    }
  }],
  "directives": [],
  "selectionSet": {
    "kind": "SelectionSet",
    "selections": [{
      "kind": "InlineFragment",
      "typeCondition": {
        "kind": "Name",
        "value": "Todo",
        "loc": {
          "start": 89,
          "end": 93,
          "source": {
            "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
            "name": "GraphQL request"
          }
        }
      },
      "directives": [],
      "selectionSet": {
        "kind": "SelectionSet",
        "selections": [{
          "kind": "Field",
          "alias": null,
          "name": {
            "kind": "Name",
            "value": "text",
            "loc": {
              "start": 104,
              "end": 108,
              "source": {
                "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
                "name": "GraphQL request"
              }
            }
          },
          "arguments": [],
          "directives": [],
          "selectionSet": null,
          "loc": {
            "start": 104,
            "end": 108,
            "source": {
              "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
              "name": "GraphQL request"
            }
          }
        }],
        "loc": {
          "start": 94,
          "end": 116,
          "source": {
            "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
            "name": "GraphQL request"
          }
        }
      },
      "loc": {
        "start": 82,
        "end": 116,
        "source": {
          "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
          "name": "GraphQL request"
        }
      }
    }],
    "loc": {
      "start": 74,
      "end": 122,
      "source": {
        "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
        "name": "GraphQL request"
      }
    }
  },
  "loc": {
    "start": 36,
    "end": 122,
    "source": {
      "body": "\n  query QueryWithoutFragment {\n    todo(_id: \"55a624bad009804e552eeea8\") {\n      ... on Todo {\n        text\n      }\n    }\n  }\n",
      "name": "GraphQL request"
    }
  }
}

I’m currently working on conversion fieldASTs to MongoDB projections. I’ve done with InlineFragment. Any thoughts?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jakepusatericommented, Oct 16, 2016

@Sandreu I made a library that expands on the example above and includes handling for skip and include directives: graphql-list-fields

0reactions
Sandreucommented, Nov 11, 2015

@ansarizafar A solution about what ? Getting the requested fields works with this getFieldList() function, I’m just preventing about corner cases like skip directives usage…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fragments - Apollo GraphQL Docs
A GraphQL fragment is a piece of logic that can be shared between multiple queries and mutations. ... Every fragment includes a subset...
Read more >
Migrating from v3.x to v4.x - GraphQL .NET
GraphQL nullability semantics are enforced on the external AST representation of the data. ... This may be needed to get the parameters of...
Read more >
Graphql.SelectionSet - elm-graphql 5.0.11
Since both single fields and collections of fields are SelectionSet s in dillonkearns/elm-graphql , you can easily pull in sub- SelectionSet s to...
Read more >
GraphQL specification
In this query, the id , firstName , and lastName fields form a selection set. Selection sets may also contain fragment references.
Read more >
How to check requested GraphQL fields in Java with fragments?
getTypeName(); List<Field> fields = selectionSet. ... If like me you use GraphQL SPQR, you can get it with io.leangen.graphql.execution.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found