0.9.2 • Published 3 years ago

graphity v0.9.2

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

Graphity is a library that makes typescript and GraphQL easy to use. As much as possible, the object of GraphQL.js can be used as it is.

Installation

Currently, Graphity is only responsible for the Schema of GraphQL and can be run through Apollo Server.

npm i graphity apollo-server

Typescript Confituration

set this option in tsconfig.json file of your project.

{
  "experimentalDecorators": true
}

Example

Documents

Let's create a Todo list using Graphity. The minimum unit in Graphity is Entity.

import { Field, GraphityEntity } from "graphity"
import { GraphQLBoolean, GraphQLID, GraphQLNonNull, GraphQLString } from "graphql"


@GraphityEntity({
  description: "todo entity",
})
export class Todo {
  @Field(type => GraphQLID)
  public id!: string

  @Field(type => GraphQLNonNull(GraphQLString), {
    description: "do what you want to do",
  })
  public contents!: string | null

  @Field(type => GraphQLBoolean)
  public isDone!: boolean
}

This entity is converted to a GraphQL Schema:

"""todo entity"""
type Todo {
  id: ID

  """do what you want to do"""
  contents: String!
  isDone: Boolean
}

Now let's create a Resolver that returns Todo Entity. If you create an entire CRUD with an array without a DB:

import {
  GraphQLListOf,
  GraphityResolver,
  listOf,
  Mutation,
  Query
  } from "graphity"
import { GraphQLID, GraphQLNonNull, GraphQLString } from "graphql"
import { Todo } from "../entities/todo"

let increment = 1

@GraphityResolver(type => Todo)
export class TodoResolver {

  public repo: Todo[] = []

  @Query({
    returns: todo => GraphQLListOf(todo),
  })
  public todos() {
    return listOf(this.repo)
  }

  @Query({
    input: {
      id: {type: GraphQLID},
    },
  })
  public todo(parent: null, input: {id: string}) {
    return this.repo.find(({id}) => id === input.id)
  }

  @Mutation({
    input: {
      contents: {
        type: GraphQLString,
      },
    },
  })
  public createTodo(parent: null, input: {contents?: string | null}) {
    const id = increment++
    const todo = Object.assign(new Todo(), {
      id: `${id}`,
      contents: input.contents,
    })
    this.repo.push(todo)
    return todo
  }

  @Mutation({
    input: {
      id: {
        type: GraphQLNonNull(GraphQLID),
      },
      contents: {
        type: GraphQLString,
      },
    },
  })
  public updateTodo(parent: null, input: {id: string, contents?: string | null}) {
    const todo = this.repo.find(({id}) => id === input.id)
    if (!todo) {
      return null
    }
    if (typeof input.contents !== "undefined") {
      todo.contents = input.contents
    }
    return todo
  }

  @Mutation({
    input: {
      id: {
        type: GraphQLNonNull(GraphQLID),
      },
    },
    description: "change 'isDone' to true",
  })
  public doneTodo(parent: null, input: {id: string}) {
    const todo = this.repo.find(({id}) => id === input.id)
    if (!todo) {
      return null
    }
    todo.isDone = true
    return todo
  }

  @Mutation({
    input: {
      id: {
        type: GraphQLNonNull(GraphQLID),
      },
    },
    description: "change 'isDone' to false",
  })
  public undoneTodo(parent: null, input: {id: string}) {
    const todo = this.repo.find(({id}) => id === input.id)
    if (!todo) {
      return null
    }
    todo.isDone = false
    return todo
  }

  @Mutation({
    input: {
      id: {
        type: GraphQLNonNull(GraphQLID),
      },
    },
  })
  public deleteTodo(parent: null, input: {id: string}) {
    const todo = this.repo.find(({id}) => id === input.id)
    if (!todo) {
      return null
    }
    this.repo.splice(this.repo.indexOf(todo), 1)
    return todo
  }
}

Resolver creates Query and Mutation.

type Query {
  todos: ListOfTodo
  todo(id: ID): Todo
}

type Mutation {
  createTodo(contents: String): Todo
  updateTodo(id: ID!, contents: String): Todo

  """change 'isDone' to true"""
  doneTodo(id: ID!): Todo

  """change 'isDone' to false"""
  undoneTodo(id: ID!): Todo
  deleteTodo(id: ID!): Todo
}

type ListOfTodo {
  totalCount: Int!
  nodes: [Todo!]!
}

And on the server, you can do the following:

import { ApolloServer } from "apollo-server"
import { createSchema } from "graphity"
import { TodoResolver } from "./resolvers/todo-resolver"

const app = new Graphity({
  resolvers: [
    TodoResolver,
  ],
})

const server = new ApolloServer({
  schema: app.createSchema(),
  context: ({ req }) => app.createContext(req),
})

server.listen(8888)

License

MIT

0.9.2

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.8.0-alpha.13

3 years ago

0.8.0-alpha.12

3 years ago

0.8.0-alpha.11

3 years ago

0.8.0-alpha.10

3 years ago

0.8.0-alpha.9

3 years ago

0.8.0-alpha.8

3 years ago

0.8.0-alpha.5

3 years ago

0.8.0-alpha.7

3 years ago

0.8.0-alpha.4

3 years ago

0.8.0-alpha.3

3 years ago

0.8.0-alpha.2

4 years ago

0.8.0-alpha.1

4 years ago

0.8.0-alpha.0

4 years ago

0.7.0

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.6.0-alpha.5

4 years ago

0.6.0-alpha.4

4 years ago

0.6.0-alpha.2

5 years ago

0.6.0-alpha.1

5 years ago

0.5.0-alpha.3

5 years ago

0.5.0-alpha.2

5 years ago

0.5.0-alpha.1

5 years ago

0.5.0-alpha.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago