graphql钩子
最后更新于
package customize
import (
"custom-go/pkg/plugins"
"github.com/graphql-go/graphql" # 自行学习该库的用法
)
type Person struct {
Id int `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
var (
personType = graphql.NewObject(graphql.ObjectConfig{
Name: "Person",
Description: "A person in the system",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.Int,
},
"firstName": &graphql.Field{
Type: graphql.String,
},
"lastName": &graphql.Field{
Type: graphql.String,
},
},
})
fields = graphql.Fields{
"person": &graphql.Field{
Type: personType,
Description: "Get Person By ID",
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
_ = plugins.GetGraphqlContext(params)
id, ok := params.Args["id"].(int)
if ok {
testPeopleData := []Person{
{Id: 1, FirstName: "John", LastName: "Doe"},
{Id: 2, FirstName: "Jane", LastName: "Doe"},
}
for _, p := range testPeopleData {
if p.Id == id {
return p, nil
}
}
}
return nil, nil
},
},
}
rootQuery = graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
Custom_schema, _ = graphql.NewSchema(graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)})
)http://{serverAddress}/gqls/${gql-name}/graphql
# ${gql-name}=graphql 数据源名称
# Example:: http://localhost:9992/gqls/Custom/graphql<script >
GraphQLHelixGraphiQL.init({
defaultQuery: undefined,
defaultVariableEditorOpen: undefined,
endpoint: '${graphqlEndpoint}', // 修改为 http://localhost:9992/gqls/Custom/graphql
headers: undefined,
headerEditorEnabled: undefined,
subscriptionsEndpoint: undefined,
useWebSocketLegacyProtocol: undefined,
hybridSubscriptionTransportConfig: undefined,
shouldPersistHeaders: undefined,
});
</script>http://{serverAddress}/gqls/${gql-name}/graphql
# Example:: http://localhost:9992/gqls/Custom/graphql
Content-Type: application/json
# JSON request
{
"query": "IntrospectionQuery content", // 内省QUERY OPERATION,见下文
"variables": {},
"operationName": "IntrospectionQuery"
}
# JSON response
{
// ... graphql schema struct
}query IntrospectionQuery {
__schema {
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type {
...TypeRef
}
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
GraphqlServers: []plugins.GraphQLServerConfig{
{
ApiNamespace: "Custom",
ServerName: "Custom",
EnableGraphQLEndpoint: true,
Schema: customize.Custom_schema,
},
},