From b771a410e69f6911b3e54aa0c1348353fb3fd386 Mon Sep 17 00:00:00 2001 From: meskill <8974488+meskill@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:02:19 +0000 Subject: [PATCH] fix: tailcall generation schema for non required fields --- generated/.tailcallrc.graphql | 16 +++---- generated/.tailcallrc.schema.json | 5 +- src/core/blueprint/operators/graphql.rs | 2 +- src/core/config/directives/graphql.rs | 2 +- .../src/directive_definition.rs | 2 +- .../src/input_definition.rs | 47 ++++++++++--------- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/generated/.tailcallrc.graphql b/generated/.tailcallrc.graphql index 46e48378af..9f6f170ea6 100644 --- a/generated/.tailcallrc.graphql +++ b/generated/.tailcallrc.graphql @@ -71,7 +71,7 @@ directive @graphQL( argument to batch several requests into a single batch request.Make sure you have also specified batch settings to the `@upstream` and to the `@graphQL` operator. """ - batch: Boolean! + batch: Boolean """ Enables deduplication of IO operations to enhance performance.This flag prevents duplicate IO requests from being executed concurrently, reducing resource load. Caution: @@ -269,7 +269,7 @@ directive @link( The source of the link. It can be a URL or a path to a file. If a path is provided, it is relative to the file that imports the link. """ - src: String! + src: String """ The type of the link. It can be `Config`, or `Protobuf`. """ @@ -728,8 +728,8 @@ input Headers { } input Routes { - graphQL: String! - status: String! + graphQL: String + status: String } input ScriptOptions { @@ -772,7 +772,7 @@ Output the telemetry metrics data to prometheus server """ input PrometheusExporter { format: PrometheusFormat - path: String! + path: String } """ @@ -782,7 +782,7 @@ input StdoutExporter { """ Output to stdout in pretty human-readable format """ - pretty: Boolean! + pretty: Boolean } input TelemetryExporter { @@ -793,7 +793,7 @@ input TelemetryExporter { } input Batch { - delay: Int! + delay: Int headers: [String!] maxSize: Int } @@ -816,7 +816,7 @@ input GraphQL { argument to batch several requests into a single batch request.Make sure you have also specified batch settings to the `@upstream` and to the `@graphQL` operator. """ - batch: Boolean! + batch: Boolean """ Enables deduplication of IO operations to enhance performance.This flag prevents duplicate IO requests from being executed concurrently, reducing resource load. Caution: diff --git a/generated/.tailcallrc.schema.json b/generated/.tailcallrc.schema.json index 78840b9bea..ffb4d75c7e 100644 --- a/generated/.tailcallrc.schema.json +++ b/generated/.tailcallrc.schema.json @@ -522,10 +522,7 @@ }, "dedupe": { "description": "Enables deduplication of IO operations to enhance performance.\n\nThis flag prevents duplicate IO requests from being executed concurrently, reducing resource load. Caution: May lead to issues with APIs that expect unique results for identical inputs, such as nonce-based APIs.", - "type": [ - "boolean", - "null" - ] + "type": "boolean" }, "headers": { "description": "The headers parameter allows you to customize the headers of the GraphQL request made by the `@graphQL` operator. It is used by specifying a key-value map of header names and their values.", diff --git a/src/core/blueprint/operators/graphql.rs b/src/core/blueprint/operators/graphql.rs index 4fe3189f2b..fa678fb37b 100644 --- a/src/core/blueprint/operators/graphql.rs +++ b/src/core/blueprint/operators/graphql.rs @@ -84,7 +84,7 @@ pub fn compile_graphql( .map(|req_template| { let field_name = graphql.name.clone(); let batch = graphql.batch; - let dedupe = graphql.dedupe.unwrap_or_default(); + let dedupe = graphql.dedupe; IR::IO(IO::GraphQL { req_template, field_name, batch, dl_id: None, dedupe }) }) } diff --git a/src/core/config/directives/graphql.rs b/src/core/config/directives/graphql.rs index 509c2a0646..f9b74bc07e 100644 --- a/src/core/config/directives/graphql.rs +++ b/src/core/config/directives/graphql.rs @@ -55,5 +55,5 @@ pub struct GraphQL { /// concurrently, reducing resource load. Caution: May lead to issues /// with APIs that expect unique results for identical inputs, such as /// nonce-based APIs. - pub dedupe: Option, + pub dedupe: bool, } diff --git a/tailcall-typedefs-common/src/directive_definition.rs b/tailcall-typedefs-common/src/directive_definition.rs index 908e9087c7..c662130e49 100644 --- a/tailcall-typedefs-common/src/directive_definition.rs +++ b/tailcall-typedefs-common/src/directive_definition.rs @@ -80,7 +80,7 @@ pub fn into_directive_definition( TypeSystemDefinition::Directive(pos(async_graphql::parser::types::DirectiveDefinition { description: description.map(|inner| pos(inner.clone())), name: pos(Name::new(name)), - arguments: into_input_value_definition(&schema), + arguments: into_input_value_definition(schema), is_repeatable: attrs.repeatable, locations: attrs .locations diff --git a/tailcall-typedefs-common/src/input_definition.rs b/tailcall-typedefs-common/src/input_definition.rs index 0d8819c8ef..086722e2d2 100644 --- a/tailcall-typedefs-common/src/input_definition.rs +++ b/tailcall-typedefs-common/src/input_definition.rs @@ -14,27 +14,27 @@ pub trait InputDefinition { } pub fn into_input_definition(schema: SchemaObject, name: &str) -> TypeSystemDefinition { - let description = get_description(&schema); + let description = get_description(&schema).cloned(); TypeSystemDefinition::Type(pos(TypeDefinition { name: pos(Name::new(name)), kind: TypeKind::InputObject(InputObjectType { - fields: into_input_value_definition(&schema), + fields: into_input_value_definition(schema), }), - description: description.map(|inner| pos(inner.clone())), + description: description.map(pos), directives: vec![], extend: false, })) } -pub fn into_input_value_definition(schema: &SchemaObject) -> Vec> { +pub fn into_input_value_definition(schema: SchemaObject) -> Vec> { let mut arguments_type = vec![]; if let Some(subschema) = schema.subschemas.clone() { let list = subschema.any_of.or(subschema.all_of).or(subschema.one_of); if let Some(list) = list { for schema in list { let schema_object = schema.into_object(); - arguments_type.extend(build_arguments_type(&schema_object)); + arguments_type.extend(build_arguments_type(schema_object)); } return arguments_type; @@ -44,22 +44,19 @@ pub fn into_input_value_definition(schema: &SchemaObject) -> Vec Vec> { +fn build_arguments_type(schema: SchemaObject) -> Vec> { let mut arguments = vec![]; - if let Some(properties) = schema - .object - .as_ref() - .map(|object| object.properties.clone()) - { - for (name, property) in properties.into_iter() { + if let Some(obj) = schema.object { + let required = obj.required; + for (name, property) in obj.properties.into_iter() { let property = property.into_object(); let description = get_description(&property); + let nullable = !required.contains(&name); let definition = pos(InputValueDefinition { description: description.map(|inner| pos(inner.to_owned())), name: pos(Name::new(&name)), ty: pos(determine_input_value_type_from_schema( - name, - property.clone(), + name, &property, nullable, )), default_value: None, directives: Vec::new(), @@ -72,7 +69,11 @@ fn build_arguments_type(schema: &SchemaObject) -> Vec Type { +fn determine_input_value_type_from_schema( + mut name: String, + schema: &SchemaObject, + nullable: bool, +) -> Type { first_char_to_upper(&mut name); if let Some(instance_type) = &schema.instance_type { match instance_type { @@ -82,10 +83,10 @@ fn determine_input_value_type_from_schema(mut name: String, schema: SchemaObject | InstanceType::Number | InstanceType::String | InstanceType::Integer => Type { - nullable: false, + nullable, base: BaseType::Named(Name::new(get_instance_type_name(typ))), }, - _ => determine_type_from_schema(name, &schema), + _ => determine_type_from_schema(name, schema), }, SingleOrVec::Vec(typ) => match typ.first().unwrap() { InstanceType::Null @@ -93,14 +94,14 @@ fn determine_input_value_type_from_schema(mut name: String, schema: SchemaObject | InstanceType::Number | InstanceType::String | InstanceType::Integer => Type { - nullable: true, + nullable, base: BaseType::Named(Name::new(get_instance_type_name(typ.first().unwrap()))), }, - _ => determine_type_from_schema(name, &schema), + _ => determine_type_from_schema(name, schema), }, } } else { - determine_type_from_schema(name, &schema) + determine_type_from_schema(name, schema) } } @@ -145,14 +146,16 @@ fn determine_type_from_arr_valid(name: String, array_valid: &ArrayValidation) -> nullable: true, base: BaseType::List(Box::new(determine_input_value_type_from_schema( name, - schema.clone().into_object(), + &schema.clone().into_object(), + false, ))), }, SingleOrVec::Vec(schemas) => Type { nullable: true, base: BaseType::List(Box::new(determine_input_value_type_from_schema( name, - schemas[0].clone().into_object(), + &schemas[0].clone().into_object(), + false, ))), }, }