您可以请求graphql中的文字值吗?
我试图弄清楚当客户端位于服务器之前时如何模拟请求。我希望能够请求文本,这样我就可以稍后返回并更改它们,有什么方法可以这样做吗您可以请求graphql中的文字值吗?,graphql,Graphql,我试图弄清楚当客户端位于服务器之前时如何模拟请求。我希望能够请求文本,这样我就可以稍后返回并更改它们,有什么方法可以这样做吗 query myQuery { type { fieldName: 42 } } 是的,如果您至少设置了服务器样板代码,那么设置模拟响应是非常容易的。如果您使用的是Apollo,则有内置的工具来方便模拟 从文档中: import { makeExecutableSchema, addMockFunctionsToSchema } from 'graph
query myQuery {
type {
fieldName: 42
}
}
是的,如果您至少设置了服务器样板代码,那么设置模拟响应是非常容易的。如果您使用的是Apollo,则有内置的工具来方便模拟 从文档中:
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import { graphql } from 'graphql';
// Fill this in with the schema string
const schemaString = `...`;
// Make a GraphQL schema with no resolvers
const schema = makeExecutableSchema({ typeDefs: schemaString });
// Add mocks, modifies schema in place
addMockFunctionsToSchema({ schema });
const query = `
query tasksForUser {
user(id: 6) { id, name }
}
`;
graphql(schema, query).then((result) => console.log('Got result', result));
GraphQLAPI的强类型特性非常适合自己
嘲笑。这是GraphQL第一次开发的一个重要部分
流程,因为它使前端开发人员能够构建UI
无需等待后端的组件和功能
实施
以下是文档中的一个示例:
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import { graphql } from 'graphql';
// Fill this in with the schema string
const schemaString = `...`;
// Make a GraphQL schema with no resolvers
const schema = makeExecutableSchema({ typeDefs: schemaString });
// Add mocks, modifies schema in place
addMockFunctionsToSchema({ schema });
const query = `
query tasksForUser {
user(id: 6) { id, name }
}
`;
graphql(schema, query).then((result) => console.log('Got result', result));
这个模拟逻辑只是查看您的模式并确保
返回一个字符串,其中您的架构包含一个字符串,一个数字代表一个数字,
等等,这样你就可以得到正确的结果形状。但是如果你想
要使用模拟进行复杂的测试,您可能需要
根据您的特定数据模型自定义它们