GraphQL(3):参数类型与参数传递

1 基本参数类型

(1)基本类型:String,Int,Float,Boolean和ID。可以在shema声明的时候直接使用。

(2)[类型]代表数组,例如:[int]代表整型数组

2 参数传递

(1)和js传递参数一样,小括号内定义形参,但是注意:参数需要定义类型。

(2)!(叹号)代表参数不能为空。

type Query {
    rollDice(numDice: Int!,numSides: Int):[Int]
}

3 代码实例

新建baseType.js

const express = require('express');
const {buildSchema} = require('graphql');
const grapqlHTTP = require('express-graphql').graphqlHTTP;

// 定义schema,查询和类型
const schema = buildSchema(`
    type Query {
        getClassMates(classNo: Int!):[String]
    }
`)

//定义查询对应的处理器
const root ={
    getClassMates({classNo}){
        const obj ={
            31:['张三','李四','王五'],
            61:['张大三','李大四','王大五']
        }
        return obj[classNo];
    }
}

const app = express();
app.use('/graphql', grapqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true
}))
app.listen(3000);

使用命令启动测试。

node baseType.js

访问地址如下:http://localhost:3000/graphql

接口如下:

下面进行测试

(1)不传参数

结果如下,报错提示缺少必填的参数。

(2)传递空值

结果如下,提示该函数类型为int型

(3)传递真实参数

4 自定义参数

GraphQL允许用户自定义参数类型,通常用来描述要获取的资源的属性。

代码如下:

const express = require('express');
const {buildSchema} = require('graphql');
const grapqlHTTP = require('express-graphql').graphqlHTTP;
// 定义schema,查询和类型
const schema = buildSchema(`
    type Account {
        name: String
        age: Int
        sex: String
        department: String
        salary(city: String): Int
    }
    type Query {
        getClassMates(classNo: Int!): [String]
        account(username: String): Account
    }
`)
// 定义查询对应的处理器
const root = {
    getClassMates({ classNo}) {
        const obj = {
            31: ['张三', '李四', '王五'],
            61: ['张大三', '李大四', '王大五']
        }
        return obj[classNo];
    },
    account({ username}) {
        const name = username;
        const sex = 'man';
        const age = 18;
        const department = '开发部';
        const salary = ({city}) => {
            if(city === "北京" || city == "上海" || city == "广州" || city == "深圳") {
                return 10000;
            }
            return 3000;
        }
        return {
            name,
            sex,
            age,
            department,
            salary
        }
    }
}

const app = express();

app.use('/graphql', grapqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true
}))

// 公开文件夹,供用户访问静态资源
app.use(express.static('public'))

app.listen(3000);

结果如下:

不传递salary参数时候,如下:

传递salary参数时候,如下:

相关推荐

  1. androidReact Native之间传递参数

    2024-06-06 13:32:07       29 阅读
  2. springboot参数传递总结

    2024-06-06 13:32:07       16 阅读
  3. React参数传递问题

    2024-06-06 13:32:07       13 阅读
  4. MyBatis 参数传递详解

    2024-06-06 13:32:07       5 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-06 13:32:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-06 13:32:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-06 13:32:07       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-06 13:32:07       20 阅读

热门阅读

  1. 正则表达式

    2024-06-06 13:32:07       6 阅读
  2. Trie字典树和AC自动机(题目)

    2024-06-06 13:32:07       7 阅读
  3. linux防火墙

    2024-06-06 13:32:07       4 阅读
  4. HTML5 新表单元素详解

    2024-06-06 13:32:07       10 阅读
  5. Docker面试整理-Docker 常用命令

    2024-06-06 13:32:07       10 阅读
  6. Vector容器详解

    2024-06-06 13:32:07       6 阅读
  7. 常见的布局方法及优缺点

    2024-06-06 13:32:07       9 阅读
  8. 服务器硬件的基础知识

    2024-06-06 13:32:07       8 阅读
  9. selenium自动化测试入门:下拉框元素定位

    2024-06-06 13:32:07       6 阅读