用户工具

站点工具


light4j:graphql

graphql

  • hello:简单示例
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-graphql-4j -o light-example-4j/graphql/helloworld -c model-config/graphql/helloworld/config.json
mvn exec:exec
curl -k -H 'Content-Type:application/json' -X POST https://localhost:8443/graphql -d '{"query":"{ hello }"}'
// {"data":{"hello":"world"}}
// service.yml定义SchemaProvider,handler.yml路由请求
  • starwars:使用Graghql IDL
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-graphql-4j -o light-example-4j/graphql/starwars -m model-config/graphql/starwars/schema.graphqls -c model-config/graphql/starwars/config.json
// 缺少自定义逻辑,运行报错:There is no type resolver defined for interface / union 'Character' type
// SchemaGenerator.makeExecutableSchema(TypeDefinitionRegistry=SchemaParser.parse(schema.graphqls), RuntimeWiring)
  • mutation:修改helloworld
// GraphQLSchema.newSchema().query(queryType).mutation(mutationType).build();
// queryType=GraphQLObjectType.newObject().name('queryType').field(field).build()
// field=GraphQLFieldDefinition.newFieldDefinition.name('numberHolder').dataFetcher(查值).type()
curl -k -H 'Content-Type:application/json' -XPOST https://localhost:8443/graphql -d '{"query":"{ numberHolder { theNumber }}"}'
curl -k -H 'Content-Type:application/json' -XPOST https://localhost:8443/graphql -d '{"query":"mutation { changeTheNumber(newNumber: 4) { theNumber }}"}'
  • mutation-idl:从schema.graphqls生成
schema {
  query: Query
  mutation: Mutation
}
type Query {
  numberHolder: NumberHolder
}
type NumberHolder {
  theNumber: Int
}
type Mutation {
  changeTheNumber(newNumber: Int!): NumberHolder
  failToChangeTheNumber(newNumber: Int): NumberHolder
}
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-graphql-4j -o light-example-4j/graphql/mutation-idl -m model-config/graphql/mutation-idl/mutation-schema.graphqls -c model-config/graphql/mutation-idl/config.json
query { numberHolder { theNumber } }  //numberHolderFetcher、theNumberFetcher
mutation { changeTheNumber(newNumber: 4){ theNumber } } //changeTheNumberFetcher
  • relaytodo:修改mutation,演示relayjs
cd app && npm install && npm start
mvn exec:exec
http://localhost:3000/
  • subscription:演示websocket
mvn exec:exec
cd app && npm i && npm start
http://localhost:3000/channel/1
  • graphql-common,graphql.yml,只处理/graphql和/subscriptions两个路径
  • graphql-validator,ValidatorHandler,只处理GET POST OPTIONS三个方法,解析参数或正文为Map<String, Object>
  • graphql-security,JwtVerifyHandler,
  • graphql-router,GraphqlGetHandler GraphqlPostHandler GraphqlOptionsHandler
  • hello,示例,只需要提供SchemaProvider的实现,并配置graphql提供的handler即可
# service.yml
singletons:
- com.networknt.graphql.router.SchemaProvider:
  - com.networknt.starwars.schema.StarWarsSchema
# handler.yml
handlers:
- com.networknt.graphql.security.JwtVerifyHandler@security
- com.networknt.graphql.validator.ValidatorHandler@validator
- com.networknt.graphql.router.handlers.GraphqlGetHandler@get
- com.networknt.graphql.router.handlers.GraphqlPostHandler@post
- com.networknt.graphql.router.handlers.GraphqlOptionsHandler@options
chains:
  default:
    - exception
    - metrics
    - traceability
    - correlation
    - security
    - validator
paths:
  - path: '/graphql'
    method: 'get'
    exec:
      - default
      - get
  - path: '/graphql'
    method: 'post'
    exec:
      - default
      - post
  - path: '/graphql'
    method: 'options'
    exec:
      - default
      - options
# StarWarsSchema.java
public class StarWarsSchema implements SchemaProvider {
    @Override
    public GraphQLSchema getSchema() {
        GraphQLObjectType queryType = newObject()
                .name("helloWorldQuery")
                .field(newFieldDefinition()
                        .type(GraphQLString)
                        .name("hello")
                        .staticValue("world"))
                .build();

        return GraphQLSchema.newSchema()
                .query(queryType)
                .build();
    }
}
light4j/graphql.txt · 最后更改: 2021/11/26 15:53 由 admin