as a Field, and I want to insert subsubfield3 in the selection set of subfield2. I can do it using transform with quite a lot of boilerplate code, wondering if it's the only way.
AstPrinter allows to print the Field (and any other Node) into a String. Is the opposite possible? I'd like to parse a String into a Field.I realize that maybe it's not a very common usage of the Field, so let me clarify why I'm asking this: I have different generic GraphQL requests which selectionSet I need to change, and send them to an underlying service. I have access to DataFetchingEnvironment corresponding to these requests, so I retrieve its Field and work with it.
Thanks in advance 🙏 If the functionality is missing but you would consider it useful, I would be able to contribute
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"graphql.language.Field as its name suggests is the AST representation of graphql query text. And yes it can be parsed into a string with some tricks
The parser does NOT start from a syntax element of field but its can be tricked
\n static Field parseField(String sdlField) {\n String spec = \"\"\" query Foo {\n $sdlField\n }\n \"\"\"\n def document = parseQuery(spec)\n def op = document.getDefinitionsOfType(OperationDefinition.class)[0]\n return op.getSelectionSet().getSelectionsOfType(Field.class)[0] as Field\n }\nparseField(\"f(arg : 1) { inner sub selection }\") for example
The thing is Field and graphql.language.SelectionSet is not really a representation of the \"executable notion of a field and its subselection\"
They are the AST syntax elements. For example read https://www.graphql-java.com/blog/deep-dive-merged-fields
\n{\n foo\n foo\n}\nThis is valid AST and will include two graphql.language.Field objects but there is only one runtime field
You need some other representation of a \"field and its sub selection\". With the https://github.com/atlassian-labs/nadel (our Atlassian federated query engine) we use graphql.normalized.ExecutableNormalizedField and graphql.normalized.ExecutableNormalizedOperation - this is a more complete representation of the \"runtime\" when you take out the syntactical elements
You can mutate this structure and then \"print\" a sub document from this to form \"sub operations\"
\nIf you are just wanting to know \"the simple runtime field subselection\" then graphql.schema.DataFetchingEnvironment#getSelectionSet and graphql.schema.DataFetchingFieldSelectionSet is what you want.
See https://www.graphql-java.com/blog/deep-dive-data-fetcher-results
","upvoteCount":1,"url":"https://github.com/graphql-java/graphql-java/discussions/4022#discussioncomment-13567598"}}}-
|
Hello! I have a couple of questions about the usage of
as a
I realize that maybe it's not a very common usage of the Thanks in advance 🙏 If the functionality is missing but you would consider it useful, I would be able to contribute |
Beta Was this translation helpful? Give feedback.
-
|
The parser does NOT start from a syntax element of field but its can be tricked
The thing is Field and They are the AST syntax elements. For example read https://www.graphql-java.com/blog/deep-dive-merged-fields This is valid AST and will include two You need some other representation of a "field and its sub selection". With the https://github.com/atlassian-labs/nadel (our Atlassian federated query engine) we use You can mutate this structure and then "print" a sub document from this to form "sub operations" If you are just wanting to know "the simple runtime field subselection" then See https://www.graphql-java.com/blog/deep-dive-data-fetcher-results |
Beta Was this translation helpful? Give feedback.
graphql.language.Fieldas its name suggests is the AST representation of graphql query text. And yes it can be parsed into a string with some tricksThe parser does NOT start from a syntax element of field but its can be tricked
parseField("f(arg : 1) { inner sub selection }")for exampleThe thing is Field and
graphql.language.SelectionSetis not really a represen…