Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
122 lines
4.1 KiB
Plaintext
122 lines
4.1 KiB
Plaintext
import * as GraphQL from 'graphql';
|
|
import { createComplexityRule, fieldExtensionsEstimator, simpleEstimator } from './packages/graphql-query-complexity/index.js';
|
|
import { accessResolver } from './resolvers/auth/access.js';
|
|
import { buildFallbackLocaleInputType } from './schema/buildFallbackLocaleInputType.js';
|
|
import { buildLocaleInputType } from './schema/buildLocaleInputType.js';
|
|
import { buildPoliciesType } from './schema/buildPoliciesType.js';
|
|
import { initCollections } from './schema/initCollections.js';
|
|
import { initGlobals } from './schema/initGlobals.js';
|
|
import { wrapCustomFields } from './utilities/wrapCustomResolver.js';
|
|
export function configToSchema(config) {
|
|
const collections = config.collections.reduce((acc, collection)=>{
|
|
acc[collection.slug] = {
|
|
config: collection
|
|
};
|
|
return acc;
|
|
}, {});
|
|
const globals = {
|
|
config: config.globals
|
|
};
|
|
const graphqlResult = {
|
|
collections,
|
|
globals,
|
|
Mutation: {
|
|
name: 'Mutation',
|
|
fields: {}
|
|
},
|
|
Query: {
|
|
name: 'Query',
|
|
fields: {}
|
|
},
|
|
types: {
|
|
arrayTypes: {},
|
|
blockInputTypes: {},
|
|
blockTypes: {},
|
|
groupTypes: {},
|
|
tabTypes: {}
|
|
}
|
|
};
|
|
if (config.localization) {
|
|
graphqlResult.types['localeInputType'] = buildLocaleInputType(config.localization);
|
|
graphqlResult.types['fallbackLocaleInputType'] = buildFallbackLocaleInputType(config.localization);
|
|
}
|
|
initCollections({
|
|
config,
|
|
graphqlResult
|
|
});
|
|
initGlobals({
|
|
config,
|
|
graphqlResult
|
|
});
|
|
graphqlResult.Query.fields['Access'] = {
|
|
type: buildPoliciesType(config),
|
|
resolve: accessResolver(config)
|
|
};
|
|
if (typeof config.graphQL.queries === 'function') {
|
|
const customQueries = config.graphQL.queries(GraphQL, {
|
|
...graphqlResult,
|
|
config
|
|
});
|
|
graphqlResult.Query = {
|
|
...graphqlResult.Query,
|
|
fields: {
|
|
...graphqlResult.Query.fields,
|
|
...wrapCustomFields(customQueries || {})
|
|
}
|
|
};
|
|
}
|
|
if (typeof config.graphQL.mutations === 'function') {
|
|
const customMutations = config.graphQL.mutations(GraphQL, {
|
|
...graphqlResult,
|
|
config
|
|
});
|
|
graphqlResult.Mutation = {
|
|
...graphqlResult.Mutation,
|
|
fields: {
|
|
...graphqlResult.Mutation.fields,
|
|
...wrapCustomFields(customMutations || {})
|
|
}
|
|
};
|
|
}
|
|
const query = new GraphQL.GraphQLObjectType(graphqlResult.Query);
|
|
const mutation = new GraphQL.GraphQLObjectType(graphqlResult.Mutation);
|
|
const schema = new GraphQL.GraphQLSchema({
|
|
mutation,
|
|
query
|
|
});
|
|
const validationRules = (args)=>[
|
|
createComplexityRule({
|
|
estimators: [
|
|
fieldExtensionsEstimator(),
|
|
simpleEstimator({
|
|
defaultComplexity: 1
|
|
})
|
|
],
|
|
maximumComplexity: config.graphQL.maxComplexity,
|
|
variables: args.variableValues
|
|
}),
|
|
...config.graphQL.disableIntrospectionInProduction ? [
|
|
NoProductionIntrospection
|
|
] : [],
|
|
...typeof config?.graphQL?.validationRules === 'function' ? config.graphQL.validationRules(args) : []
|
|
];
|
|
return {
|
|
schema,
|
|
validationRules
|
|
};
|
|
}
|
|
const NoProductionIntrospection = (context)=>({
|
|
Field (node) {
|
|
if (process.env.NODE_ENV === 'production') {
|
|
if (node.name.value === '__schema' || node.name.value === '__type') {
|
|
context.reportError(new GraphQL.GraphQLError('GraphQL introspection is not allowed, but the query contained __schema or __type', {
|
|
nodes: [
|
|
node
|
|
]
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
//# sourceMappingURL=index.js.map |