用户工具

站点工具


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
light4j/graphql.txt · 最后更改: 2020/08/20 00:07 由 admin