@@ -5,8 +5,10 @@ import queryString from 'query-string';
55import Provider from './Provider' ;
66import Generator from './Generator' ;
77import hocs from '../hocs' ;
8+ import { createEmptyData } from 'canner-helpers' ;
89import { Parser , Traverser } from 'canner-compiler' ;
9-
10+ import { createClient , MemoryConnector } from 'canner-graphql-interface' ;
11+ import { isEmpty , isPlainObject } from 'lodash' ;
1012// i18n
1113import en from 'react-intl/locale-data/en' ;
1214import zh from 'react-intl/locale-data/zh' ;
@@ -83,6 +85,7 @@ class CannerCMS extends React.Component<Props, State> {
8385 result [ key ] = v ;
8486 return result ;
8587 } , { } ) ;
88+ this . client = genClient ( { ...props . schema , schema : schema } ) ;
8689 }
8790
8891 deploy = ( key : string , id ? : string ) : Promise < * > => {
@@ -108,7 +111,7 @@ class CannerCMS extends React.Component<Props, State> {
108111 afterDeploy,
109112 intl = { } ,
110113 hideButtons,
111- schema : { client , storages}
114+ schema : { storages}
112115 } = this . props ;
113116 const { location, push} = history ;
114117 const { pathname} = location ;
@@ -126,7 +129,7 @@ class CannerCMS extends React.Component<Props, State> {
126129 >
127130 < Provider
128131 ref = { provider => this . provider = provider }
129- client = { client }
132+ client = { this . client }
130133 schema = { this . schema }
131134 dataDidChange = { dataDidChange }
132135 afterDeploy = { afterDeploy }
@@ -167,4 +170,47 @@ function compile(schema, visitors) {
167170 return componentTree ;
168171}
169172
173+ export function genClient ( schema ) {
174+ const {
175+ resolvers,
176+ connector,
177+ graphqlClient,
178+ } = schema ;
179+
180+ const options : Object = {
181+ schema : schema . schema
182+ } ;
183+
184+ if ( connector ) {
185+ if ( isPlainObject ( connector ) ) {
186+ if ( ! isEmpty ( connector ) ) {
187+ options . connectors = connector
188+ }
189+ } else {
190+ options . connector = connector ;
191+ }
192+ }
193+
194+ if ( graphqlClient ) {
195+ if ( isPlainObject ( graphqlClient ) ) {
196+ if ( ! isEmpty ( connector ) ) {
197+ options . graphqlClients = graphqlClient ;
198+ }
199+ } else {
200+ options . graphqlClient = graphqlClient ;
201+ }
202+ }
203+
204+ if ( isEmpty ( connector ) && isEmpty ( graphqlClient ) ) {
205+ options . connector = new MemoryConnector ( {
206+ defaultData : createEmptyData ( schema . schema ) . toJS ( )
207+ } ) ;
208+ }
209+
210+ if ( ! isEmpty ( resolvers ) ) {
211+ options . resolvers = resolvers
212+ }
213+ return createClient ( options ) ;
214+ }
215+
170216export default CannerCMS ;
0 commit comments