Skip to content
\n

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.

\n
    \n
  1. 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.
  2. \n
\n

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.

\n

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

\n

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    }\n
\n

parseField(\"f(arg : 1) { inner sub selection }\") for example

\n

The thing is Field and graphql.language.SelectionSet is not really a representation of the \"executable notion of a field and its subselection\"

\n

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}\n
\n

This is valid AST and will include two graphql.language.Field objects but there is only one runtime field

\n

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

\n

You can mutate this structure and then \"print\" a sub document from this to form \"sub operations\"

\n

If you are just wanting to know \"the simple runtime field subselection\" then graphql.schema.DataFetchingEnvironment#getSelectionSet and graphql.schema.DataFetchingFieldSelectionSet is what you want.

\n

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"}}}
Discussion options

You must be logged in to vote

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

    static Field parseField(String sdlField) {
        String spec = """ query Foo {
        $sdlField
        }
        """
        def document = parseQuery(spec)
        def op = document.getDefinitionsOfType(OperationDefinition.class)[0]
        return op.getSelectionSet().getSelectionsOfType(Field.class)[0] as Field
    }

parseField("f(arg : 1) { inner sub selection }") for example

The thing is Field and graphql.language.SelectionSet is not really a represen…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@momentary-lapse
Comment options

Answer selected by momentary-lapse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants