Skip to content

Instantly share code, notes, and snippets.

View delvalle's full-sized avatar

Pablo A. Del Valle H. delvalle

View GitHub Profile
@delvalle
delvalle / Resolvers.ts
Created June 3, 2020 04:44
GraphQL/TypeScript
import { Query, Resolver, Arg, Mutation, createUnionType } from 'type-graphql';
import { Abilities, Pokemon, PokemonAbilities, Types } from '../models';
const PokemonAbilitiesUnion = createUnionType({
name: 'PokemonAbilitiesUnion',
types: () => [ Pokemon, PokemonAbilities ] as const,
});
@Resolver()
export class Resolvers {
@delvalle
delvalle / database.ts
Created June 3, 2020 04:41
GraphQL/TypeScript
import { createConnection } from 'typeorm';
import { Abilities, Pokemon, PokemonAbilities, Types } from '../models';
export const init_db = async() => {
const connection = await createConnection();
await connection.dropDatabase();
await connection.synchronize();
// Types
const electric = new Types();
@delvalle
delvalle / Abilities.ts
Created June 3, 2020 04:35
GraphQL/TypeScript
import { Field, ID, ObjectType } from 'type-graphql';
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
@ObjectType()
export class Abilities extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: number | null = null;
@delvalle
delvalle / tsconfig.json
Created June 2, 2020 21:32
GraphQL/TypeScript
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"lib": ["es2018", "esnext.asynciterable"],
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"removeComments": true,
"strict": true,
@delvalle
delvalle / main.py
Last active June 2, 2020 14:46
GraphQL/Python
# /main.py
from books.app import app
from books.database.init_db import init_db
def main():
init_db()
app.run()
@delvalle
delvalle / app.py
Last active June 2, 2020 14:46
GraphQL/Python
# /books/app.py
from flask import Flask
from flask_graphql import GraphQLView
from .database.db_session import db_session
from .schema.schema import schema
app = Flask(__name__)
app.debug = True
@delvalle
delvalle / input_to_dictionary.py
Last active June 2, 2020 14:46
GraphQL/Python
# /books/utils/input_to_dictionary.py
from graphql_relay.node.node import from_global_id
def input_to_dictionary(input):
"""Method to convert Graphene inputs into dictionary"""
dictionary = {}
for key in input:
# Convert GraphQL global id to database id
@delvalle
delvalle / mutation.py
Last active June 2, 2020 14:47
GraphQL/Python
# /books/schema/mutation.py
import graphene
from ..database.db_session import db_session
from ..models.books import Books as BooksModel
from ..types.books import Books, CreateBookInput
from ..utils.input_to_dictionary import input_to_dictionary
@delvalle
delvalle / base.py
Last active June 2, 2020 14:47
GraphQL/Python
# /books/database/base.py
from sqlalchemy.ext.declarative import declarative_base
from .db_session import db_session
Base = declarative_base()
Base.query = db_session.query_property()
@delvalle
delvalle / books.py
Last active June 2, 2020 14:48
GraphQL/Python
# /books/types/books.py
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from ..models.books import Books as BooksModel
class Books(SQLAlchemyObjectType):
class Meta: