GraphQL schema directives to generate CRUD queries, mutations and resolvers which are automatically connected to a database.
MIT License
Note: This package is under active development.
GraphQL schema directives to generate CRUD queries, mutations and resolvers which are automatically connected to a database.
Supported databases:
Database of your choice missing? Adding one is easy - implement the Store interface.
Available directives:
@model
- Generates queries, mutations and resolvers for the annotated type.npm install graphql-crud
or yarn add graphql-crud
.npm install graphql-crud-mongo
or yarn add graphql-crud-mongo
.makeExecutableSchema
to generate the schema.directives.model.store
on the GraphQL context
.import { makeExecutableSchema } from 'graphql-tools';
import { execute } from 'graphql';
import gql from 'graphql-tag';
import crud from 'graphql-crud';
import MongoStore from 'graphql-crud-mongo';
import typeDefs from './typeDefs';
const typeDefs = `
type Author @model {
name: String!
books: [Book]
favoriteBook: Book
}
type Book @model {
name: String!
authors: [Author]
}
type Query {
_: Boolean
}
type Mutation {
_: Boolean
}
`
const schema = makeExecutableSchema({
typeDefs,
schemaDirectives: {
...crud
},
});
const context = {
directives: {
model: {
store: new MongoStore({ connection: 'mongodb://localhost/my-database' }),
},
},
};
execute(
schema,
gql`
mutation {
createAuthor(data: {
name:"Leo Tolstoy"
}) {
id
name
}
}
`
null,
context
);
The above example will generate the following schema with functioning resolvers.
"type Author {
name: String
books: [Book]
id: ID
}
input AuthorInputType {
name: String
books: [BookInputType]
id: ID
}
type Book {
name: String
authors: [Author]
id: ID
}
input BookInputType {
name: String
authors: [AuthorInputType]
id: ID
}
type Mutation {
_: Boolean
createAuthor(data: AuthorInputType): Author
updateAuthor(data: UpdateAuthorInputType, where: UpdateAuthorInputType, upsert: Boolean): Boolean
removeAuthor(where: AuthorInputType): Boolean
createBook(data: BookInputType): Book
updateBook(data: UpdateBookInputType, where: UpdateBookInputType, upsert: Boolean): Boolean
removeBook(where: BookInputType): Boolean
}
type Query {
_: Boolean
author(where: AuthorInputType): Author
authors(where: AuthorInputType): [Author]
book(where: BookInputType): Book
books(where: BookInputType): [Book]
}
input UpdateAuthorInputType {
name: String
books: [UpdateBookInputType]
id: ID
}
input UpdateBookInputType {
name: String
authors: [UpdateAuthorInputType]
id: ID
}
In the repo's root run the following:
docker-compose up -d
to start dependent databases.npm install
or yarn install
In examples/simple
run the following:
npm install; npm start
or yarn install; yarn start
docker-compose up -d
to start database dependencies for testing and the example.npm install
or yarn install
.npm run link:packages
or yarn link:packages
.npm run build:watch
or yarn build:watch