1.6.5 • Published 4 years ago

no-backend v1.6.5

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

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:

idtitlepricecategory_id
1prod1991

categorys table:

idtitle
1cat1

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

no-backend

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

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Or you can sponsor via Open Collective

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

1.6.5

4 years ago

1.6.4

4 years ago

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.9

5 years ago

1.5.8

5 years ago

1.5.7

5 years ago

1.5.6

5 years ago

1.5.5

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.9

5 years ago

1.4.8

5 years ago

1.4.7

5 years ago

1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.9

5 years ago

1.3.8

5 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago