graphql
graphql copied to clipboard
Create custom directives
Is there a way to create custom directives? I was looking to implement authentication using directives by following this blog post, but could not figure out how to define a custom directive from the source code or godocs.
Here is an example from the blog post.
type Vehicle {
id: ID!
year: Int!
make: String!
model: Int!
askingPrice: Float
costBasis: Float @hasRole(role: “MANAGER”)
numberOfOffers: Int @isAuthenticated
}
It would be nice to be able to define an isAuthenticated directive that will determine whether or not a field should resolve.
Is there currently a way to implement this?
currently, i think graphql-go only supports '''include''' and '''skip''' directives.
@1046102779 do you know if there are any examples of using the include or skip directives in a graphql.ObjectConfig?
Hi guys, any news on this? The example @conord33 provided shows how useful directives are. Looking forward to seeing custom directives in this package.
I'm working on a go version of makeExecutableSchema that might help with this https://github.com/bhoriuchi/graphql-go-tools.
Any news or progress on when graphql-go/graphql is going to support custom directives? I see in the godocs you can create new directives but there's nothing about implementing the logic for them anywhere.
I do not understand why the lib is restrictive about creating custom directives.
// SpecifiedRules The full list of specified directives. var SpecifiedDirectives = []*Directive{ IncludeDirective, SkipDirective, DeprecatedDirective, }
It has been more than 3 years, let's make this happen
any news about this question?
Is there a way to create custom directives? I was looking to implement authentication using directives by following this blog post, but could not figure out how to define a custom directive from the source code or godocs.
Here is an example from the blog post.
type Vehicle { id: ID! year: Int! make: String! model: Int! askingPrice: Float costBasis: Float @hasRole(role: “MANAGER”) numberOfOffers: Int @isAuthenticated }It would be nice to be able to define an
isAuthenticateddirective that will determine whether or not a field should resolve.Is there currently a way to implement this?
I found that, you can get the directive informations in resolve function:
func(p graphql.ResolveParams) (interface{}, error) {
...
for _, iSelection := range p.Info.Operation.GetSelectionSet().Selections {
switch selection := iSelection.(type) {
case *ast.Field:
fmt.Println(selection.Directives[len(selection.Directives)-1].Name.Value)
case *ast.InlineFragment:
case *ast.FragmentSpread:
}
}
...
}
May i have example? Directive os Schema type like @hasRole(role: __)
i try to thing about this all day and i found this solution can use middleware pattern
example product resolve
type Product struct {}
func (m *Product) Resolve(p graphql.ResolveParams) (interface{}, error) {
return "ggg", nil
}
custom go type for short typing ><
type GqlResolver func(p graphql.ResolveParams) (interface{}, error)
create middleware function like this
usePermission := func(role string, next GqlResolver) func(p graphql.ResolveParams) (interface{}, error) {
return func(p graphql.ResolveParams) (interface{}, error) {
if p.Context.Value("role") == nil {
return nil, errors.New("role not found")
}
return next(p)
}
}
use middleware function on graphql resolve
p := Product{}
fields := graphql.Fields{
"product": &graphql.Field{
Type: graphql.String,
Resolve: usePermission("user", p.Resolve),
},
}
i think this solution can do feature like custom directive @hasRole(role: __)
i hope this solution can be good idea for everyone ><
I mentioned this a bit ago, but I have integrated this functionality into https://github.com/bhoriuchi/graphql-go-tools
See test code for example usage. https://github.com/bhoriuchi/graphql-go-tools/blob/master/directives_test.go#L18-L22 https://github.com/bhoriuchi/graphql-go-tools/blob/master/directives_test.go#L47-L66
At its core it uses the same middleware pattern @bakatest-me describes but uses the graphql mechanics