gqlmanage

所属分类:MongoDB
开发工具:GO
文件大小:50KB
下载次数:0
上传日期:2020-08-03 15:10:38
上 传 者sh-1993
说明:  使用MongoDB在golang中编写的完全工作的GraphQL API。功能包括-解析器,数据加载器,订阅,和...
(Fully working GraphQL API written in golang with MongoDB. Features include - resolvers, dataloaders, subscriptions, auth, middleware, filter, redis pub/sub etc.)

文件列表:
.env-example (454, 2020-08-03)
LICENSE (1062, 2020-08-03)
cmd (0, 2020-08-03)
cmd\create_email.go (946, 2020-08-03)
cmd\create_user.go (876, 2020-08-03)
dataloader (0, 2020-08-03)
dataloader\dataloader.go (2989, 2020-08-03)
dataloader\deviceloader_gen.go (5749, 2020-08-03)
dataloader\roomloader_gen.go (5639, 2020-08-03)
go.mod (935, 2020-08-03)
go.sum (32224, 2020-08-03)
gqlgen.yml (2038, 2020-08-03)
graphql (0, 2020-08-03)
graphql\device_resolver.go (453, 2020-08-03)
graphql\errors.go (225, 2020-08-03)
graphql\generated.go (164114, 2020-08-03)
graphql\mutation_resolver.go (4540, 2020-08-03)
graphql\query_resolver.go (2123, 2020-08-03)
graphql\resolver.go (492, 2020-08-03)
graphql\room_resolver.go (387, 2020-08-03)
graphql\subscription_resolver.go (1051, 2020-08-03)
main.go (97, 2020-08-03)
middleware (0, 2020-08-03)
middleware\auth_middleware.go (2600, 2020-08-03)
models (0, 2020-08-03)
models\device.go (1517, 2020-08-03)
models\email.go (382, 2020-08-03)
models\models_gen.go (2162, 2020-08-03)
models\room.go (801, 2020-08-03)
models\user.go (2163, 2020-08-03)
mongodb (0, 2020-08-03)
mongodb\device.go (2405, 2020-08-03)
mongodb\email.go (1611, 2020-08-03)
mongodb\errors.go (74, 2020-08-03)
mongodb\room.go (2220, 2020-08-03)
mongodb\user.go (2306, 2020-08-03)
schema.graphql (2380, 2020-08-03)
... ...

GraphQL API Server with MongoDB ======= ### Introduction Learning [gqlgen](https://github.com/99designs/gqlgen) to build full GraphQL servers with MongoDB. The following project is built to manage multiple devices in multiple rooms. This GraphQL API is designed to be secure and requires the user to login to manage devices. A React Frontend is being built to use this API. The following Golang Project uses: - [MongoDB](https://github.com/mongodb/mongo-go-driver) - [dataloaden](https://github.com/vektah/dataloaden) - [chi](https://github.com/go-chi/chi) - [jwt-go](https://github.com/dgrijalva/jwt-go) ### Instructions Rename [.env-example](.env-example) to .env and setup variables ``` # MongoDB server config MONGODB_URL=mongodb://localhost/ MONGODB_DATABASE=gqlmanage # Server Port PORT=8000 CORS=http://localhost:8000 # JWT Token Settings JWT_SECRET=my_jwt_secret JWT_ISSUER=my_issuer ``` Need to insert first user to access API (TODO) ##### Run server - default port :8000 - GraphQL Playground default url http://localhost:8080 ```bash go run ./main.go # OR go run github.com/scorpionknifes/gqlmanage ``` ### Notes ##### Resolvers Custom resolvers for devices and rooms are created to allow recursive and nested GraphQL calls. Example of a recursive query. ```graphql { rooms{ devices{ room{ devices{ ... } } } } } ``` ##### Authentication Login with username and password to get JWT Token to access other routes. JWT Token has an expire time of 7 days. ```graphql mutation login{ login(input:{ username: "username" password: "password" }){ authToken{ accessToken } } } ``` Example result getting JWT token ```graphql { "data": { "login": { "authToken": { "accessToken": "JWT_TOKEN_HERE" } } } } ``` Use JWT token to authenticate. Add Authorization Bearer token in Http Header ```json { "Authorization": "Bearer JWT_TOKEN_HERE" } ``` ##### Dataloader [Read Here](https://gqlgen.com/reference/dataloaders/) for more info about dataloader in gqlgen. Dataloader solves repeated queries. The dataloader is used in custom resolver for devices. Example of repeated queries - room needs to be called multiple times for each device that exist ```graphql { devices{ room{ ... } } } ``` ##### Subscriptions (with Redis) [Read Subscription with Redis](https://github.com/99designs/gqlgen/issues/846) [Read Passing token using Subscription](https://github.com/99designs/gqlgen/issues/691#issuecomment-503352009) Websocket subscription is secured with jwt by passing the token through "graphql-ws" header. A different jwt extract method is used. Redis pub/sub for running multiple instances. Example of using a jwt token using Apollo Client in ReactJS ```js const subscriptionClient = new SubscriptionClient( "ws://localhost:8000/query", { reconnect: true, }, null, [ "graphql-ws", 'JWT TOKEN' ]) const wsLink = new WebSocketLink(subscriptionClient); ```

近期下载者

相关文件


收藏者