Skip to content

Commit

Permalink
fixed error printing list types
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Jul 25, 2019
1 parent 69fb3aa commit 0e7722e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
48 changes: 38 additions & 10 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func PrintQuery(document *ast.QueryDocument) (string, error) {
gVarDefs := []*gAst.VariableDefinition{}

for _, variable := range operation.VariableDefinitions {
varType, err := printerBuildType(variable.Type)
if err != nil {
return "", err
}
gVarDefs = append(gVarDefs, &gAst.VariableDefinition{
Kind: "VariableDefinition",
Variable: &gAst.Variable{
Expand All @@ -88,16 +92,7 @@ func PrintQuery(document *ast.QueryDocument) (string, error) {
Value: variable.Variable,
},
},
Type: &gAst.NonNull{
Kind: "NonNull",
Type: &gAst.Named{
Kind: "Named",
Name: &gAst.Name{
Kind: "Name",
Value: variable.Type.Name(),
},
},
},
Type: varType,
})
}

Expand Down Expand Up @@ -291,6 +286,39 @@ func printerConvertSelectionSet(selectionSet ast.SelectionSet) (*gAst.SelectionS
}, nil
}

func printerBuildType(from *ast.Type) (gAst.Type, error) {
// the final type
var finalType gAst.Type

// start with the inner name
finalType = &gAst.Named{
Kind: "Named",
Name: &gAst.Name{
Kind: "Name",
Value: from.Name(),
},
}

// check if we have a list
if from.Elem != nil {
// wrap the element in a list
finalType = &gAst.List{
Kind: "List",
Type: finalType,
}
}

// check if we are not null
if from.NonNull == true {
finalType = &gAst.NonNull{
Kind: "NonNull",
Type: finalType,
}
}

return finalType, nil
}

func printerBuildValue(from *ast.Value) (gAst.Value, error) {
if from.Kind == ast.Variable {
return &gAst.Variable{
Expand Down
23 changes: 23 additions & 0 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,29 @@ fragment Foo on User {
},
},
},
}, {
`query foo($id: [String]) {
hello
}
`,
&ast.QueryDocument{
Operations: ast.OperationList{&ast.OperationDefinition{
Operation: ast.Query,
Name: "foo",
SelectionSet: ast.SelectionSet{
&ast.Field{
Name: "hello",
},
},
VariableDefinitions: ast.VariableDefinitionList{
&ast.VariableDefinition{
Variable: "id",
Type: ast.ListType(ast.NamedType("String", &ast.Position{}), &ast.Position{}),
},
},
},
},
},
},
// single mutation field
{
Expand Down

0 comments on commit 0e7722e

Please sign in to comment.