Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for named GraphQL operations #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
return err
}
if out.Data != nil {
err := jsonutil.UnmarshalGraphQL(*out.Data, v)
_, dest := deconstructOperation(v)
err := jsonutil.UnmarshalGraphQL(*out.Data, dest)
if err != nil {
// TODO: Consider including response body in returned error, if deemed helpful.
return err
Expand Down
45 changes: 40 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,55 @@ import (
"github.com/shurcooL/graphql/ident"
)

type NamedOperation struct {
Name string
Document interface{}
}

func NewNamedOperation(name string, v interface{}) *NamedOperation {
return &NamedOperation{
Name: name,
Document: v,
}
}

func deconstructOperation(v interface{}) (string, interface{}) {
if named, ok := v.(*NamedOperation); ok {
return named.Name, named.Document
}
return "", v
}

func constructQuery(v interface{}, variables map[string]interface{}) string {
query := query(v)
queryName, queryDoc := deconstructOperation(v)
query := query(queryDoc)

queryPrefix := "query"
if queryName != "" {
queryPrefix = "query " + queryName
}

if len(variables) > 0 {
return "query(" + queryArguments(variables) + ")" + query
return queryPrefix + "(" + queryArguments(variables) + ")" + query
} else if queryName != "" {
return queryPrefix + query
}
return query
}

func constructMutation(v interface{}, variables map[string]interface{}) string {
query := query(v)
mutationName, queryDoc := deconstructOperation(v)
query := query(queryDoc)

mutationPrefix := "mutation"
if mutationName != "" {
mutationPrefix = "mutation " + mutationName
}

if len(variables) > 0 {
return "mutation(" + queryArguments(variables) + ")" + query
return mutationPrefix + "(" + queryArguments(variables) + ")" + query
}
return "mutation" + query
return mutationPrefix + query
}

// queryArguments constructs a minified arguments string for variables.
Expand Down
32 changes: 32 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ func TestConstructQuery(t *testing.T) {
}{},
want: `{viewer{login,createdAt,id,databaseId}}`,
},
{
inV: NewNamedOperation("FindPerson", struct {
Login string
}{}),
inVariables: nil,
want: `query FindPerson{login}`,
},
{
inV: NewNamedOperation("FindPerson", struct {
Login string
}{}),
inVariables: map[string]interface{}{"id": Int(1)},
want: `query FindPerson($id:Int!){login}`,
},
}
for _, tc := range tests {
got := constructQuery(tc.inV, tc.inVariables)
Expand Down Expand Up @@ -262,6 +276,24 @@ func TestConstructMutation(t *testing.T) {
},
want: `mutation($input:AddReactionInput!){addReaction(input:$input){subject{reactionGroups{users{totalCount}}}}}`,
},
{
inV: NewNamedOperation("ThumbsUp", struct {
AddReaction struct {
Name string
} `graphql:"addReaction(input:$input)"`
}{}),
inVariables: nil,
want: `mutation ThumbsUp{addReaction(input:$input){name}}`,
},
{
inV: NewNamedOperation("ThumbsUp", struct {
AddReaction struct {
Name string
} `graphql:"addReaction(input:$input)"`
}{}),
inVariables: map[string]interface{}{"id": Int(1)},
want: `mutation ThumbsUp($id:Int!){addReaction(input:$input){name}}`,
},
}
for _, tc := range tests {
got := constructMutation(tc.inV, tc.inVariables)
Expand Down