标签:对象 接口 result name string ogr 定义 重要 def
// 第一步:引入所依赖的库
const { makeExecutableSchema, addMockFunctionsToSchema } = require(‘graphql-tools‘);
const { graphql } = require(‘graphql‘);
// 第二步:定义GraphQL Schema
const schemaString = `
type User {
id: ID,
name: String,
age: Int,
address: String
}
type Query {
user: User
}
`;
// 样例数据
const user = { id: 1, name: ‘zhang‘ };
// 第三步:定义Resolver
const resolvers = {
Query: {
user: () => user
}
};
// 第四步:调用makeExecutableSchema函数生成Schema对象
const schema = makeExecutableSchema({ typeDefs: schemaString, resolvers });
// 第五步:也是重要的一步,调用addMockFunctionsToSchema函数mock Schema
addMockFunctionsToSchema({
schema,
mocks: {
Int: () => 6,
Float: () => 22.1,
String: () => ‘Hello‘
}
});
// 第六步:也是最后一步,查询并验证结果
const query = `
query tasksForUser {
user {
id,
name,
age,
address
}
}
`;
graphql(schema, query).then(result => console.log(‘Got result‘, result));
// 参考资料网址: https://www.apollographql.com/docs/graphql-tools/mocking/
标签:对象 接口 result name string ogr 定义 重要 def
原文地址:https://www.cnblogs.com/popgis/p/11839586.html