示例#1
0
func getUserSchema(context *application.MyContext) graphql.Schema {
	var userType = graphql.Fields{
		"user": &graphql.Field{
			Type: graphql.NewObject(
				graphql.ObjectConfig{
					Name: "User",
					Fields: graphql.Fields{
						"id": &graphql.Field{
							Type: graphql.String,
						},
						"name": &graphql.Field{
							Type: graphql.String,
						},
						"password": &graphql.Field{
							Type: graphql.String,
						},
					},
				},
			),
			Args: graphql.FieldConfigArgument{
				"id": &graphql.ArgumentConfig{
					Type: graphql.String,
				},
			},
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				id, isOK := p.Args["id"].(string)

				if isOK {
					var dbConnection = (context.GetDbConnection()).(*mgo.Database)
					var result = user_models.UserStruct{}
					err := dbConnection.C("users").Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&result)
					if err != nil {
						panic(err)
					}
					return result, nil
				}
				return nil, nil
			},
		},
	}
	var schema, _ = graphql.NewSchema(
		graphql.SchemaConfig{
			Query: graphql.NewObject(
				graphql.ObjectConfig{
					Name:   "UserQuery",
					Fields: userType,
				}),
		},
	)
	return schema
}
示例#2
0
func Register(rw http.ResponseWriter, rq *http.Request, context *application.MyContext) {
	username := rq.FormValue("username")
	password := rq.FormValue("password")

	isValid := helpers.ValidateRegister(username, password)

	if !isValid {
		application.ResponseData(rw, nil, http.StatusBadRequest)
		return
	}

	var newPerson = &user_models.UserStruct{Username: strings.ToLower(username), Password: password}
	var dbConnection = (context.GetDbConnection()).(*mgo.Database)
	err := dbConnection.C("users").Insert(newPerson)
	if err != nil {
		panic(err)
	}
	application.ResponseData(rw, newPerson, http.StatusGone)
}