Apollo GraphQL 的 Mocking 示例具有以下代码(见下文)。
有趣的是最后一行 - 他们创建并执行graphql
查询。但是你通常需要创建 ApolloClient 对象。我不知道该怎么做。
ApolloClient 期望 NetworkingInterface 作为参数而不是可执行模式。
那么,有没有一种方法可以在没有 NetworkingInterface 的情况下从可执行模式创建 ApolloClient?
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));