Skip to content

Commit

Permalink
Commit:
Browse files Browse the repository at this point in the history
6741c3192d0c0d5a1f6a9185adcf083b01699935 [6741c31]
Parents:
72e90c0db1
Author:
Hyohyeon Jeong <[email protected]>
Date:
9 February 2016 at 3:16:33 AM SGT
Committer:
Lee Byron <[email protected]>
Commit Date:
9 February 2016 at 4:15:57 PM SGT

Provides a correct OperationType without name in GraphQLPrinter
  • Loading branch information
sogko committed Mar 8, 2016
1 parent 7415cd2 commit fd180fb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
15 changes: 9 additions & 6 deletions language/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,35 +147,38 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
op := node.Operation
name := fmt.Sprintf("%v", node.Name)

defs := wrap("(", join(toSliceString(node.VariableDefinitions), ", "), ")")
varDefs := wrap("(", join(toSliceString(node.VariableDefinitions), ", "), ")")
directives := join(toSliceString(node.Directives), " ")
selectionSet := fmt.Sprintf("%v", node.SelectionSet)
// Anonymous queries with no directives or variable definitions can use
// the query short form.
str := ""
if name == "" {
if name == "" && directives == "" && varDefs == "" && op == "query" {
str = selectionSet
} else {
str = join([]string{
op,
join([]string{name, defs}, ""),
join([]string{name, varDefs}, ""),
directives,
selectionSet,
}, " ")
}
return visitor.ActionUpdate, str
case map[string]interface{}:

op := getMapValueString(node, "Operation")
name := getMapValueString(node, "Name")

defs := wrap("(", join(toSliceString(getMapValue(node, "VariableDefinitions")), ", "), ")")
varDefs := wrap("(", join(toSliceString(getMapValue(node, "VariableDefinitions")), ", "), ")")
directives := join(toSliceString(getMapValue(node, "Directives")), " ")
selectionSet := getMapValueString(node, "SelectionSet")
str := ""
if name == "" {
if name == "" && directives == "" && varDefs == "" && op == "query" {
str = selectionSet
} else {
str = join([]string{
op,
join([]string{name, defs}, ""),
join([]string{name, varDefs}, ""),
directives,
selectionSet,
}, " ")
Expand Down
62 changes: 62 additions & 0 deletions language/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,68 @@ func TestPrinter_PrintsMinimalAST(t *testing.T) {
}
}

// TestPrinter_ProducesHelpfulErrorMessages
// Skipped, can't figure out how to pass in an invalid astDoc, which is already strongly-typed

func TestPrinter_CorrectlyPrintsNonQueryOperationsWithoutName(t *testing.T) {

// Test #1
queryAstShorthanded := `query { id, name }`
expected := `{
id
name
}
`
astDoc := parse(t, queryAstShorthanded)
results := printer.Print(astDoc)

if !reflect.DeepEqual(expected, results) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(results, expected))
}

// Test #2
mutationAst := `mutation { id, name }`
expected = `mutation {
id
name
}
`
astDoc = parse(t, mutationAst)
results = printer.Print(astDoc)

if !reflect.DeepEqual(expected, results) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(results, expected))
}

// Test #3
queryAstWithArtifacts := `query ($foo: TestType) @testDirective { id, name }`
expected = `query ($foo: TestType) @testDirective {
id
name
}
`
astDoc = parse(t, queryAstWithArtifacts)
results = printer.Print(astDoc)

if !reflect.DeepEqual(expected, results) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(results, expected))
}

// Test #4
mutationAstWithArtifacts := `mutation ($foo: TestType) @testDirective { id, name }`
expected = `mutation ($foo: TestType) @testDirective {
id
name
}
`
astDoc = parse(t, mutationAstWithArtifacts)
results = printer.Print(astDoc)

if !reflect.DeepEqual(expected, results) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(results, expected))
}
}

func TestPrinter_PrintsKitchenSink(t *testing.T) {
b, err := ioutil.ReadFile("../../kitchen-sink.graphql")
if err != nil {
Expand Down

0 comments on commit fd180fb

Please sign in to comment.