0.0.1-beta.3 ā¢ Published 2 years ago
ts-to-gql v0.0.1-beta.3
TS to GQL - BETA PROJECT
Introduction
Generate graphql types based on your typescript types. DO NOT ADD the schema generated in .gitignore. Schemas are generated at development time and versioned!
š» š Api example here š š»
Example
// ./src/models/post.ts
export type GqlModelPostSelect = {
id: string;
body?: string;
};
// ./src/graphql/resolvers/mutations/post.ts
import { GqlModelPostSelect } from '@/models/Post';
interface CreatePostPayload {
createPostPayload: {
body: string;
};
}
type GqlMutationPost = {
createPost: (
_: unknown,
createPostPayload: CreatePostPayload
) => Promise<GqlModelPostSelect>;
};
export const MutationPostResolver: GqlMutationPost = {
// code resolvers
}
configure ts-to-gql
import { searchGqlSchemaAndBuild } from 'ts-to-gql';
const typeDefs = searchGqlSchemaAndBuild({
isProduction: false,
pathScanProject: './src',
});
this.server = new ApolloServer<MyContext>({
resolvers,
typeDefs,
});
And magic, your types in graphql
type PostSelect {
id: String!
body: String
};
interface CreatePostPayload {
createPostPayload: {
body: string;
};
}
type Mutation {
createPost(createPostPayload: CreatePostPayload): PostSelect!
}
Options searchGqlSchemaAndBuild
command | example | description |
---|---|---|
(required) isProduction | true or false | |
true will use the generated and versioned schema in the source code. false, it will generate a new schema every hot reload (or reload) of your application. | ||
(required) pathScanProject | './src' | path to search models, queries and mutations |
pathSaveSchema | './schema.graphql' | path to save schema |
prefixModel | 'Model' or 'GqlModel' for example | prefix to search models |
prefixMutation | 'Mutation' or 'GqlMutation' for example | prefix to search mutations |
prefixQuery | 'Query' or 'GqlQuery' for example | prefix to find queries |
removePrefixFromSchema | true or false | if true, remove prefix schema in final schema |
fixSchema | (schemaGql: string): string => schemaGql | function to fix schema, add new values or modify existents. Use to add things the library doesn't support for now |
Special types
if necessary use Float, Int, ID or similar, import special types from 'ts-to-gql'. Typescript types like string, number, boolean are automatically converted to String, Number and Boolean respectively.
import { Int, ID, DateTime, Float } from 'ts-to-gql';
Int, Float is string, DateTime is Date, and ID is string
Common errors on migration
Migration is manual, for now.
Not use Partial, extends, implements, or advanced typescript. for now
use types, not interfaces (for now)
ts-to-gql use only second param, define first param or contexts for example
type MutationPost = {
createPost: (input: CreatePost) => Promise<ModePost>;
}
to
type MutationPost = {
createPost: (_: unknown, input: CreatePost) => Promise<ModePost>;
}
- replace your resolver to arrow types, this
type MutationPost = {
createPost(_: unknown, input: CreatePost): Promise<ModePost>;
}
to
type MutationPost = {
createPost: (_: unknown, input: CreatePost) => Promise<ModePost>;
}
- use prefix, for ts-to-gql find your models, queries and mutations (other not necessary)
type MyPost = {}
to
type MutationMyPost = {}