在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Shopify/graphql_java_gen开源软件地址(OpenSource Url):https://github.com/Shopify/graphql_java_gen开源编程语言(OpenSource Language):Java 52.9%开源软件介绍(OpenSource Introduction):GraphQLJavaGenGenerate code for a specific GraphQL schema that provides query builders and response classes. InstallationThe code generator requires ruby version 2.1 or later. It is recommended to use bundler to install the code generators ruby package. Add this line to your application's Gemfile: gem 'graphql_java_gen' And then execute:
The generated code depends on the com.shopify.graphql.support java
package. This can be added to a gradle project by adding the following
dependancy to you
UsageMonolith (single file) schema generationCreate a script that generates the code for a GraphQL API require 'graphql_java_gen'
require 'graphql_schema'
require 'json'
introspection_result = File.read("graphql_schema.json")
schema = GraphQLSchema.new(JSON.parse(introspection_result))
GraphQLJavaGen.new(schema,
package_name: "com.example.MyApp",
nest_under: 'ExampleSchema',
version: '2020-01',
custom_scalars: [
GraphQLJavaGen::Scalar.new(
type_name: 'Decimal',
java_type: 'BigDecimal',
deserialize_expr: ->(expr) { "new BigDecimal(jsonAsString(#{expr}, key))" },
imports: ['java.math.BigDecimal'],
),
]
).save("#{Dir.pwd}/../MyApp/src/main/java/com/example/MyApp/ExampleSchema.java") The generated Granular (multiple file) schema generationYou may also generate separate class files for each schema entity using the e.g. require 'graphql_java_gen'
require 'graphql_schema'
require 'json'
introspection_result = File.read("graphql_schema.json")
schema = GraphQLSchema.new(JSON.parse(introspection_result))
GraphQLJavaGen.new(schema,
package_name: "com.example.MyApp",
nest_under: 'ExampleSchema',
version: '2020-01',
custom_scalars: [
GraphQLJavaGen::Scalar.new(
type_name: 'Decimal',
java_type: 'BigDecimal',
deserialize_expr: ->(expr) { "new BigDecimal(jsonAsString(#{expr}, key))" },
imports: ['java.math.BigDecimal'],
),
]
).save_granular("#{Dir.pwd}/../MyApp/src/main/java/com/example/MyApp/") When using the granular option, the Generated codeRegardless of whether you use the monolith or granular schema generator, the usage is largely the same.
The only difference depends on whether you access the schema entities directly from the provided package
or as nested, static classes on the That generated code depends on the com.shopify.graphql.support package. The generated code includes a query builder that can be used to create a GraphQL query in a type-safe manner. String queryString = ExampleSchema.query(query -> query
.user(user -> user
.firstName()
.lastName()
)
).toString(); The generated code also includes response classes that will deserialize the response and provide methods for accessing the field data with it already coerced to the correct type. ExampleSchema.QueryResponse response = ExampleSchema.QueryResponse.fromJson(responseJson);
if (!response.getErrors().isEmpty()) {
for (Error error : response.getErrors()) {
System.err.println("GraphQL error: " + error.message());
}
}
if (data != null) {
ExampleSchema.User user = data.getUser();
System.out.println("user.firstName() + " " + user.lastName());
} Lambda ExpressionsThe java 8 lambda expressions feature is essential for having a nice syntax for the query builder. This feature is also available for Android apps by using the Jack toolchain. If an older version of java must be used, then retrolambda can be used to backport this feature to java 5-7. DevelopmentAfter checking out the repo, run To install this gem onto your local machine, run ContributingSee our contributing guidelines for more information. LicenseThe gem is available as open source under the terms of the MIT License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论