Skip to content

A printer? #126

Closed
Closed

Description

@dminkovsky

Hi,

I didn't see a printer in the repository anywhere, something like https://github.com/graphql/graphql-js/blob/master/src/language/printer.js.

So I wrote one: https://gist.github.com/dminkovsky/b5efd2df4a13c6c89a01c5c49fbdffc8#file-graphqlprinter-java

I haven't fully tested this. But the AST I've needed to print has printed correctly. Thought maybe to contribute. Are there other plans for a printer?

Regarding the implementation/style, the way it is now, it could use some double dispatch with Node. But the big switch works. Also, I considered/attempted/aborted writing a reducing visitor as in LB's JS implementation, but the approach I took was just much simpler for me.

Activity

  1. yrashk commented on May 6, 2016

    @yrashk
    Contributor

    I think this would be a very valuable addition as it helps figuring out what went wrong with the design of the schema much faster than reading a JSON representation of the introspection query result.

  2. danielkwinsor commented on May 21, 2016

    @danielkwinsor
    Contributor

    For printing, I set a jackson mapper to indent and let it do the work on the json introspection:

        public static ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
        public static String prettyPrint(Map<String, Object> resultMap) {
            try {
                return mapper.writeValueAsString(resultMap);
            }
            catch (JsonProcessingException e) {
                e.printStackTrace();
                return resultMap.toString();
            }
        }
    

    Looks like

    {
      "testObj" : {
        "strings" : [ "asdf", "2", "test" ],
        "ints" : [ 0 ],
        "objects" : [ {
          "objName" : "name",
    

    How is the output of your printer different from the json introspection result? Can you show an example output?

  3. dminkovsky commented on May 21, 2016

    @dminkovsky
    ContributorAuthor

    @danielkwinsor this printer is for printing GraphQL AST Java Objects (graphql.language) into a GraphQL string. The use case for me is to manipulate input GraphQL AST and then print it back out into GraphQL string as a request to an upstream service.

  4. dminkovsky commented on Aug 17, 2016

    @dminkovsky
    ContributorAuthor

    @yrashk this is actually not a schema printer. This is an AST node printer. A schema printer is needed, too though, for the purpose you mention. Should also be pretty simple, but with tests.

  5. dminkovsky commented on Aug 17, 2016

    @dminkovsky
    ContributorAuthor

    Closing this for now. Can revisit with pull request.

  6. dminkovsky commented on Aug 17, 2016

    @dminkovsky
    ContributorAuthor

    Re-opening because we definitely want a printer. Just want it to be tested and on a PR.

  7. aschrijver commented on Aug 17, 2016

    @aschrijver
    Contributor

    @dminkovsky Your gist would be best. The ObjectMapper approach would be opinionated, leading to a Jackson dependency, just one of the popular Json parsers around.
    While I am using Jackson myself in most of my projects, you would probably be forcing this on, say 50 to 60% (just guessing) of developers using other json libraries.

    (Only problem is its Java 8, now turn the rest into 8 as well 😉)

  8. andimarek commented on Aug 18, 2016

    @andimarek
    Member

    A direct jackson dependency is not an option, imho: We should add only dependencies we really need or want.

  9. dminkovsky commented on Aug 18, 2016

    @dminkovsky
    ContributorAuthor

    I'm still confused regarding the mentions of JSON and specifically Jackson here. This printer produces GraphQL Documents from AST nodes. Is JSON relevant to this?

  10. aschrijver commented on Aug 18, 2016

    @aschrijver
    Contributor

    I just reacted on @danielkwinsor code snippet which used Jackson ObjectMapper. Json has not much to do with it except Jackson started originally as a json library (can now handle many formats)

  11. zhbrass commented on Sep 14, 2016

    @zhbrass

    I modified the gist by @dminkovsky to use pre-Java 8 features. Feel free to use.
    https://gist.github.com/zhbrass/1fd7fbabad852a323740682065708b1b

  12. bbakerman commented on Apr 21, 2017

    @bbakerman
    Member

    The PR here now adds a pretty printer for reverse decompiling a schema

    #386

  13. dminkovsky commented on Apr 24, 2017

    @dminkovsky
    ContributorAuthor

    Makes sense to close this then!

  14. RusticFlare commented on Sep 14, 2017

    @RusticFlare
    Contributor

    (Because I still had to go looking for this)

    You can print by using the SchemaPrinter class.

    SchemaPrinter schemaPrinter = new SchemaPrinter();
    schemaPrinter.print(schema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions