no-backend v1.6.5
What do no-backend :
Transform your database into working GraphQl schema
- Creates Querys and Querys resolvers for all tables
- Creates Mutations and Mutations resolvers for all tables
- Creates Subscriptions and Subscriptions resolvers for all tables
Look at this small example
products table:
id | title | price | category_id |
---|---|---|---|
1 | prod1 | 99 | 1 |
categorys table:
id | title |
---|---|
1 | cat1 |
cli
npm i no-backend graphql
index.js
See more examples with ( Apollo-Server ,GraphQL-Yoga , Apollo-Express and Express )
const { GraphQLServer, PubSub, withFilter } = require("graphql-yoga");
const noBackend = require("no-backend");
const pubsub = new PubSub();
(async () => {
const { typeDefs, resolvers } = await noBackend({
connection: {
driver: "mysql",
host: "localhost",
port: "3306",
user: "root",
password: "gherciu1",
database: "test"
}
});
const server = new GraphQLServer({
typeDefs,
resolvers,
context: req => {
return {
req,
pubsub,
withFilter
};
},
subscriptions: "/"
});
server.start({ port: 3000 }, () => {
console.log("Server is running on http://localhost:3000");
});
})();
open browser on http://localhost:3000
and see the result
Links
With rules
by default all rules is equal to true
await noBackend({
connection:{
...connectionConfig
},
+ rules:{//rules for all tables
+ _read:false,//boolean
+ _delete:(req)=>(req.user),//or a function that return boolean
+ _insert:false,
+ _update:true
+ _exclude:["categorys_shops","categorys"],//exclude a certain table from schema
+
+ products:{//rules for a certain table
+ _read:true,
+ _insert:(req)=>(req.user.id === 1),//function that return boolean
+ _update:true,
+ _delete:true,
+ }
+ }
})
Extend schema (mutations,querys,subscriptions) and resolvers
+ const { GraphQLString,GraphQLList } = require("graphql");
await noBackend({
connection:{
...connectionConfig
},
+ extend: {
+ Query: {
+ hello: { type: GraphQLString },
+ myProducts: ( types ) => { //or a function
+ //types ==> all types used to create the schema (inclusiv input types)
+ return { type: new GraphQLList( types.product ) };
+ }
+ },
+ Mutation: {
+ echo: {
+ type: GraphQLString,
+ args: {
+ value: { type: GraphQLString }
+ }
+ }
+ },
+ Subscription: {
+ onEcho: {
+ type: GraphQLString
+ }
+ },
+ Resolvers: {
+ Query: {
+ hello: () => ("Hello!"),
+ myProducts: async (_, args, { req, pubsub, connection }) => {
+ //connection ==> is equal to ( mysql.createPool({...connectionConfig}) )
+ return await connection.query("SELECT * FROM products");
+ }
+ },
+ Mutation: {
+ echo: (_, args, { req, pubsub, connection }) => {
+ pubsub.publish("echo_topic", { onEcho: args.value });
+ return args.value;
+ }
+ },
+ Subscription: {
+ onEcho: {
+ subscribe: (_, args, { req, pubsub, withFilter, connection }) => pubsub.asyncIterator("echo_topic")
+ }
+ }
+ }
+ }
})
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Or you can sponsor via Open Collective
Author
@Gherciu/no-backend © GHERCIU, Released under the MIT License. Authored and maintained by GHERCIU with help from contributors (list).
If you like this repository star⭐ and watch👀 on GitHub
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago