GraphQL接口前端实现(graphql-request)

GraphQL

前端

基于graphql-request三方实现GraphQL在前端应用。
参考:
1.https://github.com/prisma-labs/graphql-request
2.https://graphql.cn/code

1.创建graph_api.js文件,

const {GraphQLClient } = require('graphql-request')
//url:例如http://localhost:port/chantor
const graphQLClient = new GraphQLClient(URL, {})
export function graph_request_test(mutation,variables) {
    return graphQLClient.request(mutation,variables)
}

2.创建index.vue文件

...

<script>
    import {graph_request_test} from '@/api/graph_api'  //graph_api.js的路径
    import {gql} from 'graphql-request'
    
    export default{
        data():{
        	return{
        		stu_id:123,
        		stu_name:"chantor"
    		}
    	},
        methods:{
            query_stu(){
                //根据学生id、姓名,查询学生性别、年龄、院系
            	var query=gql`
					query($stu_id:Int!,$stu_name:String){
						stu_infos(stu_id:$stu_id,stu_name:$stu_name){
							sex
							age
							department
						}
					}`
            	var variables={"stu_id":this.stu_id,"stu_name":this.stu_name}
                
                graph_request_test(query,variables).then(response=>{
                    //todo
                })
                
                }
        }
    }
    
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值