Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use field configs #1451

Merged
merged 7 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions packages/stitch/src/mergeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
TypeMap,
IResolvers,
IFieldResolverOptions,
mapSchemaByField,
mapSchema,
MapperKind,
} from '@graphql-tools/utils';

import { delegateToSchema, isSubschemaConfig, SubschemaConfig } from '@graphql-tools/delegate';
Expand Down Expand Up @@ -217,17 +218,20 @@ export function completeMergeInfo(mergeInfo: MergeInfo, resolvers: IResolvers):
}

export function addMergeInfo(stitchedSchema: GraphQLSchema, mergeInfo: MergeInfo): GraphQLSchema {
return mapSchemaByField(stitchedSchema, ([fieldName, originalFieldConfig]) => {
const newFieldConfig = { ...originalFieldConfig };
const resolver = originalFieldConfig.resolve;
if (resolver != null) {
newFieldConfig.resolve = (parent, args, context, info) => resolver(parent, args, context, { ...info, mergeInfo });
}
const subscriber = originalFieldConfig.subscribe;
if (subscriber != null) {
newFieldConfig.subscribe = (parent, args, context, info) =>
subscriber(parent, args, context, { ...info, mergeInfo });
}
return [fieldName, newFieldConfig];
return mapSchema(stitchedSchema, {
[MapperKind.OBJECT_FIELD]: originalFieldConfig => {
const newFieldConfig = { ...originalFieldConfig };
const resolver = originalFieldConfig.resolve;
if (resolver != null) {
newFieldConfig.resolve = (parent, args, context, info) =>
resolver(parent, args, context, { ...info, mergeInfo });
}
const subscriber = originalFieldConfig.subscribe;
if (subscriber != null) {
newFieldConfig.subscribe = (parent, args, context, info) =>
subscriber(parent, args, context, { ...info, mergeInfo });
}
return newFieldConfig;
},
});
}
53 changes: 18 additions & 35 deletions packages/stitch/tests/alternateStitchSchemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import {
FieldNode,
printSchema,
graphqlSync,
GraphQLField,
assertValidSchema,
GraphQLFieldConfig,
isSpecifiedScalarType,
GraphQLNamedType,
GraphQLFieldConfigArgumentMap,
} from 'graphql';

import {
Expand Down Expand Up @@ -355,26 +353,15 @@ describe('transform object fields', () => {
(
typeName: string,
fieldName: string,
field: GraphQLField<any, any>,
fieldConfig: GraphQLFieldConfig<any, any>,
) => {
if (typeName !== 'Property' || fieldName !== 'name') {
return undefined;
}
return {
description: field.deprecationReason,
type: field.type,
args: field.args.reduce<GraphQLFieldConfigArgumentMap>(
(prev, curr) => ({
...prev,
[curr.name]: curr,
}),
{},
),
...fieldConfig,
description: fieldConfig.deprecationReason,
resolve: () => 'test',
subscribe: field.subscribe,
deprecationReason: field.deprecationReason,
extensions: field.extensions,
astNode: field.astNode,
};
},
(typeName: string, fieldName: string, fieldNode: FieldNode) => {
Expand Down Expand Up @@ -434,27 +421,23 @@ describe('default values', () => {
(
typeName: string,
fieldName: string,
field: GraphQLField<any, any>,
fieldConfig: GraphQLFieldConfig<any, any>,
) => {
if (typeName === 'Query' && fieldName === 'jsonTest') {
const fieldConfig: GraphQLFieldConfig<any, any> = {
description: field.deprecationReason,
type: field.type,
args: field.args.reduce<GraphQLFieldConfigArgumentMap>(
(prev, curr) => ({
...prev,
[curr.name]: curr,
}),
{},
),
resolve: field.resolve,
subscribe: field.subscribe,
deprecationReason: field.deprecationReason,
extensions: field.extensions,
astNode: field.astNode,
};
fieldConfig.args.input.defaultValue = { test: 'test' };
return { name: 'renamedJsonTest', field: fieldConfig };
return [
'renamedJsonTest',
{
...fieldConfig,
description: fieldConfig.deprecationReason,
args: {
...fieldConfig.args,
input: {
...fieldConfig.args.input,
defaultValue: { test: 'test' }
}
}
}
];
}
},
),
Expand Down
38 changes: 33 additions & 5 deletions packages/utils/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
GraphQLOutputType,
FieldDefinitionNode,
GraphQLFieldConfig,
GraphQLInputFieldConfig,
} from 'graphql';

import { SchemaVisitor } from './SchemaVisitor';
Expand Down Expand Up @@ -130,12 +131,16 @@ export type FieldNodeMapper = (

export type FieldNodeMappers = Record<string, Record<string, FieldNodeMapper>>;

export type FieldFilter = (typeName?: string, fieldName?: string, field?: GraphQLField<any, any>) => boolean;
export type FieldFilter = (
typeName?: string,
fieldName?: string,
fieldConfig?: GraphQLFieldConfig<any, any>
) => boolean;

export type RootFieldFilter = (
operation?: 'Query' | 'Mutation' | 'Subscription',
rootFieldName?: string,
field?: GraphQLField<any, any>
fieldConfig?: GraphQLFieldConfig<any, any>
) => boolean;

export type RenameTypesOptions = {
Expand Down Expand Up @@ -353,6 +358,15 @@ export enum MapperKind {
MUTATION = 'MapperKind.MUTATION',
SUBSCRIPTION = 'MapperKind.SUBSCRIPTION',
DIRECTIVE = 'MapperKind.DIRECTIVE',
FIELD = 'MapperKind.FIELD',
COMPOSITE_FIELD = 'MapperKind.COMPOSITE_FIELD',
OBJECT_FIELD = 'MapperKind.OBJECT_FIELD',
ROOT_FIELD = 'MapperKind.ROOT_FIELD',
QUERY_ROOT_FIELD = 'MapperKind.QUERY_ROOT_FIELD',
MUTATION_ROOT_FIELD = 'MapperKind.MUTATION_ROOT_FIELD',
SUBSCRIPTION_ROOT_FIELD = 'MapperKind.SUBSCRIPTION_ROOT_FIELD',
INTERFACE_FIELD = 'MapperKind.INTERFACE_FIELD',
INPUT_OBJECT_FIELD = 'MapperKind.INPUT_OBJECT_FIELD',
}

export interface SchemaMapper {
Expand All @@ -370,6 +384,13 @@ export interface SchemaMapper {
[MapperKind.MUTATION]?: ObjectTypeMapper;
[MapperKind.SUBSCRIPTION]?: ObjectTypeMapper;
[MapperKind.DIRECTIVE]?: DirectiveMapper;
[MapperKind.OBJECT_FIELD]?: ObjectFieldMapper;
[MapperKind.ROOT_FIELD]?: ObjectFieldMapper;
[MapperKind.QUERY_ROOT_FIELD]?: ObjectFieldMapper;
[MapperKind.MUTATION_ROOT_FIELD]?: ObjectFieldMapper;
[MapperKind.SUBSCRIPTION_ROOT_FIELD]?: ObjectFieldMapper;
[MapperKind.INTERFACE_FIELD]?: InterfaceFieldMapper;
[MapperKind.INPUT_OBJECT_FIELD]?: InputObjectFieldMapper;
}

export type NamedTypeMapper = (type: GraphQLNamedType, schema: GraphQLSchema) => GraphQLNamedType | null | undefined;
Expand Down Expand Up @@ -407,6 +428,13 @@ export type DirectiveMapper = (
schema: GraphQLSchema
) => GraphQLDirective | null | undefined;

export type FieldMapper = (
originalField: [string, GraphQLFieldConfig<any, any>]
) => [string, GraphQLFieldConfig<any, any>] | null | undefined;
export type FieldMapper<
F extends GraphQLFieldConfig<any, any> | GraphQLInputFieldConfig,
T extends GraphQLObjectType | GraphQLInterfaceType | GraphQLInputObjectType
> = (fieldConfig: F, fieldName: string, type: T, schema: GraphQLSchema) => F | [string, F] | null | undefined;

export type ObjectFieldMapper = FieldMapper<GraphQLFieldConfig<any, any>, GraphQLObjectType>;

export type InterfaceFieldMapper = FieldMapper<GraphQLFieldConfig<any, any>, GraphQLInterfaceType>;

export type InputObjectFieldMapper = FieldMapper<GraphQLInputFieldConfig, GraphQLInputObjectType>;
166 changes: 118 additions & 48 deletions packages/utils/src/fields.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,131 @@
import { GraphQLFieldConfigMap, GraphQLObjectType, GraphQLFieldConfig } from 'graphql';

import { TypeMap } from './Interfaces';
import { fieldToFieldConfig } from './toConfig';

export function appendFields(typeMap: TypeMap, typeName: string, fields: GraphQLFieldConfigMap<any, any>): void {
let type = typeMap[typeName];
if (type != null) {
const newFieldConfigMap: GraphQLFieldConfigMap<any, any> = Object.entries(
(type as GraphQLObjectType).getFields()
).reduce(
(acc, [fieldName, field]) => ({
...acc,
[fieldName]: fieldToFieldConfig(field),
}),
{}
);

Object.keys(fields).forEach(fieldName => {
newFieldConfigMap[fieldName] = fields[fieldName];
import { GraphQLFieldConfigMap, GraphQLObjectType, GraphQLFieldConfig, GraphQLSchema } from 'graphql';
import { MapperKind } from './Interfaces';
import { mapSchema } from './mapSchema';
import { rewireTypes } from './rewire';

export function modifyFields(
schema: GraphQLSchema,
{
append = [],
remove = [],
}: {
append?: Array<{ typeName: string; additionalFields: GraphQLFieldConfigMap<any, any> }>;
remove?: Array<{
typeName: string;
testFn: (fieldName: string, field: GraphQLFieldConfig<any, any>) => boolean;
}>;
}
): GraphQLSchema {
const queryType = schema.getQueryType();
const mutationType = schema.getMutationType();
const subscriptionType = schema.getSubscriptionType();

const queryTypeName = queryType != null ? queryType.name : undefined;
const mutationTypeName = mutationType != null ? mutationType.name : undefined;
const subscriptionTypeName = subscriptionType != null ? subscriptionType.name : undefined;

const config = schema.toConfig();

const originalTypeMap = {};
config.types.forEach(type => {
originalTypeMap[type.name] = type;
});

remove.forEach(({ typeName, testFn }) => {
const config = (originalTypeMap[typeName] as GraphQLObjectType).toConfig();
const originalFieldConfigMap = config.fields;
const newFieldConfigMap = {};
Object.keys(originalFieldConfigMap).forEach(fieldName => {
if (!testFn(fieldName, originalFieldConfigMap[fieldName])) {
newFieldConfigMap[fieldName] = originalFieldConfigMap[fieldName];
}
});
type = new GraphQLObjectType({
...(type as GraphQLObjectType).toConfig(),
originalTypeMap[typeName] = new GraphQLObjectType({
...config,
fields: newFieldConfigMap,
});
} else {
type = new GraphQLObjectType({
name: typeName,
fields,
});
}
typeMap[typeName] = type;
});

append.forEach(({ typeName, additionalFields }) => {
if (originalTypeMap[typeName] == null) {
originalTypeMap[typeName] = new GraphQLObjectType({
name: typeName,
fields: additionalFields,
});
} else {
const config = (originalTypeMap[typeName] as GraphQLObjectType).toConfig();
const originalFieldConfigMap = config.fields;
const newFieldConfigMap = {};
Object.keys(originalFieldConfigMap).forEach(fieldName => {
newFieldConfigMap[fieldName] = originalFieldConfigMap[fieldName];
});
Object.keys(additionalFields).forEach(fieldName => {
newFieldConfigMap[fieldName] = additionalFields[fieldName];
});
originalTypeMap[typeName] = new GraphQLObjectType({
...config,
fields: newFieldConfigMap,
});
}
});

const { typeMap, directives } = rewireTypes(originalTypeMap, config.directives);

return new GraphQLSchema({
...config,
query: queryTypeName ? (typeMap[queryTypeName] as GraphQLObjectType) : undefined,
mutation: mutationTypeName ? (typeMap[mutationTypeName] as GraphQLObjectType) : undefined,
subscription: subscriptionTypeName != null ? (typeMap[subscriptionTypeName] as GraphQLObjectType) : undefined,
types: Object.keys(typeMap).map(typeName => typeMap[typeName]),
directives,
});
}

export function appendFields(
schema: GraphQLSchema,
typeName: string,
additionalFields: GraphQLFieldConfigMap<any, any>
): GraphQLSchema {
return modifyFields(schema, { append: [{ typeName, additionalFields }] });
}

export function removeFields(
typeMap: TypeMap,
schema: GraphQLSchema,
typeName: string,
testFn: (fieldName: string, field: GraphQLFieldConfig<any, any>) => boolean
): GraphQLFieldConfigMap<any, any> {
let type = typeMap[typeName];

const originalFields = (type as GraphQLObjectType).getFields();
const newFields = {};
const removedFields = {};
Object.keys(originalFields).forEach(fieldName => {
const originalFieldConfig = fieldToFieldConfig(originalFields[fieldName]);
if (testFn(fieldName, originalFieldConfig)) {
removedFields[fieldName] = originalFieldConfig;
} else {
newFields[fieldName] = originalFieldConfig;
}
): [GraphQLSchema, GraphQLFieldConfigMap<any, any>] {
const selectedFields = getFields(schema, typeName, testFn);
const selectedFieldNames = Object.keys(selectedFields);
const newSchema = modifyFields(schema, {
remove: [{ typeName, testFn: fieldName => selectedFieldNames.includes(fieldName) }],
});

type = new GraphQLObjectType({
...(type as GraphQLObjectType).toConfig(),
fields: newFields,
return [newSchema, selectedFields];
}

export function getFields(
schema: GraphQLSchema,
typeName: string,
testFn: (fieldName: string, field: GraphQLFieldConfig<any, any>) => boolean
): GraphQLFieldConfigMap<any, any> {
const selectedFields = {};
mapSchema(schema, {
[MapperKind.OBJECT_TYPE]: type => {
if (type.name === typeName) {
const config = type.toConfig();
const originalFieldConfigMap = config.fields;

Object.keys(originalFieldConfigMap).forEach(fieldName => {
const originalFieldConfig = originalFieldConfigMap[fieldName];
if (testFn(fieldName, originalFieldConfig)) {
selectedFields[fieldName] = originalFieldConfig;
}
});
}

return undefined;
},
});
typeMap[typeName] = type;

return removedFields;
return selectedFields;
}
Loading