sets default graphql context locales through resolver 'middleware'
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
const withDefaultLocale = require('../../../graphql/resolvers/withDefaultLocale');
|
||||
const withPolicy = require('../../../graphql/resolvers/withPolicy');
|
||||
const findQuery = require('../../queries/find');
|
||||
|
||||
const find = ({ config, model }) => {
|
||||
return withPolicy(
|
||||
config.policies.read,
|
||||
const getFind = (config, collection) => withDefaultLocale(
|
||||
config.localization,
|
||||
withPolicy(
|
||||
collection.config.policies.read,
|
||||
async (_, args, context) => {
|
||||
const options = {
|
||||
depth: 0,
|
||||
model,
|
||||
model: collection.model,
|
||||
};
|
||||
|
||||
if (Object.keys(args).length > 0) {
|
||||
@@ -16,12 +18,12 @@ const find = ({ config, model }) => {
|
||||
if (arg === 'where') {
|
||||
return {
|
||||
...query,
|
||||
arg: args[arg],
|
||||
[arg]: args[arg],
|
||||
};
|
||||
}
|
||||
|
||||
return query;
|
||||
});
|
||||
}, {});
|
||||
}
|
||||
|
||||
if (args.locale) {
|
||||
@@ -38,7 +40,7 @@ const find = ({ config, model }) => {
|
||||
|
||||
return results;
|
||||
},
|
||||
);
|
||||
};
|
||||
),
|
||||
);
|
||||
|
||||
module.exports = find;
|
||||
module.exports = getFind;
|
||||
|
||||
@@ -1,30 +1,34 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
const withDefaultLocale = require('../../../graphql/resolvers/withDefaultLocale');
|
||||
const withPolicy = require('../../../graphql/resolvers/withPolicy');
|
||||
const findByIDQuery = require('../../queries/findByID');
|
||||
|
||||
const findByID = ({ config, model }) => withPolicy(
|
||||
config.policies.read,
|
||||
async (_, args, context) => {
|
||||
const options = {
|
||||
depth: 0,
|
||||
model,
|
||||
id: args.id,
|
||||
};
|
||||
const findByID = (config, collection) => withDefaultLocale(
|
||||
config.localization,
|
||||
withPolicy(
|
||||
collection.config.policies.read,
|
||||
async (_, args, context) => {
|
||||
const options = {
|
||||
depth: 0,
|
||||
model: collection.model,
|
||||
id: args.id,
|
||||
};
|
||||
|
||||
if (args.locale) {
|
||||
context.locale = args.locale;
|
||||
options.locale = args.locale;
|
||||
}
|
||||
if (args.locale) {
|
||||
context.locale = args.locale;
|
||||
options.locale = args.locale;
|
||||
}
|
||||
|
||||
if (args.fallbackLocale) {
|
||||
context.fallbackLocale = args.fallbackLocale;
|
||||
options.fallbackLocale = args.fallbackLocale;
|
||||
}
|
||||
if (args.fallbackLocale) {
|
||||
context.fallbackLocale = args.fallbackLocale;
|
||||
options.fallbackLocale = args.fallbackLocale;
|
||||
}
|
||||
|
||||
const result = await findByIDQuery(options);
|
||||
const result = await findByIDQuery(options);
|
||||
|
||||
return result;
|
||||
},
|
||||
return result;
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
module.exports = findByID;
|
||||
|
||||
@@ -9,6 +9,7 @@ const {
|
||||
} = require('graphql');
|
||||
const graphQLHTTP = require('express-graphql');
|
||||
const getBuildObjectType = require('./schema/getBuildObjectType');
|
||||
const buildBlockTypeIfMissing = require('./schema/buildBlockTypeIfMissing');
|
||||
const buildWhereInputType = require('./schema/buildWhereInputType');
|
||||
const buildLocaleInputType = require('./schema/buildLocaleInputType');
|
||||
const buildPaginatedListType = require('./schema/buildPaginatedListType');
|
||||
@@ -24,7 +25,6 @@ class GraphQL {
|
||||
this.init = this.init.bind(this);
|
||||
this.registerUser = this.registerUser.bind(this);
|
||||
this.registerCollections = this.registerCollections.bind(this);
|
||||
this.buildBlockTypeIfMissing = this.buildBlockTypeIfMissing.bind(this);
|
||||
|
||||
this.Query = {
|
||||
name: 'Query',
|
||||
@@ -43,6 +43,7 @@ class GraphQL {
|
||||
};
|
||||
|
||||
this.buildObjectType = getBuildObjectType(this);
|
||||
this.buildBlockTypeIfMissing = buildBlockTypeIfMissing.bind(this);
|
||||
}
|
||||
|
||||
init() {
|
||||
@@ -96,7 +97,7 @@ class GraphQL {
|
||||
singularLabel,
|
||||
fields,
|
||||
singularLabel,
|
||||
getFindByID(this.collections[slug]),
|
||||
getFindByID(this.config, this.collections[slug]),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -128,7 +129,7 @@ class GraphQL {
|
||||
args: {
|
||||
id: { type: GraphQLString },
|
||||
},
|
||||
resolve: getFindByID(collection),
|
||||
resolve: getFindByID(this.config, collection),
|
||||
};
|
||||
|
||||
this.Query.fields[pluralLabel] = {
|
||||
@@ -144,38 +145,10 @@ class GraphQL {
|
||||
type: this.types.fallbackLocaleInputType,
|
||||
},
|
||||
},
|
||||
resolve: getFind(collection),
|
||||
resolve: getFind(this.config, collection),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
buildBlockTypeIfMissing(block) {
|
||||
const {
|
||||
slug,
|
||||
labels: {
|
||||
singular,
|
||||
},
|
||||
} = block;
|
||||
|
||||
if (!this.types.blockTypes[slug]) {
|
||||
const formattedBlockName = formatName(singular);
|
||||
this.types.blockTypes[slug] = this.buildObjectType(
|
||||
formattedBlockName,
|
||||
[
|
||||
...block.fields,
|
||||
{
|
||||
name: 'blockName',
|
||||
type: 'text',
|
||||
},
|
||||
{
|
||||
name: 'blockType',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
formattedBlockName,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GraphQL;
|
||||
|
||||
11
src/graphql/resolvers/withDefaultLocale.js
Normal file
11
src/graphql/resolvers/withDefaultLocale.js
Normal file
@@ -0,0 +1,11 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
const withDefaultLocale = (localization, resolver) => (_, context, args) => {
|
||||
const { defaultLocale, fallback } = localization;
|
||||
|
||||
context.locale = defaultLocale;
|
||||
context.fallbackLocale = fallback ? defaultLocale : null;
|
||||
|
||||
return resolver(_, context, args);
|
||||
};
|
||||
|
||||
module.exports = withDefaultLocale;
|
||||
31
src/graphql/schema/buildBlockTypeIfMissing.js
Normal file
31
src/graphql/schema/buildBlockTypeIfMissing.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const formatName = require('../utilities/formatName');
|
||||
|
||||
function buildBlockTypeIfMissing(block) {
|
||||
const {
|
||||
slug,
|
||||
labels: {
|
||||
singular,
|
||||
},
|
||||
} = block;
|
||||
|
||||
if (!this.types.blockTypes[slug]) {
|
||||
const formattedBlockName = formatName(singular);
|
||||
this.types.blockTypes[slug] = this.buildObjectType(
|
||||
formattedBlockName,
|
||||
[
|
||||
...block.fields,
|
||||
{
|
||||
name: 'blockName',
|
||||
type: 'text',
|
||||
},
|
||||
{
|
||||
name: 'blockType',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
formattedBlockName,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = buildBlockTypeIfMissing;
|
||||
@@ -66,7 +66,7 @@ function getBuildObjectType(graphQLContext) {
|
||||
const { relationTo, label } = field;
|
||||
const isRelatedToManyCollections = Array.isArray(relationTo);
|
||||
const hasManyValues = field.hasMany;
|
||||
const name = combineParentName(parentName, label);
|
||||
const relationshipName = combineParentName(parentName, label);
|
||||
|
||||
let type;
|
||||
|
||||
@@ -76,7 +76,7 @@ function getBuildObjectType(graphQLContext) {
|
||||
});
|
||||
|
||||
type = new GraphQLUnionType({
|
||||
name,
|
||||
name: relationshipName,
|
||||
types,
|
||||
resolveType(data) {
|
||||
return graphQLContext.types.blockTypes[data.blockType];
|
||||
@@ -174,26 +174,26 @@ function getBuildObjectType(graphQLContext) {
|
||||
};
|
||||
|
||||
if (isRelatedToManyCollections) {
|
||||
const relatedCollectionFields = relationTo.reduce((relatedCollectionFields, relation) => {
|
||||
const relatedCollectionFields = relationTo.reduce((allFields, relation) => {
|
||||
return [
|
||||
...relatedCollectionFields,
|
||||
...allFields,
|
||||
...graphQLContext.collections[relation].config.fields,
|
||||
];
|
||||
}, []);
|
||||
|
||||
relationship.args.where = {
|
||||
type: buildWhereInputType({
|
||||
name,
|
||||
name: relationshipName,
|
||||
fields: relatedCollectionFields,
|
||||
parent: name,
|
||||
parent: relationshipName,
|
||||
}),
|
||||
};
|
||||
} else {
|
||||
relationship.args.where = {
|
||||
type: buildWhereInputType({
|
||||
name,
|
||||
name: relationshipName,
|
||||
fields: graphQLContext.collections[relationTo].config.fields,
|
||||
parent: name,
|
||||
parent: relationshipName,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user