-
Notifications
You must be signed in to change notification settings - Fork 111
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 abstract-typed named fragments #79
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package generate | ||
|
||
// Code relating to generating GoDoc from GraphQL descriptions. | ||
// | ||
// For fields, and types where we just copy the "whole" type (enum and | ||
// input-object), this is easy: we just use the GraphQL description. But for | ||
// struct types, there are often more useful things we can say. | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// descriptionInfo is embedded in types whose descriptions may be more complex | ||
// than just a copy of the GraphQL doc. | ||
type descriptionInfo struct { | ||
// user-specified comment for this type | ||
CommentOverride string | ||
// name of the corresponding GraphQL type | ||
GraphQLName string | ||
// GraphQL description of the type .GraphQLName, if any | ||
GraphQLDescription string | ||
// name of the corresponding GraphQL fragment (on .GraphQLName), if any | ||
FragmentName string | ||
} | ||
|
||
func maybeAddTypeDescription(info descriptionInfo, description string) string { | ||
if info.GraphQLDescription == "" { | ||
return description | ||
} | ||
return fmt.Sprintf( | ||
"%v\nThe GraphQL type's documentation follows.\n\n%v", | ||
description, info.GraphQLDescription) | ||
} | ||
|
||
func fragmentDescription(info descriptionInfo) string { | ||
return maybeAddTypeDescription(info, fmt.Sprintf( | ||
"%v includes the GraphQL fields of %v requested by the fragment %v.", | ||
info.FragmentName, info.GraphQLName, info.FragmentName)) | ||
} | ||
|
||
func structDescription(typ *goStructType) string { | ||
switch { | ||
case typ.CommentOverride != "": | ||
return typ.CommentOverride | ||
case typ.IsInput: | ||
// Input types have all their fields, just use the GraphQL description. | ||
return typ.GraphQLDescription | ||
case typ.FragmentName != "": | ||
return fragmentDescription(typ.descriptionInfo) | ||
default: | ||
// For types where we only have some fields, note that, along with | ||
// the GraphQL documentation (if any). We don't want to just use | ||
// the GraphQL documentation, since it may refer to fields we | ||
// haven't selected, say. | ||
return maybeAddTypeDescription(typ.descriptionInfo, fmt.Sprintf( | ||
"%v includes the requested fields of the GraphQL type %v.", | ||
typ.GoName, typ.GraphQLName)) | ||
} | ||
} | ||
|
||
func interfaceDescription(typ *goInterfaceType) string { | ||
goImplNames := make([]string, len(typ.Implementations)) | ||
for i, impl := range typ.Implementations { | ||
goImplNames[i] = impl.Reference() | ||
} | ||
implementationList := fmt.Sprintf( | ||
"\n\n%v is implemented by the following types:\n\t%v", | ||
typ.GoName, strings.Join(goImplNames, "\n\t")) | ||
|
||
switch { | ||
case typ.CommentOverride != "": | ||
return typ.CommentOverride + implementationList | ||
case typ.FragmentName != "": | ||
return fragmentDescription(typ.descriptionInfo) + implementationList | ||
default: | ||
return maybeAddTypeDescription(typ.descriptionInfo, fmt.Sprintf( | ||
"%v includes the requested fields of the GraphQL interface %v.%v", | ||
typ.GoName, typ.GraphQLName, implementationList)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the current "comment" and "description" terminology a tad confusing. For example, this function returns the struct comment, which comes from the Comment field (if populated), or it generates a "description", which possibly includes the GraphQL description, if one is present.
What do you think about using the term "comment" for the code that generates Go comments, renaming Description to GraphQLDescription, and also renaming Comment to CommentOverride (or something similar)? I'm not quite sure how that jibes with your TODO to include any selection set commit, but I think it reflects how the current code works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I like
CommentOverride
, will do!