[GraphQL] Deploy a GraphQL dev playground with graphql-up

In this lesson we'll use a simple GraphQL IDL schema to deploy and explore a fully functional GraphQL service in minutes with graphql-up.

 

Install:

npm i -g graphql-up -g

 

Create schema:

type Person {
    id: ID!,
    name: String!,
    tasks: [Task!]! @relation(name: "PersonTask")
}

type Task {
    id: ID!,
    description: String!
    person: Person @relation(name: "PersonTask")
}

 

Run:

graphql-up tasks.schema

 

It will generate two url for use, just grap one. It will open graphcool.

 

We can query the data:

{
    allPersons {
    id,
    name,
    tasks {
      id,
      description
    }
  }
}

We won't get any, because we haven't create anything.

 

Create some mock data:

mutation {
  createPerson(name:"Zhentian") {
    id,
    name
  }
}

// get back
  "data": {
    "createPerson": {
      "id": "cj2t31akybh3g01184klolj0t",
      "name": "Zhentian"
    }
  }
}

 

Now if query again:

{
  allPersons {
    id,
    name,
    tasks {
      id,
      description
    }
  }
}

// get back

  "data": {
    "allPersons": [
      {
        "id": "cj2t31akybh3g01184klolj0t",
        "name": "Zhentian",
        "tasks": []
      }
    ]
  }
}

 

Create data for task:

mutation {
  createTask(description: "Learn GraphQL", personId: "cj2t31akybh3g01184klolj0t") {
    id,
    description
  }
}

// get back
  "data": {
    "createTask": {
      "id": "cj2t37fo7kizn0102kf9otzh5",
      "description": "Learn GraphQL"
    }
  }
}

 

When we do the query again:

{
  allPersons {
    id,
    name,
    tasks {
      id,
      description
    }
  }
}

// get back

  "data": {
    "allPersons": [
      {
        "id": "cj2t31akybh3g01184klolj0t",
        "name": "Zhentian",
        "tasks": [
          {
            "id": "cj2t37fo7kizn0102kf9otzh5",
            "description": "Learn GraphQL"
          }
        ]
      }
    ]
  }
}

 

Create Task and Person in same mutation:

mutation {
  createPerson(name:"Wan", tasks:[
    {description: "Learn Recompose"},
    {description: "Learn SCSS"}
  ]) {
    id,
    name
  }
}

 

After done with playground, can click "Generate code". Select Node env:

Install:

npm install lokka lokka-transport-http --save

Copy the code to index.js file, we should be able to run the code and get data back.

转载于:https://www.cnblogs.com/Answer1215/p/6869972.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值