• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jeffwillette/graphql-go-pets-example: An example of neelance/graphql-go, resolve ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

jeffwillette/graphql-go-pets-example

开源软件地址(OpenSource Url):

https://github.com/jeffwillette/graphql-go-pets-example

开源编程语言(OpenSource Language):

Go 100.0%

开源软件介绍(OpenSource Introduction):

Pets Example

This example is meant to show a full implementation of the server using an SQL datastore.

Starting

start the server, if you don't have the sqlite library already installed from gorm, then it may take a while to compile it

go run *.go

and then visit the GraphiQL dev server at localhost:8080

NOTES:

  • int32 maps to the graphql type Int, so if a number is desired, int32 must be used

  • there are data structs for database models, and there is another struct that is usually in the form of...

    type FooResolver struct {
      db  *DB
      m   Foo
    }
    
  • authentication could go in middleware in the server part

  • authorization could be done with middleware, or from the user in the context after authentication in either the db methods or the resolvers. Official graphql recommends that these go into the data layer, but if a per-field authorization is needed, the field resolvers are the perfect place.

  • edges, connections, used for pagination

    • there is a type
    • it has a connection, which is another type that it is connected to
    • the connection happens through an edge which represents the relationship

Example queries

get user by ids with pets belonging to the user...

{
  getUser(id: 1) {
    name
    pets {
      name
      id
    }
  }
}

get pet by id and owner...

{
 getPet(id: 1) {
  name
  owner {
    name
    id
  }
}
}

insert pet with owner

mutation addPet($userID: Int!, $pet: PetInput!) {
  addPet(userID: $userID, pet: $pet) {
    name
  }
}

# query variables...
{
  "userID": 1,
  "pet": {
    "name": "slkdjsaldkjsalkj"
  }
}

update pet (needs query variables)

mutation UpdatePet($pet: PetInput!) {
  updatePet(pet: $pet) {
    name
    id
    owner {
      name
    }
    tags {
      title
    }
  }

}

delete pet (needs query variables)

mutation DeletePet($userID: Int!, $petID: Int!) {
  deletePet(userID: $userID, petID: $petID) {
    
  }
}

query Pet {
  one: getPet(id: 1) {
    name
    owner {
      name
    }
    tags {
      title
    }
  },
  two: 	getUser(id: 1) {
    name
  }
}

pagination ("after" or "before" can be added with the encoded cursor)

{
  getUser(id: 1) {
    name
    petsConnection(first: 2) {
      totalCount
      edges {
        cursor 
        node {
          name
        }
      }
    }
  }
}



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap