1 Star 0 Fork 0

麦冬果果 / graphql-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
directives.go 4.64 KB
一键复制 编辑 原始数据 按行查看 历史
package graphql
const (
// Operations
DirectiveLocationQuery = "QUERY"
DirectiveLocationMutation = "MUTATION"
DirectiveLocationSubscription = "SUBSCRIPTION"
DirectiveLocationField = "FIELD"
DirectiveLocationFragmentDefinition = "FRAGMENT_DEFINITION"
DirectiveLocationFragmentSpread = "FRAGMENT_SPREAD"
DirectiveLocationInlineFragment = "INLINE_FRAGMENT"
// Schema Definitions
DirectiveLocationSchema = "SCHEMA"
DirectiveLocationScalar = "SCALAR"
DirectiveLocationObject = "OBJECT"
DirectiveLocationFieldDefinition = "FIELD_DEFINITION"
DirectiveLocationArgumentDefinition = "ARGUMENT_DEFINITION"
DirectiveLocationInterface = "INTERFACE"
DirectiveLocationUnion = "UNION"
DirectiveLocationEnum = "ENUM"
DirectiveLocationEnumValue = "ENUM_VALUE"
DirectiveLocationInputObject = "INPUT_OBJECT"
DirectiveLocationInputFieldDefinition = "INPUT_FIELD_DEFINITION"
)
// DefaultDeprecationReason Constant string used for default reason for a deprecation.
const DefaultDeprecationReason = "No longer supported"
// SpecifiedRules The full list of specified directives.
var SpecifiedDirectives = []*Directive{
IncludeDirective,
SkipDirective,
DeprecatedDirective,
}
// Directive structs are used by the GraphQL runtime as a way of modifying execution
// behavior. Type system creators will usually not create these directly.
type Directive struct {
Name string `json:"name"`
Description string `json:"description"`
Locations []string `json:"locations"`
Args []*Argument `json:"args"`
err error
}
// DirectiveConfig options for creating a new GraphQLDirective
type DirectiveConfig struct {
Name string `json:"name"`
Description string `json:"description"`
Locations []string `json:"locations"`
Args FieldConfigArgument `json:"args"`
}
func NewDirective(config DirectiveConfig) *Directive {
dir := &Directive{}
// Ensure directive is named
err := invariant(config.Name != "", "Directive must be named.")
if err != nil {
dir.err = err
return dir
}
// Ensure directive name is valid
err = assertValidName(config.Name)
if err != nil {
dir.err = err
return dir
}
// Ensure locations are provided for directive
err = invariant(len(config.Locations) > 0, "Must provide locations for directive.")
if err != nil {
dir.err = err
return dir
}
args := []*Argument{}
for argName, argConfig := range config.Args {
err := assertValidName(argName)
if err != nil {
dir.err = err
return dir
}
args = append(args, &Argument{
PrivateName: argName,
PrivateDescription: argConfig.Description,
Type: argConfig.Type,
DefaultValue: argConfig.DefaultValue,
})
}
dir.Name = config.Name
dir.Description = config.Description
dir.Locations = config.Locations
dir.Args = args
return dir
}
// IncludeDirective is used to conditionally include fields or fragments.
var IncludeDirective = NewDirective(DirectiveConfig{
Name: "include",
Description: "Directs the executor to include this field or fragment only when " +
"the `if` argument is true.",
Locations: []string{
DirectiveLocationField,
DirectiveLocationFragmentSpread,
DirectiveLocationInlineFragment,
},
Args: FieldConfigArgument{
"if": &ArgumentConfig{
Type: NewNonNull(Boolean),
Description: "Included when true.",
},
},
})
// SkipDirective Used to conditionally skip (exclude) fields or fragments.
var SkipDirective = NewDirective(DirectiveConfig{
Name: "skip",
Description: "Directs the executor to skip this field or fragment when the `if` " +
"argument is true.",
Args: FieldConfigArgument{
"if": &ArgumentConfig{
Type: NewNonNull(Boolean),
Description: "Skipped when true.",
},
},
Locations: []string{
DirectiveLocationField,
DirectiveLocationFragmentSpread,
DirectiveLocationInlineFragment,
},
})
// DeprecatedDirective Used to declare element of a GraphQL schema as deprecated.
var DeprecatedDirective = NewDirective(DirectiveConfig{
Name: "deprecated",
Description: "Marks an element of a GraphQL schema as no longer supported.",
Args: FieldConfigArgument{
"reason": &ArgumentConfig{
Type: String,
Description: "Explains why this element was deprecated, usually also including a " +
"suggestion for how to access supported similar data. Formatted" +
"in [Markdown](https://daringfireball.net/projects/markdown/).",
DefaultValue: DefaultDeprecationReason,
},
},
Locations: []string{
DirectiveLocationFieldDefinition,
DirectiveLocationEnumValue,
},
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mdgg0816/graphql-go.git
git@gitee.com:mdgg0816/graphql-go.git
mdgg0816
graphql-go
graphql-go
v0.7.4

搜索帮助

344bd9b3 5694891 D2dac590 5694891