Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Natoandro committed Sep 25, 2024
1 parent efab4ca commit 255f38e
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 216 deletions.
14 changes: 5 additions & 9 deletions src/metagen/src/client_ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,6 @@ export class QueryGraph extends _QueryGraphBase {{
EffectType::Update | EffectType::Delete | EffectType::Create => "MutationNode",
};

let in_files = fun
.in_files
.iter()
.map(|path| path.0.iter().map(ToString::to_string).collect::<Vec<_>>())
.collect::<Vec<_>>();
let in_files = serde_json::to_string(&in_files)?;

write!(
dest,
r#"
Expand All @@ -207,7 +200,7 @@ export class QueryGraph extends _QueryGraphBase {{
[["{node_name}", nodeMetas.{meta_method}]],
"$q",
)[0];
return new {node_type}(inner, {in_files}) as {node_type}<{out_ty_name}>;
return new {node_type}(inner) as {node_type}<{out_ty_name}>;
}}"#
)?;
}
Expand Down Expand Up @@ -280,7 +273,10 @@ fn render_node_metas(
) -> Result<NameMemo> {
let mut renderer = TypeRenderer::new(
name_mapper.nodes.clone(),
Rc::new(node_metas::TsNodeMetasRenderer { name_mapper }),
Rc::new(node_metas::TsNodeMetasRenderer {
name_mapper,
input_files: manifest.files.clone(),
}),
);
for &id in &manifest.node_metas {
_ = renderer.render(id)?;
Expand Down
20 changes: 18 additions & 2 deletions src/metagen/src/client_ts/node_metas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ use std::fmt::Write;
use common::typegraph::*;

use super::utils::normalize_type_title;
use crate::{interlude::*, shared::types::*};
use crate::{
interlude::*,
shared::{files::ObjectPath, types::*},
};

pub struct TsNodeMetasRenderer {
pub name_mapper: Rc<super::NameMapper>,
pub input_files: Rc<HashMap<u32, Vec<ObjectPath>>>,
}

impl TsNodeMetasRenderer {
Expand Down Expand Up @@ -50,6 +54,7 @@ impl TsNodeMetasRenderer {
ty_name: &str,
return_node: &str,
argument_fields: Option<IndexMap<String, Rc<str>>>,
input_files: Option<String>,
) -> std::fmt::Result {
write!(
dest,
Expand Down Expand Up @@ -79,6 +84,13 @@ impl TsNodeMetasRenderer {
}},"#
)?;
}
if let Some(input_files) = input_files {
write!(
dest,
r#"
inFiles: {input_files},"#
)?;
}
write!(
dest,
r#"
Expand Down Expand Up @@ -132,7 +144,11 @@ impl RenderType for TsNodeMetasRenderer {
};
let node_name = &base.title;
let ty_name = normalize_type_title(node_name).to_pascal_case();
self.render_for_func(renderer, &ty_name, &return_ty_name, props)?;
let input_files = self.input_files.get(&cursor.id).map(|files| {
files.iter().map(|path| path.0.iter().map(ToString::to_string).collect::<Vec<_>>()).collect::<Vec<_>>()
}).unwrap_or_default();
let input_files = (!input_files.is_empty()).then(|| serde_json::to_string(&input_files)).transpose()?;
self.render_for_func(renderer, &ty_name, &return_ty_name, props, input_files)?;
ty_name
}
TypeNode::Object { data, base } => {
Expand Down
55 changes: 19 additions & 36 deletions src/metagen/src/client_ts/static/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function _selectionToNodeSet(
continue;
}

const { argumentTypes, subNodes } = metaFn();
const { argumentTypes, subNodes, inFiles: files } = metaFn();

const nodeInstances = nodeSelection instanceof Alias
? nodeSelection.aliases()
Expand All @@ -37,7 +37,7 @@ function _selectionToNodeSet(
`nested Alias discovored at ${parentPath}.${instanceName}`,
);
}
const node: SelectNode = { instanceName, nodeName };
const node: SelectNode = { instanceName, nodeName, files };

if (argumentTypes) {
// make sure the arg is of the expected form
Expand Down Expand Up @@ -128,14 +128,12 @@ type SelectNode<_Out = unknown> = {
instanceName: string;
args?: NodeArgs;
subNodes?: SelectNode[];
files?: ObjectPath[];
};

export class QueryNode<Out> {
#inner: SelectNode<Out>;
constructor(
inner: SelectNode<Out>,
_files: ObjectPath[],
) {
constructor(inner: SelectNode<Out>) {
this.#inner = inner;
}

Expand All @@ -149,22 +147,13 @@ type EffectiveObjectPath = ("" | `[${number}]` | `.${string}`)[];

export class MutationNode<Out> {
#inner: SelectNode<Out>;
#files: ObjectPath[];
constructor(
inner: SelectNode<Out>,
files: ObjectPath[],
) {
constructor(inner: SelectNode<Out>) {
this.#inner = inner;
this.#files = files;
}

inner() {
return this.#inner;
}

files() {
return this.#files;
}
}

class FileExtractor {
Expand Down Expand Up @@ -273,6 +262,7 @@ type QueryDocOut<T> = T extends
type NodeMeta = {
subNodes?: [string, () => NodeMeta][];
argumentTypes?: { [name: string]: string };
inFiles?: ObjectPath[];
};

/* Selection types section */
Expand Down Expand Up @@ -402,20 +392,18 @@ class GqlBuilder {
#variables = new Map<string, NodeArgValue>();
#files: Map<string, File> = new Map();

#convertQueryNodeGql(
node: SelectNode,
files: ObjectPath[],
) {
#convertQueryNodeGql(node: SelectNode) {
let out = node.nodeName == node.instanceName
? node.nodeName
: `${node.instanceName}: ${node.nodeName}`;

const files = node.files;
const filesByInputKey = new Map<string, ObjectPath[]>();
for (const path of files) {
for (const path of files ?? []) {
const inKey = path[0].slice(1);
const files = filesByInputKey.get(inKey);
if (files) {
files.push(path);
const mapValue = filesByInputKey.get(inKey);
if (mapValue) {
mapValue.push(path);
} else {
filesByInputKey.set(inKey, [path]);
}
Expand All @@ -427,7 +415,6 @@ class GqlBuilder {
Object.entries(args)
.map(([key, val]) => {
const name = `in${this.#variables.size}`;
const files = filesByInputKey.get(key);
const object = { [key]: val.value };
if (files && files.length > 0) {
const extractedFiles = FileExtractor.extractFrom(object, files);
Expand All @@ -446,27 +433,26 @@ class GqlBuilder {

const subNodes = node.subNodes;
if (subNodes) {
// FIXME can't we have file inputs in subnodes?
out = `${out} { ${
subNodes.map((node) => this.#convertQueryNodeGql(node, [])).join(" ")
subNodes.map((node) => this.#convertQueryNodeGql(node)).join(" ")
} }`;
}
return out;
}

static build(
typeToGqlTypeMap: Record<string, string>,
query: Record<string, [SelectNode, ObjectPath[]]>,
query: Record<string, SelectNode>,
ty: "query" | "mutation",
name: string = "",
) {
const builder = new GqlBuilder();

const rootNodes = Object
.entries(query)
.map(([key, [node, files]]) => {
.map(([key, node]) => {
const fixedNode = { ...node, instanceName: key };
return builder.#convertQueryNodeGql(fixedNode, files);
return builder.#convertQueryNodeGql(fixedNode);
})
.join("\n ");

Expand Down Expand Up @@ -607,7 +593,7 @@ export class GraphQLTransport {
Object.fromEntries(
Object.entries(query).map((
[key, val],
) => [key, [(val as QueryNode<unknown>).inner(), []]]),
) => [key, (val as QueryNode<unknown>).inner()]),
),
"query",
name,
Expand All @@ -630,10 +616,7 @@ export class GraphQLTransport {
Object.fromEntries(
Object.entries(query).map((
[key, val],
) => {
const node = val as MutationNode<unknown>;
return [key, [node.inner(), node.files()]];
}),
) => [key, (val as MutationNode<unknown>).inner()]),
),
"mutation",
name,
Expand Down Expand Up @@ -714,7 +697,7 @@ export class PreparedRequest<
[key, val],
) => {
const node = val as MutationNode<unknown>;
return [key, [node.inner(), node.files()]];
return [key, node.inner()];
}),
),
ty,
Expand Down
Loading

0 comments on commit 255f38e

Please sign in to comment.