prettier commit

This commit is contained in:
eric-burel 2019-01-29 17:32:08 +01:00
parent c7aeaea949
commit f51f558cb1
10 changed files with 244 additions and 195 deletions

View file

@ -1,5 +1,9 @@
import {addTrackFunction} from 'meteor/vulcan:events';
import { getApolloClient, getFragment, createClientTemplate } from 'meteor/vulcan:lib';
import {
getApolloClient,
getFragment,
createClientTemplate,
} from 'meteor/vulcan:lib';
import gql from 'graphql-tag';
function trackInternal(eventName, eventProperties) {
@ -8,7 +12,10 @@ function trackInternal(eventName, eventProperties) {
const fragmentName = 'AnalyticsEventFragment';
const fragment = getFragment(fragmentName);
const mutation = gql`${createClientTemplate({ typeName: 'AnalyticsEvent', fragmentName })}${fragment}`;
const mutation = gql`
${createClientTemplate({typeName: 'AnalyticsEvent', fragmentName})}
${fragment}
`;
const variables = {
data: {

View file

@ -39,7 +39,11 @@ import {
import {getSetting} from '../../modules/settings.js';
import {formatError} from 'apollo-errors';
export const setupGraphQLMiddlewares = (apolloServer, config, apolloApplyMiddlewareOptions) => {
export const setupGraphQLMiddlewares = (
apolloServer,
config,
apolloApplyMiddlewareOptions
) => {
// DEBUG LOG
const logReqMiddleware = (req, res, next) => {
console.log('REQ', req.url, req.headers), next();
@ -75,7 +79,6 @@ export const setupGraphQLMiddlewares = (apolloServer, config, apolloApplyMiddlew
...apolloApplyMiddlewareOptions,
});
// setup the end point otherwise the request hangs
// TODO: undestand why this is necessary
// @see
@ -108,7 +111,6 @@ export const createApolloServer = ({
apolloServerOptions = {}, // apollo options
config = {}, // Vulcan options
}) => {
// given options contains the schema
const apolloServer = new ApolloServer({
// graphql playground (replacement to graphiql), available on the app path

View file

@ -124,7 +124,10 @@ export const computeContextFromReq = (currentContext, customContextFromReq) => {
// console.log('// apollo_server.js user-agent:', req.headers['user-agent']);
// console.log('// apollo_server.js locale:', req.headers.locale);
context.locale = getHeaderLocale(req.headers, context.currentUser && context.currentUser.locale);
context.locale = getHeaderLocale(
req.headers,
context.currentUser && context.currentUser.locale
);
return context;
};

View file

@ -7,7 +7,11 @@ import { graphql } from 'graphql';
import {Collections} from '../modules/collections.js';
import DataLoader from 'dataloader';
import findByIds from '../modules/findbyids.js';
import { getDefaultFragmentText, extractFragmentName, getFragmentText } from '../modules/fragments.js';
import {
getDefaultFragmentText,
extractFragmentName,
getFragmentText,
} from '../modules/fragments.js';
import {getSetting} from '../modules/settings';
import merge from 'lodash/merge';
import {singleClientTemplate} from '../modules/graphql_templates';
@ -16,20 +20,31 @@ import { GraphQLSchema } from '../modules/graphql';
// note: if no context is passed, default to running requests with full admin privileges
export const runGraphQL = async (query, variables = {}, context) => {
const defaultContext = { currentUser: {isAdmin: true}, locale: getSetting('locale') };
const defaultContext = {
currentUser: {isAdmin: true},
locale: getSetting('locale'),
};
const queryContext = merge(defaultContext, context);
const executableSchema = GraphQLSchema.getExecutableSchema();
// within the scope of this specific request,
// decorate each collection with a new Dataloader object and add it to context
Collections.forEach(collection => {
collection.loader = new DataLoader(ids => findByIds(collection, ids, queryContext), { cache: true });
collection.loader = new DataLoader(
ids => findByIds(collection, ids, queryContext),
{cache: true}
);
queryContext[collection.options.collectionName] = collection;
});
// see http://graphql.org/graphql-js/graphql/#graphql
const result = await graphql(executableSchema, query, {}, queryContext, variables);
const result = await graphql(
executableSchema,
query,
{},
queryContext,
variables
);
if (result.errors) {
// eslint-disable-next-line no-console
@ -51,42 +66,51 @@ If no fragment is passed, default to default fragment
*/
export const buildQuery = (collection, {fragmentName, fragmentText}) => {
const collectionName = collection.options.collectionName;
const typeName = collection.options.typeName;
const defaultFragmentName = `${collectionName}DefaultFragment`;
const defaultFragmentText = getDefaultFragmentText(collection, { onlyViewable: false });
const defaultFragmentText = getDefaultFragmentText(collection, {
onlyViewable: false,
});
// default to default name and text
let name = defaultFragmentName;
let text = defaultFragmentText;
if (fragmentName) { // if fragmentName is passed, use that to get name and text
if (fragmentName) {
// if fragmentName is passed, use that to get name and text
name = fragmentName;
text = getFragmentText(fragmentName);
} else if (fragmentText) { // if fragmentText is passed, use that to get name and text
} else if (fragmentText) {
// if fragmentText is passed, use that to get name and text
name = extractFragmentName(fragmentText);
text = fragmentText;
}
const query = `${singleClientTemplate({ typeName, fragmentName: name })}${text}`;
const query = `${singleClientTemplate({
typeName,
fragmentName: name,
})}${text}`;
return query;
};
Meteor.startup(() => {
Collections.forEach(collection => {
const typeName = collection.options.typeName;
collection.queryOne = async (documentId, { fragmentName, fragmentText, context }) => {
collection.queryOne = async (
documentId,
{fragmentName, fragmentText, context}
) => {
const query = buildQuery(collection, {fragmentName, fragmentText});
const result = await runQuery(query, { input: { selector: { documentId } } }, context);
const result = await runQuery(
query,
{input: {selector: {documentId}}},
context
);
return result.data[Utils.camelCaseify(typeName)].result;
};
});
});

View file

@ -4,7 +4,7 @@ import {
defaultConfig,
initGraphQL,
initContext,
computeContextFromReq
computeContextFromReq,
} from '../../lib/server/apollo-server';
import {GraphQLSchema} from '../../lib/modules/graphql';
import expect from 'expect';
@ -53,6 +53,5 @@ describe('apollo-server', function() {
test.skip('initial context is merged with provided context', function() {
// TODO
});
});
});

View file

@ -74,5 +74,5 @@ const resolvers = {
export const executableSchema = makeExecutableSchema({
typeDefs,
resolvers
resolvers,
});

View file

@ -31,17 +31,22 @@ Users.getUser = function (userOrUserId) {
*/
Users.getUserName = function(user) {
try {
if (user.username)
return user.username;
if (user && user.services && user.services.twitter && user.services.twitter.screenName)
if (user.username) return user.username;
if (
user &&
user.services &&
user.services.twitter &&
user.services.twitter.screenName
)
return user.services.twitter.screenName;
}
catch (error){
} catch (error) {
console.log(error); // eslint-disable-line
return null;
}
};
Users.getUserNameById = function (userId) {return Users.getUserName(Users.findOne(userId));};
Users.getUserNameById = function(userId) {
return Users.getUserName(Users.findOne(userId));
};
/**
* @summary Get a user's display name (not unique, can take special characters and spaces)
@ -51,10 +56,12 @@ Users.getDisplayName = function (user) {
if (!user) {
return '';
} else {
return (user.displayName) ? user.displayName : Users.getUserName(user);
return user.displayName ? user.displayName : Users.getUserName(user);
}
};
Users.getDisplayNameById = function (userId) {return Users.getDisplayName(Users.findOne(userId));};
Users.getDisplayNameById = function(userId) {
return Users.getDisplayName(Users.findOne(userId));
};
/**
* @summary Get a user's profile URL
@ -98,7 +105,9 @@ Users.getTwitterName = function (user) {
}
return null;
};
Users.getTwitterNameById = function (userId) {return Users.getTwitterName(Users.findOne(userId));};
Users.getTwitterNameById = function(userId) {
return Users.getTwitterName(Users.findOne(userId));
};
/**
* @summary Get a user's GitHub name
@ -108,12 +117,15 @@ Users.getGitHubName = function (user) {
// return twitter name provided by user, or else the one used for twitter login
if (Utils.checkNested(user, 'profile', 'github')) {
return user.profile.github;
}else if(Utils.checkNested(user, 'services', 'github', 'screenName')){ // TODO: double-check this with GitHub login
} else if (Utils.checkNested(user, 'services', 'github', 'screenName')) {
// TODO: double-check this with GitHub login
return user.services.github.screenName;
}
return null;
};
Users.getGitHubNameById = function (userId) {return Users.getGitHubName(Users.findOne(userId));};
Users.getGitHubNameById = function(userId) {
return Users.getGitHubName(Users.findOne(userId));
};
/**
* @summary Get a user's email
@ -126,7 +138,9 @@ Users.getEmail = function (user) {
return null;
}
};
Users.getEmailById = function (userId) {return Users.getEmail(Users.findOne(userId));};
Users.getEmailById = function(userId) {
return Users.getEmail(Users.findOne(userId));
};
/**
* @summary Get a user's email hash
@ -135,7 +149,9 @@ Users.getEmailById = function (userId) {return Users.getEmail(Users.findOne(user
Users.getEmailHash = function(user) {
return user.emailHash;
};
Users.getEmailHashById = function (userId) {return Users.getEmailHash(Users.findOne(userId));};
Users.getEmailHashById = function(userId) {
return Users.getEmailHash(Users.findOne(userId));
};
/**
* @summary Get a user setting
@ -161,13 +177,11 @@ Users.getSetting = function (user = null, settingName, defaultValue = null) {
* @param {Object} user
*/
Users.hasCompletedProfile = function(user) {
if (!user) return false;
return _.every(Users.getRequiredFields(), function(fieldName) {
return !!Utils.getNestedProperty(user, fieldName);
});
};
///////////////////
@ -181,8 +195,7 @@ Users.findLast = function (user, collection) {
Users.timeSinceLast = function(user, collection) {
var now = new Date().getTime();
var last = this.findLast(user, collection);
if(!last)
return 999; // if this is the user's first post or comment ever, stop here
if (!last) return 999; // if this is the user's first post or comment ever, stop here
return Math.abs(Math.floor((now - last.createdAt) / 1000));
};
@ -191,8 +204,8 @@ Users.numberOfItemsInPast24Hours = function (user, collection) {
var items = collection.find({
userId: user._id,
createdAt: {
$gte: mNow.subtract(24, 'hours').toDate()
}
$gte: mNow.subtract(24, 'hours').toDate(),
},
});
return items.count();
};
@ -203,7 +216,9 @@ Users.getProperty = function (object, property) {
if (array.length > 1) {
var parent = array.shift();
// if our property is not at this level, call function again one level deeper if we can go deeper, else return undefined
return (typeof object[parent] === 'undefined') ? undefined : this.getProperty(object[parent], array.join('.'));
return typeof object[parent] === 'undefined'
? undefined
: this.getProperty(object[parent], array.join('.'));
} else {
// else return property
return object[array[0]];
@ -220,7 +235,6 @@ Users.getProperty = function (object, property) {
// Users.update(user._id, modifier);
// }
////////////////////
// More Helpers //
////////////////////
@ -249,5 +263,5 @@ Users.getRequiredFields = function () {
// };
Users.findByEmail = function(email) {
return Users.findOne({'email': email});
return Users.findOne({email: email});
};