【发布时间】:2018-07-23 08:26:50
【问题描述】:
我希望能够在客户端模拟一些查询,这样我就不必提供 GraphQL 端点来处理我的 React 应用程序。
根据 Apollo 文档,看起来我应该使用 apollo-link-schema。我尝试按照示例进行操作,但在我的情况下,我最终遇到错误:Network error: Expected undefined to be a GraphQL schema. 在尝试访问包装组件内的查询结果时。
有人可以帮我理解我在这里做错了什么吗?
这是一个完整的 index.js 示例来说明我的问题:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { ApolloProvider } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import { SchemaLink } from "apollo-link-schema";
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloCache } from 'apollo-cache';
import gql from 'graphql-tag';
import { graphql } from 'react-apollo';
import { makeExecutableSchema, addMockFunctionsToSchema, MockList } from 'graphql-tools';
const typeDefs = `
type Query {
me: Person!
}
type Person {
name: String!
}
`;
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });
const schemaLink = new SchemaLink(schema);
const client = new ApolloClient({
link: schemaLink,
cache: new InMemoryCache()
});
class App extends Component {
render() {
if (this.props.query && this.props.query.loading) {
return <div>Loading...</div>
}
if (this.props.query && this.props.query.error) {
return <div>Error: {this.props.query.error.message}</div>
}
const person = this.props.query.me;
return <div> {person.name} </div>
}
}
const personQuery = gql`
query PersonQuery {
me {
name
}
}
`;
App = graphql(personQuery, { name: 'query' })(App);
ReactDOM.render(
<ApolloProvider client={client}>
< App />
</ApolloProvider>
, document.getElementById('root')
);
【问题讨论】:
标签: graphql apollo react-apollo apollo-client