GraphQL parsing, validation, execution, and native HTTP transport for MoonBit.
///|
let builder : SchemaBuilder[Unit] = SchemaBuilder::from_sdl(
(
#|type Query {
#| hello(name: String = "world"): String!
#|}
),
)
///|
let schema = builder
.resolve("Query", "hello", async fn(_info, args, _ctx) {
let name : String = args.get("name")
GqlString("hello " + name)
})
.finish()///|
let response = execute(
schema,
GraphQLRequest::new("query SayHello($name: String!) { hello(name: $name) }", variables={
"name": InputString("Ada"),
}),
(),
)
///|
let json = response.to_json()///|
let query = @parser.parse_query("query { viewer { id name } }")
///|
let pretty = query.format()
///|
let compact = @parser.minify_query(pretty)
///|
let schema_doc = @parser.parse_schema("type Query { viewer: User }")
///|
let sdl = schema_doc.format(style=@parser.Style::default().with_indent(2))let config = @graphql_http.HttpConfig::default()
@graphql_http.serve(server, schema, _request => (), config~)impl FromGraphQL for Boolimpl FromGraphQL for Intimpl FromGraphQL for Int64impl FromGraphQL for Doubleimpl FromGraphQL for Stringimpl FromGraphQL for Option[T]impl FromGraphQL for Array[T]impl FromGraphQL for Jsonimpl Show for CoercionErrorimpl Show for ExecutionErrorimpl Show for SchemaErrorimpl Show for ValidationErrorpub(all) struct DirectiveSpec {
name : String
arguments : Array[InputValue]
locations : Array[String]
repeatable : Bool
} derive(Eq, Debug)pub(all) struct ExecutableSchema[Ctx] {
registry : TypeRegistry
query_type : String
mutation_type : String?
subscription_type : String?
resolvers : Map[String, async (ResolveInfo, Args, Ctx) -> GqlValue]
subscriptions : Map[String, async (ResolveInfo, Args, Ctx) -> SubscriptionStream]
type_resolvers : Map[String, async (String, GqlValue, Ctx) -> String]
validation : ValidationConfig
}pub(all) struct ExecutionHttpResult {
status_code : Int
response : GraphQLResponse
} derive(Eq, Debug)pub(all) struct GraphQLError {
message : String
path : Array[PathSegment]
locations : Array[SourceLocation]
} derive(Eq, Debug)fn GraphQLError::new(message : String, path? : Array[PathSegment], locations? : Array[SourceLocation]) -> GraphQLErrorpub(all) struct GraphQLRequest {
query : String
operation_name : String?
variables : Map[String, InputValue]
} derive(Eq, Debug)fn GraphQLRequest::new(query : String, operation_name? : String, variables? : Map[String, InputValue]) -> GraphQLRequestpub(all) enum InputValue {
InputNull
InputString(String)
InputBoolean(Bool)
InputInt(Int64)
InputFloat(Double)
InputEnum(String)
InputList(Array[InputValue])
InputObject(Map[String, InputValue])
} derive(Eq, Debug)pub(all) struct ResolveInfo {
type_name : String
field_name : String
response_key : String
path : Array[PathSegment]
source : GqlValue
} derive(Eq, Debug)pub(all) struct SchemaBuilder[Ctx] {
registry : TypeRegistry
query_type : String
mutation_type : String?
subscription_type : String?
resolvers : Map[String, async (ResolveInfo, Args, Ctx) -> GqlValue]
subscriptions : Map[String, async (ResolveInfo, Args, Ctx) -> SubscriptionStream]
type_resolvers : Map[String, async (String, GqlValue, Ctx) -> String]
validation : ValidationConfig
}fn[Ctx] SchemaBuilder::disable_introspection(self : SchemaBuilder[Ctx], disabled : Bool) -> SchemaBuilder[Ctx]fn[Ctx] SchemaBuilder::resolve(self : SchemaBuilder[Ctx], type_name : String, field_name : String, resolver : async (ResolveInfo, Args, Ctx) -> GqlValue) -> SchemaBuilder[Ctx]fn[Ctx] SchemaBuilder::resolve_type(self : SchemaBuilder[Ctx], type_name : String, resolver : async (String, GqlValue, Ctx) -> String) -> SchemaBuilder[Ctx]fn[Ctx] SchemaBuilder::subscribe(self : SchemaBuilder[Ctx], type_name : String, field_name : String, resolver : async (ResolveInfo, Args, Ctx) -> SubscriptionStream) -> SchemaBuilder[Ctx]pub(all) enum TypeDef {
ScalarDef(String?)
ObjectDef(description~ : String?, fields~ : Map[String, FieldDef], interfaces~ : Array[String])
InterfaceDef(description~ : String?, fields~ : Map[String, FieldDef], interfaces~ : Array[String])
UnionDef(description~ : String?, members~ : Array[String])
EnumDef(description~ : String?, values~ : Array[String])
InputObjectDef(description~ : String?, fields~ : Map[String, InputValue])
} derive(Eq, Debug)async fn[Ctx] execute(schema : ExecutableSchema[Ctx], request : GraphQLRequest, context : Ctx) -> GraphQLResponseasync fn[Ctx] execute_with_status(schema : ExecutableSchema[Ctx], request : GraphQLRequest, context : Ctx) -> ExecutionHttpResultasync fn[Ctx] start_subscription(schema : ExecutableSchema[Ctx], request : GraphQLRequest, context : Ctx) -> Result[SubscriptionStream, GraphQLResponse]