Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit d0c6bdd

Browse files
committed
Generate InputType
1 parent fb1a39e commit d0c6bdd

30 files changed

+618
-191
lines changed

codegen-for-async-graphql-example/schema.graphql

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Me implements User {
4141
email: String
4242
age: Int
4343
active: Bool
44-
friends: FriendConnection!
44+
friends(first: Int): FriendConnection!
4545
notifications: [Notification]
4646
web: Url
4747
}

codegen-for-async-graphql-example/src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use async_std::task;
77
use models::mutation::Mutation;
88
use models::query::Query;
99
use models::url::Url;
10+
use models::user::User;
1011

1112
#[derive(Debug, Clone, Copy)]
1213
pub struct DataSource {}
@@ -59,7 +60,7 @@ impl DataSource {
5960
pub trait ResolveMutation {
6061
fn create_friend_mutation_resolver(
6162
&self,
62-
id: ID,
63+
input: CreateFriendMutationInput,
6364
) -> FieldResult<models::create_friend_mutation_payload::CreateFriendMutationPayload> {
6465
Ok(models::create_friend_mutation_payload::CreateFriendMutationPayload {})
6566
}
@@ -73,6 +74,7 @@ fn main() {
7374
async fn run(query: &str) -> String {
7475
let data_source = DataSource {};
7576
let schema = Schema::build(Query { active: true }, Mutation, EmptySubscription)
77+
.register_type::<User>()
7678
.data(data_source)
7779
.finish();
7880
let res = schema.execute(query).await;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use async_graphql::*;
2+
#[InputObject]
3+
pub struct CreateFriendMutationInput {
4+
userId: ID,
5+
}

codegen-for-async-graphql-example/src/models/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::ResolveMutation;
22
use crate::DataSource;
3+
pub mod create_friend_mutation_input;
34
pub mod create_friend_mutation_payload;
45
pub mod friend;
56
pub mod friend_connection;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::create_friend_mutation_input::CreateFriendMutationInput;
12
use super::create_friend_mutation_payload::CreateFriendMutationPayload;
23
use super::ResolveMutation;
34
use async_graphql::*;
@@ -7,8 +8,8 @@ impl ResolveMutation for Mutation {}
78
impl Mutation {
89
async fn create_friend_mutation(
910
&self,
10-
body: String,
11+
input: CreateFriendMutationInput,
1112
) -> FieldResult<CreateFriendMutationPayload> {
12-
self.create_friend_mutation_resolver(ID::from("1-1"))
13+
self.create_friend_mutation_resolver(input)
1314
}
1415
}

codegen-for-async-graphql-example/src/models/user.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::friend::Friend;
22
use super::me::Me;
33
use async_graphql::*;
44
#[Interface(field(name = "id", type = "ID"), field(name = "name", type = "String"))]
5-
enum User {
5+
pub enum User {
66
Friend(Friend),
77
Me(Me),
88
}

codegen-for-async-graphql-example/tests/snapshots/introspection.json

+131-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"queryType": {
55
"name": "Query"
66
},
7-
"mutationType": null,
7+
"mutationType": {
8+
"name": "Mutation"
9+
},
810
"subscriptionType": null,
911
"types": [
1012
{
@@ -16,6 +18,31 @@
1618
"enumValues": null,
1719
"possibleTypes": null
1820
},
21+
{
22+
"kind": "OBJECT",
23+
"name": "CreateFriendMutationPayload",
24+
"fields": [
25+
{
26+
"name": "friend",
27+
"args": [],
28+
"type": {
29+
"kind": "NON_NULL",
30+
"name": null,
31+
"ofType": {
32+
"kind": "OBJECT",
33+
"name": "Friend",
34+
"ofType": null
35+
}
36+
},
37+
"isDeprecated": false,
38+
"deprecationReason": null
39+
}
40+
],
41+
"inputFields": null,
42+
"interfaces": [],
43+
"enumValues": null,
44+
"possibleTypes": null
45+
},
1946
{
2047
"kind": "SCALAR",
2148
"name": "Float",
@@ -61,7 +88,13 @@
6188
}
6289
],
6390
"inputFields": null,
64-
"interfaces": [],
91+
"interfaces": [
92+
{
93+
"kind": "INTERFACE",
94+
"name": "User",
95+
"ofType": null
96+
}
97+
],
6598
"enumValues": null,
6699
"possibleTypes": null
67100
},
@@ -280,6 +313,51 @@
280313
}
281314
],
282315
"inputFields": null,
316+
"interfaces": [
317+
{
318+
"kind": "INTERFACE",
319+
"name": "User",
320+
"ofType": null
321+
}
322+
],
323+
"enumValues": null,
324+
"possibleTypes": null
325+
},
326+
{
327+
"kind": "OBJECT",
328+
"name": "Mutation",
329+
"fields": [
330+
{
331+
"name": "createFriendMutation",
332+
"args": [
333+
{
334+
"name": "body",
335+
"type": {
336+
"kind": "NON_NULL",
337+
"name": null,
338+
"ofType": {
339+
"kind": "SCALAR",
340+
"name": "String",
341+
"ofType": null
342+
}
343+
},
344+
"defaultValue": null
345+
}
346+
],
347+
"type": {
348+
"kind": "NON_NULL",
349+
"name": null,
350+
"ofType": {
351+
"kind": "OBJECT",
352+
"name": "CreateFriendMutationPayload",
353+
"ofType": null
354+
}
355+
},
356+
"isDeprecated": false,
357+
"deprecationReason": null
358+
}
359+
],
360+
"inputFields": null,
283361
"interfaces": [],
284362
"enumValues": null,
285363
"possibleTypes": null
@@ -382,6 +460,57 @@
382460
"enumValues": null,
383461
"possibleTypes": null
384462
},
463+
{
464+
"kind": "INTERFACE",
465+
"name": "User",
466+
"fields": [
467+
{
468+
"name": "id",
469+
"args": [],
470+
"type": {
471+
"kind": "NON_NULL",
472+
"name": null,
473+
"ofType": {
474+
"kind": "SCALAR",
475+
"name": "ID",
476+
"ofType": null
477+
}
478+
},
479+
"isDeprecated": false,
480+
"deprecationReason": null
481+
},
482+
{
483+
"name": "name",
484+
"args": [],
485+
"type": {
486+
"kind": "NON_NULL",
487+
"name": null,
488+
"ofType": {
489+
"kind": "SCALAR",
490+
"name": "String",
491+
"ofType": null
492+
}
493+
},
494+
"isDeprecated": false,
495+
"deprecationReason": null
496+
}
497+
],
498+
"inputFields": null,
499+
"interfaces": null,
500+
"enumValues": null,
501+
"possibleTypes": [
502+
{
503+
"kind": "OBJECT",
504+
"name": "Friend",
505+
"ofType": null
506+
},
507+
{
508+
"kind": "OBJECT",
509+
"name": "Me",
510+
"ofType": null
511+
}
512+
]
513+
},
385514
{
386515
"kind": "OBJECT",
387516
"name": "__Directive",

codegen-for-async-graphql-template/src/base/context.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
Config, FileRender, RenderType, RendererInterfaceType, RendererMutationsType,
3-
RendererObjectType, RendererScalarType,
2+
Config, FileRender, RenderType, RendererInputObjectType, RendererInterfaceType,
3+
RendererMutationsType, RendererObjectType, RendererScalarType,
44
};
55
use async_graphql_parser::schema::{Definition, Document, TypeDefinition};
66

@@ -50,9 +50,16 @@ impl<'a> Context<'a> {
5050
.map(RendererMutationsType::file_name)
5151
.collect();
5252

53+
let input_object_type_names: Vec<String> = self
54+
.input_object_types()
55+
.iter()
56+
.map(RendererInputObjectType::file_name)
57+
.collect();
58+
5359
scalar_names.extend(object_type_names);
5460
scalar_names.extend(interface_type_names);
5561
scalar_names.extend(mutation_type_names);
62+
scalar_names.extend(input_object_type_names);
5663
scalar_names
5764
}
5865

@@ -132,4 +139,18 @@ impl<'a> Context<'a> {
132139
})
133140
.collect()
134141
}
142+
143+
#[must_use]
144+
pub fn input_object_types(&self) -> Vec<RendererInputObjectType> {
145+
self.type_definition()
146+
.iter()
147+
.filter_map(|f| match &f {
148+
TypeDefinition::InputObject(f) => Some(RendererInputObjectType {
149+
doc: &f.node,
150+
context: self,
151+
}),
152+
_ => None,
153+
})
154+
.collect()
155+
}
135156
}

codegen-for-async-graphql-template/src/base/generator.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::template::{
2-
generate_interface_type_file, generate_mod_file, generate_mutation_type_file,
3-
generate_object_type_file, generate_object_types_token_stream, generate_scalar_type_file,
1+
use super::{
2+
generate_input_object_type_file, generate_interface_type_file, generate_mod_file,
3+
generate_mutation_type_file, generate_object_type_file, generate_object_types_token_stream,
4+
generate_scalar_type_file,
45
};
56
use async_graphql_parser::parse_schema;
67
use async_graphql_parser::schema::Document;
@@ -33,6 +34,7 @@ pub fn generate_file_from_string(schema: &str, config: &Config) {
3334
generate_interface_type_file(&context);
3435
generate_object_type_file(&context);
3536
generate_mutation_type_file(&context);
37+
generate_input_object_type_file(&context);
3638

3739
generate_mod_file(&context);
3840
}

codegen-for-async-graphql-template/src/base/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
mod config;
22
mod context;
33
mod generator;
4+
mod render_input_value_type;
45
mod render_type;
56
mod renderer_field_type;
7+
mod renderer_input_object_type;
68
mod renderer_interface_type;
79
mod renderer_mutation_type;
810
mod renderer_mutations_type;
911
mod renderer_object_type;
1012
mod renderer_scalar_type;
13+
mod utils;
1114

1215
pub use config::Config;
1316
pub use context::Context;
1417
use generator::{generate_file_from_string, generate_token_from_string};
1518

16-
pub use render_type::{BaseType, Dependency, FileRender, RenderType, SupportField, SupportType};
19+
pub use render_input_value_type::RendererInputValueType;
20+
pub use render_type::{
21+
BaseType, Dependency, FileRender, RenderType, SupportField, SupportFields, SupportType,
22+
SupportTypeName,
23+
};
1724
pub use renderer_field_type::RendererFieldType;
25+
pub use renderer_input_object_type::RendererInputObjectType;
1826
pub use renderer_interface_type::RendererInterfaceType;
1927
pub use renderer_mutation_type::RendererMutationType;
2028
pub use renderer_mutations_type::RendererMutationsType;
@@ -24,7 +32,12 @@ pub use renderer_scalar_type::RendererScalarType;
2432
use proc_macro2::TokenStream;
2533
use std::fs;
2634

27-
use super::snake_case;
35+
use super::template::{
36+
generate_input_object_type_file, generate_interface_type_file, generate_mod_file,
37+
generate_mutation_type_file, generate_object_type_file, generate_object_types_token_stream,
38+
generate_scalar_type_file,
39+
};
40+
use utils::snake_case;
2841

2942
#[must_use]
3043
pub fn generate_token_from_path(path: &str, config: &Config) -> Vec<TokenStream> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use super::Context;
2+
use async_graphql_parser::schema::{InputValue, Type};
3+
4+
use super::{snake_case, RenderType, SupportType, SupportTypeName};
5+
6+
#[derive(Debug, Clone)]
7+
pub struct RendererInputValueType<'a, 'b> {
8+
pub doc: &'a InputValue,
9+
pub context: &'a Context<'b>,
10+
}
11+
12+
impl<'a, 'b> SupportType for RendererInputValueType<'a, 'b> {
13+
fn ty(&self) -> &Type {
14+
&self.doc.ty.node
15+
}
16+
}
17+
18+
impl<'a, 'b> RenderType for RendererInputValueType<'a, 'b> {
19+
#[must_use]
20+
fn name(&self) -> String {
21+
self.doc.name.node.clone()
22+
}
23+
24+
#[must_use]
25+
fn description(&self) -> Option<&String> {
26+
match &self.doc.description {
27+
Some(_f) => panic!("Not Implemented"),
28+
_ => None,
29+
}
30+
}
31+
}
32+
33+
impl<'a, 'b> SupportTypeName for RendererInputValueType<'a, 'b> {
34+
fn context(&self) -> &Context {
35+
self.context
36+
}
37+
}
38+
39+
impl<'a, 'b> RendererInputValueType<'a, 'b> {
40+
#[must_use]
41+
pub fn field_name(&self) -> String {
42+
snake_case(&self.doc.name.node)
43+
}
44+
}

0 commit comments

Comments
 (0)