graphql-ts-gen
v1.0.11
Published
You put schema.graphql. It make query generator. Yes, this is query generator generator.
Downloads
15
Readme
What is it?
You put schema.graphql. It make query generator. Yes, this is query generator generator.
How it works?
Check test/expected.ts
please. I will put more description later.
Example
import { ExtractTypeFromGraphQLQuery, Query, Post, User } from "./expected";
const query = Query // {
.addPost(1, Post // post(postId: 1) {
.addId() // id
.addWriter(User // writer {
.addId() // id
.addUsername() // username
) // }
.addComments(Comment // comments {
.addId() // id
) // }
.addCreatedAt() // createdAt
); // }
// }
// Same with ES6 Fetch's option.
const fetchOptions = {
method: 'POST',
headers: {
'content-type': 'application/json',
},
// body: // NO!! You don't have to set body. YOU SHOULDN'T SET BODY YOURSELF!
}
// https://graphql.org/learn/serving-over-http/#response
const { data, errors } = await query.fetch('your-graphql-server-url', fetchOptions);
data.post.id;
data.post.title; // <- typescript error, because you didn't add 'title' on query.
data.post.writer.id;
data.post.writer.username;
// <Scalar - Date>
data.post.createdAt; // data.post.createdAt instanceof Date === true
// <Array>
data.post.comments[0].id
data.post.comments[123123].id
// true
query.toString() === `{
post(postId: 1) {
id
writer {
id
username
}
comments {
id
}
createdAt
}
}`;
const result: GraphQLQueryType<typeof query> = yourQueryResultFromServer;
// No Typescript Compile Error, Yes Intellisense Auto Complete!
result.post;
result.post.id;
result.post.writer;
result.post.writer.id;
result.post.writer.username;
result.post.comments;
result.post.comments[0].id;
result.post.createdAt;