New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@pothos/plugin-sub-graph

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pothos/plugin-sub-graph - npm Package Compare versions

Comparing version

to
3.13.0

6

CHANGELOG.md
# Change Log
## 3.13.0
### Minor Changes
- bf0385ae: Add new PothosError classes
## 3.12.7

@@ -4,0 +10,0 @@

58

esm/index.js
/* eslint-disable prefer-destructuring */ import './global-types.js';
import { getNamedType, GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLObjectType, GraphQLScalarType, GraphQLSchema, GraphQLUnionType, isInterfaceType, isNonNullType, isObjectType } from 'graphql';
import SchemaBuilder, { BasePlugin } from '@pothos/core';
import SchemaBuilder, { BasePlugin, PothosSchemaError } from '@pothos/core';
import { replaceType } from './util.js';

@@ -62,3 +62,3 @@ const pluginName = "subGraph";

types.forEach((type) => {
var ref;
var _type_extensions;
if (type.name.startsWith("__")) {

@@ -70,3 +70,3 @@ return;

}
if (!intersect(((ref = type.extensions) === null || ref === void 0 ? void 0 : ref.subGraphs) || [], subGraphs)) {
if (!intersect(((_type_extensions = type.extensions) === null || _type_extensions === void 0 ? void 0 : _type_extensions.subGraphs) || [], subGraphs)) {
return;

@@ -86,6 +86,6 @@ }

else if (type instanceof GraphQLInterfaceType) {
const typeConfig1 = type.toConfig();
const typeConfig = type.toConfig();
newTypes.set(type.name, new GraphQLInterfaceType({
...typeConfig1,
interfaces: () => typeConfig1.interfaces.map((iface) => replaceType(iface, newTypes, typeConfig1.name, subGraphs)),
...typeConfig,
interfaces: () => typeConfig.interfaces.map((iface) => replaceType(iface, newTypes, typeConfig.name, subGraphs)),
fields: this.filterFields(type, newTypes, subGraphs)

@@ -95,12 +95,12 @@ }));

else if (type instanceof GraphQLUnionType) {
const typeConfig2 = type.toConfig();
const typeConfig = type.toConfig();
newTypes.set(type.name, new GraphQLUnionType({
...typeConfig2,
types: () => typeConfig2.types.map((member) => replaceType(member, newTypes, typeConfig2.name, subGraphs))
...typeConfig,
types: () => typeConfig.types.map((member) => replaceType(member, newTypes, typeConfig.name, subGraphs))
}));
}
else if (type instanceof GraphQLInputObjectType) {
const typeConfig3 = type.toConfig();
const typeConfig = type.toConfig();
newTypes.set(type.name, new GraphQLInputObjectType({
...typeConfig3,
...typeConfig,
fields: this.mapInputFields(type, newTypes, subGraphs)

@@ -117,14 +117,14 @@ }));

Object.keys(oldFields).forEach((fieldName) => {
var ref;
var _fieldConfig_extensions;
const fieldConfig = oldFields[fieldName];
const newArguments = {};
if (!intersect(((ref = fieldConfig.extensions) === null || ref === void 0 ? void 0 : ref.subGraphs) || [], subGraphs) || !newTypes.has(getNamedType(fieldConfig.type).name)) {
if (!intersect(((_fieldConfig_extensions = fieldConfig.extensions) === null || _fieldConfig_extensions === void 0 ? void 0 : _fieldConfig_extensions.subGraphs) || [], subGraphs) || !newTypes.has(getNamedType(fieldConfig.type).name)) {
return;
}
fieldConfig.args.forEach((argConfig) => {
var ref;
const argSubGraphs = (ref = argConfig.extensions) === null || ref === void 0 ? void 0 : ref.subGraphs;
var _argConfig_extensions;
const argSubGraphs = (_argConfig_extensions = argConfig.extensions) === null || _argConfig_extensions === void 0 ? void 0 : _argConfig_extensions.subGraphs;
if (argSubGraphs && !intersect(argSubGraphs, subGraphs)) {
if (isNonNullType(argConfig.type)) {
throw new Error(`argument ${argConfig.name} of ${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}.${fieldName}`);
throw new PothosSchemaError(`argument ${argConfig.name} of ${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}.${fieldName}`);
}

@@ -160,8 +160,8 @@ return;

Object.keys(oldFields).forEach((fieldName) => {
var ref;
var _fieldConfig_extensions;
const fieldConfig = oldFields[fieldName];
const fieldSubGraphs = (ref = fieldConfig.extensions) === null || ref === void 0 ? void 0 : ref.subGraphs;
const fieldSubGraphs = (_fieldConfig_extensions = fieldConfig.extensions) === null || _fieldConfig_extensions === void 0 ? void 0 : _fieldConfig_extensions.subGraphs;
if (fieldSubGraphs && !intersect(fieldSubGraphs, subGraphs)) {
if (isNonNullType(fieldConfig.type)) {
throw new Error(`${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}`);
throw new PothosSchemaError(`${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}`);
}

@@ -188,4 +188,4 @@ return;

onTypeConfig(typeConfig) {
var ref;
var _subGraphs, ref1;
var _this_builder_options_subGraphs;
var _typeConfig_pothosOptions_subGraphs, _ref;
return {

@@ -195,3 +195,3 @@ ...typeConfig,

...typeConfig.extensions,
subGraphs: (ref1 = (_subGraphs = typeConfig.pothosOptions.subGraphs) !== null && _subGraphs !== void 0 ? _subGraphs : (ref = this.builder.options.subGraphs) === null || ref === void 0 ? void 0 : ref.defaultForTypes) !== null && ref1 !== void 0 ? ref1 : []
subGraphs: (_ref = (_typeConfig_pothosOptions_subGraphs = typeConfig.pothosOptions.subGraphs) !== null && _typeConfig_pothosOptions_subGraphs !== void 0 ? _typeConfig_pothosOptions_subGraphs : (_this_builder_options_subGraphs = this.builder.options.subGraphs) === null || _this_builder_options_subGraphs === void 0 ? void 0 : _this_builder_options_subGraphs.defaultForTypes) !== null && _ref !== void 0 ? _ref : []
}

@@ -213,3 +213,3 @@ };

onOutputFieldConfig(fieldConfig) {
var ref, ref1;
var _this_builder_options_subGraphs, _this_builder_options_subGraphs1;
const typeConfig = this.buildCache.getTypeConfig(fieldConfig.parentType);

@@ -226,9 +226,9 @@ if (typeConfig.graphqlKind !== "Interface" && typeConfig.graphqlKind !== "Object") {

}
else if ((ref = this.builder.options.subGraphs) === null || ref === void 0 ? void 0 : ref.fieldsInheritFromTypes) {
var ref2;
subGraphs = ((ref2 = typeConfig.extensions) === null || ref2 === void 0 ? void 0 : ref2.subGraphs) || [];
else if ((_this_builder_options_subGraphs = this.builder.options.subGraphs) === null || _this_builder_options_subGraphs === void 0 ? void 0 : _this_builder_options_subGraphs.fieldsInheritFromTypes) {
var _typeConfig_extensions;
subGraphs = ((_typeConfig_extensions = typeConfig.extensions) === null || _typeConfig_extensions === void 0 ? void 0 : _typeConfig_extensions.subGraphs) || [];
}
else if ((ref1 = this.builder.options.subGraphs) === null || ref1 === void 0 ? void 0 : ref1.defaultForFields) {
var ref3;
subGraphs = (ref3 = this.builder.options.subGraphs) === null || ref3 === void 0 ? void 0 : ref3.defaultForFields;
else if ((_this_builder_options_subGraphs1 = this.builder.options.subGraphs) === null || _this_builder_options_subGraphs1 === void 0 ? void 0 : _this_builder_options_subGraphs1.defaultForFields) {
var _this_builder_options_subGraphs2;
subGraphs = (_this_builder_options_subGraphs2 = this.builder.options.subGraphs) === null || _this_builder_options_subGraphs2 === void 0 ? void 0 : _this_builder_options_subGraphs2.defaultForFields;
}

@@ -235,0 +235,0 @@ return {

import { GraphQLList, GraphQLNonNull } from 'graphql';
import { PothosSchemaError } from '@pothos/core';
export function replaceType(type, newTypes, referencedBy, subGraphs) {

@@ -11,3 +12,3 @@ if (type instanceof GraphQLNonNull) {

if (!newType) {
throw new Error(`${type.name} (referenced by ${referencedBy}) does not exist in subGraph (${subGraphs})`);
throw new PothosSchemaError(`${type.name} (referenced by ${referencedBy}) does not exist in subGraph (${subGraphs})`);
}

@@ -14,0 +15,0 @@ return newType;

@@ -115,3 +115,3 @@ /* eslint-disable prefer-destructuring */ "use strict";

types.forEach((type)=>{
var ref;
var _type_extensions;
if (type.name.startsWith('__')) {

@@ -123,3 +123,3 @@ return;

}
if (!intersect(((ref = type.extensions) === null || ref === void 0 ? void 0 : ref.subGraphs) || [], subGraphs)) {
if (!intersect(((_type_extensions = type.extensions) === null || _type_extensions === void 0 ? void 0 : _type_extensions.subGraphs) || [], subGraphs)) {
return;

@@ -137,18 +137,18 @@ }

} else if (type instanceof _graphql.GraphQLInterfaceType) {
const typeConfig1 = type.toConfig();
const typeConfig = type.toConfig();
newTypes.set(type.name, new _graphql.GraphQLInterfaceType({
...typeConfig1,
interfaces: ()=>typeConfig1.interfaces.map((iface)=>(0, _util.replaceType)(iface, newTypes, typeConfig1.name, subGraphs)),
...typeConfig,
interfaces: ()=>typeConfig.interfaces.map((iface)=>(0, _util.replaceType)(iface, newTypes, typeConfig.name, subGraphs)),
fields: this.filterFields(type, newTypes, subGraphs)
}));
} else if (type instanceof _graphql.GraphQLUnionType) {
const typeConfig2 = type.toConfig();
const typeConfig = type.toConfig();
newTypes.set(type.name, new _graphql.GraphQLUnionType({
...typeConfig2,
types: ()=>typeConfig2.types.map((member)=>(0, _util.replaceType)(member, newTypes, typeConfig2.name, subGraphs))
...typeConfig,
types: ()=>typeConfig.types.map((member)=>(0, _util.replaceType)(member, newTypes, typeConfig.name, subGraphs))
}));
} else if (type instanceof _graphql.GraphQLInputObjectType) {
const typeConfig3 = type.toConfig();
const typeConfig = type.toConfig();
newTypes.set(type.name, new _graphql.GraphQLInputObjectType({
...typeConfig3,
...typeConfig,
fields: this.mapInputFields(type, newTypes, subGraphs)

@@ -165,14 +165,14 @@ }));

Object.keys(oldFields).forEach((fieldName)=>{
var ref;
var _fieldConfig_extensions;
const fieldConfig = oldFields[fieldName];
const newArguments = {};
if (!intersect(((ref = fieldConfig.extensions) === null || ref === void 0 ? void 0 : ref.subGraphs) || [], subGraphs) || !newTypes.has((0, _graphql.getNamedType)(fieldConfig.type).name)) {
if (!intersect(((_fieldConfig_extensions = fieldConfig.extensions) === null || _fieldConfig_extensions === void 0 ? void 0 : _fieldConfig_extensions.subGraphs) || [], subGraphs) || !newTypes.has((0, _graphql.getNamedType)(fieldConfig.type).name)) {
return;
}
fieldConfig.args.forEach((argConfig)=>{
var ref;
const argSubGraphs = (ref = argConfig.extensions) === null || ref === void 0 ? void 0 : ref.subGraphs;
var _argConfig_extensions;
const argSubGraphs = (_argConfig_extensions = argConfig.extensions) === null || _argConfig_extensions === void 0 ? void 0 : _argConfig_extensions.subGraphs;
if (argSubGraphs && !intersect(argSubGraphs, subGraphs)) {
if ((0, _graphql.isNonNullType)(argConfig.type)) {
throw new Error(`argument ${argConfig.name} of ${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}.${fieldName}`);
throw new _core.PothosSchemaError(`argument ${argConfig.name} of ${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}.${fieldName}`);
}

@@ -208,8 +208,8 @@ return;

Object.keys(oldFields).forEach((fieldName)=>{
var ref;
var _fieldConfig_extensions;
const fieldConfig = oldFields[fieldName];
const fieldSubGraphs = (ref = fieldConfig.extensions) === null || ref === void 0 ? void 0 : ref.subGraphs;
const fieldSubGraphs = (_fieldConfig_extensions = fieldConfig.extensions) === null || _fieldConfig_extensions === void 0 ? void 0 : _fieldConfig_extensions.subGraphs;
if (fieldSubGraphs && !intersect(fieldSubGraphs, subGraphs)) {
if ((0, _graphql.isNonNullType)(fieldConfig.type)) {
throw new Error(`${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}`);
throw new _core.PothosSchemaError(`${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}`);
}

@@ -236,4 +236,4 @@ return;

onTypeConfig(typeConfig) {
var ref;
var _subGraphs, ref1;
var _this_builder_options_subGraphs;
var _typeConfig_pothosOptions_subGraphs, _ref;
return {

@@ -243,3 +243,3 @@ ...typeConfig,

...typeConfig.extensions,
subGraphs: (ref1 = (_subGraphs = typeConfig.pothosOptions.subGraphs) !== null && _subGraphs !== void 0 ? _subGraphs : (ref = this.builder.options.subGraphs) === null || ref === void 0 ? void 0 : ref.defaultForTypes) !== null && ref1 !== void 0 ? ref1 : []
subGraphs: (_ref = (_typeConfig_pothosOptions_subGraphs = typeConfig.pothosOptions.subGraphs) !== null && _typeConfig_pothosOptions_subGraphs !== void 0 ? _typeConfig_pothosOptions_subGraphs : (_this_builder_options_subGraphs = this.builder.options.subGraphs) === null || _this_builder_options_subGraphs === void 0 ? void 0 : _this_builder_options_subGraphs.defaultForTypes) !== null && _ref !== void 0 ? _ref : []
}

@@ -261,3 +261,3 @@ };

onOutputFieldConfig(fieldConfig) {
var ref, ref1;
var _this_builder_options_subGraphs, _this_builder_options_subGraphs1;
const typeConfig = this.buildCache.getTypeConfig(fieldConfig.parentType);

@@ -272,8 +272,8 @@ if (typeConfig.graphqlKind !== 'Interface' && typeConfig.graphqlKind !== 'Object') {

subGraphs = typeConfig.pothosOptions.defaultSubGraphsForFields;
} else if ((ref = this.builder.options.subGraphs) === null || ref === void 0 ? void 0 : ref.fieldsInheritFromTypes) {
var ref2;
subGraphs = ((ref2 = typeConfig.extensions) === null || ref2 === void 0 ? void 0 : ref2.subGraphs) || [];
} else if ((ref1 = this.builder.options.subGraphs) === null || ref1 === void 0 ? void 0 : ref1.defaultForFields) {
var ref3;
subGraphs = (ref3 = this.builder.options.subGraphs) === null || ref3 === void 0 ? void 0 : ref3.defaultForFields;
} else if ((_this_builder_options_subGraphs = this.builder.options.subGraphs) === null || _this_builder_options_subGraphs === void 0 ? void 0 : _this_builder_options_subGraphs.fieldsInheritFromTypes) {
var _typeConfig_extensions;
subGraphs = ((_typeConfig_extensions = typeConfig.extensions) === null || _typeConfig_extensions === void 0 ? void 0 : _typeConfig_extensions.subGraphs) || [];
} else if ((_this_builder_options_subGraphs1 = this.builder.options.subGraphs) === null || _this_builder_options_subGraphs1 === void 0 ? void 0 : _this_builder_options_subGraphs1.defaultForFields) {
var _this_builder_options_subGraphs2;
subGraphs = (_this_builder_options_subGraphs2 = this.builder.options.subGraphs) === null || _this_builder_options_subGraphs2 === void 0 ? void 0 : _this_builder_options_subGraphs2.defaultForFields;
}

@@ -280,0 +280,0 @@ return {

@@ -10,2 +10,3 @@ "use strict";

const _graphql = require("graphql");
const _core = require("@pothos/core");
function replaceType(type, newTypes, referencedBy, subGraphs) {

@@ -20,3 +21,3 @@ if (type instanceof _graphql.GraphQLNonNull) {

if (!newType) {
throw new Error(`${type.name} (referenced by ${referencedBy}) does not exist in subGraph (${subGraphs})`);
throw new _core.PothosSchemaError(`${type.name} (referenced by ${referencedBy}) does not exist in subGraph (${subGraphs})`);
}

@@ -23,0 +24,0 @@ return newType;

{
"name": "@pothos/plugin-sub-graph",
"version": "3.12.7",
"version": "3.13.0",
"description": "A Pothos plugin for creating multiple variants or sub-selections of the same graph",

@@ -44,9 +44,9 @@ "main": "./lib/index.js",

"devDependencies": {
"@pothos/core": "3.22.9",
"@pothos/plugin-errors": "3.8.7",
"@pothos/plugin-relay": "3.28.7",
"@pothos/plugin-with-input": "3.9.7",
"@pothos/test-utils": "1.4.7",
"graphql": "16.6.0",
"graphql-tag": "^2.12.6"
"graphql-tag": "^2.12.6",
"@pothos/core": "3.25.0",
"@pothos/plugin-errors": "3.11.0",
"@pothos/plugin-relay": "3.35.0",
"@pothos/plugin-with-input": "3.10.0",
"@pothos/test-utils": "1.4.7"
},

@@ -53,0 +53,0 @@ "gitHead": "9dfe52f1975f41a111e01bf96a20033a914e2acc",

# SubGraph Plugin for Pothos
A plugin for creating sub-selections of your graph. This Allows you to use the same code/types for
A plugin for creating sub-selections of your graph. This allows you to use the same code/types for
multiple variants of your API.

@@ -66,5 +66,2 @@

You can mock any field by adding a mock in the options passed to `builder.buildSchema` under
`mocks.{typeName}.{fieldName}`.
### Usage

@@ -96,4 +93,4 @@

list. Like types, output fields that are not included in a sub-graph will also be omitted. Arguments
and fields on Input types can not be removed because that would break assumptions about arguments
types in resolves.
and fields on Input types can not be removed because that would break assumptions about argument
types in resolvers.

@@ -103,2 +100,2 @@ If a type that is not included in the sub-graph is referenced by another part of the graph that is

happen in a number of cases including cases where a removed type is used in the interfaces of an
object, a member of a union, or the type of an field argument.
object, a member of a union, or the type of a field argument.

@@ -24,2 +24,3 @@ /* eslint-disable prefer-destructuring */

PothosOutputFieldConfig,
PothosSchemaError,
PothosTypeConfig,

@@ -202,3 +203,3 @@ SchemaTypes,

if (isNonNullType(argConfig.type)) {
throw new Error(
throw new PothosSchemaError(
`argument ${argConfig.name} of ${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}.${fieldName}`,

@@ -262,3 +263,3 @@ );

if (isNonNullType(fieldConfig.type)) {
throw new Error(
throw new PothosSchemaError(
`${type.name}.${fieldName} is NonNull and must be in included in all sub-graphs that include ${type.name}`,

@@ -265,0 +266,0 @@ );

import { GraphQLList, GraphQLNamedType, GraphQLNonNull, GraphQLType } from 'graphql';
import { PothosSchemaError } from '@pothos/core';

@@ -22,3 +23,3 @@ export function replaceType<T extends GraphQLType>(

if (!newType) {
throw new Error(
throw new PothosSchemaError(
`${

@@ -25,0 +26,0 @@ (type as GraphQLNamedType).name

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet