Apollo GraphQL的模拟示例具有以下代码(请参见下文)。如何将模拟的可执行模式传递给Apollo客户端?
有趣的是最后一行 - 它们创建并执行graphql
查询。但是你通常需要创建ApolloClient对象。我无法弄清楚如何做到这一点。
ApolloClient期望NetworkingInterface作为参数而不是可执行模式。
那么,有没有办法从可执行模式创建ApolloClient,没有NetworkingInterface?
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));
有一个开放的公关文档,我仍然需要合并:https://github.com/apollographql/react-docs/pull/172 – stubailo