init apollo server side client state too, better naming

This commit is contained in:
eric-burel 2018-11-22 10:10:29 +01:00
parent c08df1a36f
commit 0d5047fca4
8 changed files with 94 additions and 93 deletions

View file

@ -40,84 +40,82 @@ const clearQuery = gql`
}
`;
if (Meteor.isClient) {
// init the flash message state
registerStateLinkDefault({
name: 'flashMessages',
defaultValue: []
});
// mutations (equivalent to reducers)
registerStateLinkMutation({
name: 'flashMessagesFlash',
mutation: (obj, args, context, info) => {
// get relevant values from args
const { cache } = context;
const { content } = args;
// retrieve current state
const currentFlashMessages = cache.readData({ query: getMessagesQuery });
// transform content
const flashType = content && typeof content.type !== 'undefined' ? content.type : 'error';
const _id = currentFlashMessages.length;
const flashMessage = {
_id,
...content,
type: flashType,
seen: false,
show: true
};
// const { } = obj // the obj param is generally ignored in apollo-state-link
// const { } = info // barely needed (external info about the query)
// get the current messages
// push data
const data = {
flashMessages: [...currentFlashMessages, flashMessage]
};
cache.writeData({ data });
return null;
}
});
registerStateLinkMutation({
name: 'flashMessagesMarkAsSeen',
mutation: (obj, args, context) => {
const { cache } = context;
const { i } = args;
const currentFlashMessages = cache.readData({ query: getMessagesQuery });
currentFlashMessages[i] = { ...currentFlashMessages[i], seen: true };
const data = {
flashMessages: currentFlashMessages
};
cache.writeData({ data });
return null;
}
});
registerStateLinkMutation({
name: 'flashMessagesClear',
mutation: (obj, args, context) => {
const { cache } = context;
const { i } = args;
const currentFlashMessages = cache.readData({ query: getMessagesQuery });
currentFlashMessages[i] = { ...currentFlashMessages[i], show: false };
const data = {
flashMessages: currentFlashMessages
};
cache.writeData({ data });
return null;
}
});
registerStateLinkMutation({
name: 'flashMessagesClearSeen',
mutation: (obj, args, context) => {
const { cache } = context;
const currentFlashMessages = cache.readData({ query: getMessagesQuery });
const newValue = currentFlashMessages.map(message => (message.seen ? { ...message, show: false } : message));
const data = {
flashMessages: newValue
};
cache.writeData({ data });
return null;
}
});
}
// init the flash message state
registerStateLinkDefault({
name: 'flashMessages',
defaultValue: []
});
// mutations (equivalent to reducers)
registerStateLinkMutation({
name: 'flashMessagesFlash',
mutation: (obj, args, context, info) => {
// get relevant values from args
const { cache } = context;
const { content } = args;
// retrieve current state
const currentFlashMessages = cache.readData({ query: getMessagesQuery });
// transform content
const flashType = content && typeof content.type !== 'undefined' ? content.type : 'error';
const _id = currentFlashMessages.length;
const flashMessage = {
_id,
...content,
type: flashType,
seen: false,
show: true
};
// const { } = obj // the obj param is generally ignored in apollo-state-link
// const { } = info // barely needed (external info about the query)
// get the current messages
// push data
const data = {
flashMessages: [...currentFlashMessages, flashMessage]
};
cache.writeData({ data });
return null;
}
});
registerStateLinkMutation({
name: 'flashMessagesMarkAsSeen',
mutation: (obj, args, context) => {
const { cache } = context;
const { i } = args;
const currentFlashMessages = cache.readData({ query: getMessagesQuery });
currentFlashMessages[i] = { ...currentFlashMessages[i], seen: true };
const data = {
flashMessages: currentFlashMessages
};
cache.writeData({ data });
return null;
}
});
registerStateLinkMutation({
name: 'flashMessagesClear',
mutation: (obj, args, context) => {
const { cache } = context;
const { i } = args;
const currentFlashMessages = cache.readData({ query: getMessagesQuery });
currentFlashMessages[i] = { ...currentFlashMessages[i], show: false };
const data = {
flashMessages: currentFlashMessages
};
cache.writeData({ data });
return null;
}
});
registerStateLinkMutation({
name: 'flashMessagesClearSeen',
mutation: (obj, args, context) => {
const { cache } = context;
const currentFlashMessages = cache.readData({ query: getMessagesQuery });
const newValue = currentFlashMessages.map(message => (message.seen ? { ...message, show: false } : message));
const data = {
flashMessages: newValue
};
cache.writeData({ data });
return null;
}
});
const withMessages = compose(
// equivalent to mapDispatchToProps (map the state-link to the component props, so it can access the mutations)

View file

@ -4,7 +4,7 @@ import watchedMutationLink from './links/watchedMutation';
import errorLink from './links/error';
import httpLink from './links/http';
import meteorAccountsLink from './links/meteor';
import { createStateLink } from './links/state';
import { createStateLink } from '../../modules/apollo-common';
import cache from './cache';
// these links do not change once created

View file

@ -1,3 +1,2 @@
export * from './links/state';
export * from './updates';
export * from './apollo2';
export * from './apolloClient';

View file

@ -0,0 +1 @@
export * from './links/state';

View file

@ -20,8 +20,7 @@ export * from './strings.js';
export * from './redux.js';
export * from './headtags.js';
export * from './fragments.js';
// export * from './apollo.js';
// export * from './apollo2.js';
export * from './apollo-common'
export * from './dynamic_loader.js';
export * from './admin.js';
export * from './fragment_matcher.js';

View file

@ -11,17 +11,24 @@ import { InMemoryCache } from 'apollo-cache-inmemory';
import { SchemaLink } from 'apollo-link-schema';
import { GraphQLSchema } from '../../modules/graphql.js';
import { createStateLink } from '../../modules/apollo-common';
import { ApolloLink } from 'apollo-link';
// @see https://www.apollographql.com/docs/react/features/server-side-rendering.html#local-queries
// import { createHttpLink } from 'apollo-link-http';
// import fetch from 'node-fetch'
const createClient = (req) => {
// we need the executable schema
export const createClient = (req) => {
// init
// stateLink will init the client internal state
const cache = new InMemoryCache()
const stateLink = createStateLink({ cache });
// schemaLink will fetch data directly based on the executable schema
const schema = GraphQLSchema.getExecutableSchema()
const schemaLink = new SchemaLink({ schema })
const client = new ApolloClient({
ssrMode: true,
link: new SchemaLink({ schema }),
link: ApolloLink.from([stateLink, schemaLink]),
// @see https://www.apollographql.com/docs/react/features/server-side-rendering.html#local-queries
// Remember that this is the interface the SSR server will use to connect to the
// API server, so we need to ensure it isn't firewalled, etc
@ -37,10 +44,7 @@ const createClient = (req) => {
// // need to explicitely pass fetch server side
// fetch
//}),
cache: new InMemoryCache(),
cache
});
return client
}
export default createClient
}

View file

@ -8,7 +8,7 @@ import React from 'react';
import ReactDOM from 'react-dom/server';
import { renderToStringWithData } from 'react-apollo';
import createClient from './createApolloClient';
import { createClient } from './apolloClient';
import Head from './components/Head'
import ApolloState from './components/ApolloState'