Skip to content

Commit

Permalink
add compiled Subschema class
Browse files Browse the repository at this point in the history
for use with delegation outside of stitching context to ensure that transformSchema methods are called for all transforms

closes #1565
  • Loading branch information
yaacovCR committed Jun 3, 2020
1 parent 9b5f337 commit 5d22553
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 1 deletion.
31 changes: 31 additions & 0 deletions packages/delegate/src/GraphQLSubschema.ts
@@ -0,0 +1,31 @@
import { GraphQLSchema } from 'graphql';

import { Transform, applySchemaTransforms } from '@graphql-tools/utils';

import { SubschemaConfig, MergedTypeConfig, CreateProxyingResolverFn, Subscriber, Executor } from './types';

export function isSubschema(value: any): value is Subschema {
return Boolean((value as Subschema).transformedSchema);
}

export class Subschema {
public schema: GraphQLSchema;
public rootValue?: Record<string, any>;
public executor?: Executor;
public subscriber?: Subscriber;
public createProxyingResolver?: CreateProxyingResolverFn;
public transforms: Array<Transform>;
public transformedSchema: GraphQLSchema;
public merge?: Record<string, MergedTypeConfig>;

constructor(config: SubschemaConfig) {
this.schema = config.schema;
this.executor = config.executor;
this.subscriber = config.subscriber;
this.createProxyingResolver = config.createProxyingResolver;
this.transforms = config.transforms ?? [];
this.merge = config.merge;

this.transformedSchema = applySchemaTransforms(this.schema, this.transforms);
}
}
2 changes: 2 additions & 0 deletions packages/delegate/src/index.ts
Expand Up @@ -7,4 +7,6 @@ export { getSubschema } from './subschema';

export * from './transforms';

export { Subschema } from './GraphQLSubschema';

export * from './types';
4 changes: 3 additions & 1 deletion packages/delegate/src/types.ts
Expand Up @@ -13,8 +13,10 @@ import {
} from 'graphql';
import { Operation, Transform, Request, TypeMap, ExecutionResult } from '@graphql-tools/utils';

import { Subschema } from './GraphQLSubschema';

export interface IDelegateToSchemaOptions<TContext = Record<string, any>, TArgs = Record<string, any>> {
schema: GraphQLSchema | SubschemaConfig;
schema: GraphQLSchema | SubschemaConfig | Subschema;
operation?: Operation;
fieldName?: string;
returnType?: GraphQLOutputType;
Expand Down
133 changes: 133 additions & 0 deletions packages/delegate/tests/transforms.test.ts
@@ -0,0 +1,133 @@
import { Subschema, delegateToSchema } from '@graphql-tools/delegate';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { RenameRootFields, RenameObjectFields } from '@graphql-tools/wrap';
import { graphql, GraphQLSchema } from 'graphql';

describe('can delegate to subschema with transforms', () => {
let sourceSchema: GraphQLSchema;

beforeAll(() => {
const ITEM = {
id: '123',
// eslint-disable-next-line @typescript-eslint/camelcase
camel_case: "I'm a camel!",
};

const targetSchema = makeExecutableSchema({
typeDefs: `
type Item {
id: ID!
camel_case: String
}
type ItemConnection {
edges: [ItemEdge!]!
}
type ItemEdge {
node: Item!
}
type Query {
item: Item
allItems: ItemConnection!
}
`,
resolvers: {
Query: {
item: () => ITEM,
allItems: () => ({
edges: [
{
node: ITEM,
},
],
}),
},
},
});

const subschema = new Subschema({
schema: targetSchema,
transforms: [
new RenameRootFields((_operation, fieldName) => {
if (fieldName === 'allItems') {
return 'items';
}
return fieldName;
}),
new RenameObjectFields((_typeName, fieldName) => {
if (fieldName === 'camel_case') {
return 'camelCase';
}
return fieldName;
}),
]
});

sourceSchema = makeExecutableSchema({
typeDefs: `
type Item {
id: ID!
camelCase: String
}
type ItemConnection {
edges: [ItemEdge!]!
}
type ItemEdge {
node: Item!
}
type Query {
item: Item
items: ItemConnection!
}
`,
resolvers: {
Query: {
item: (_root, _args, _context, info) => delegateToSchema({
schema: subschema,
info,
}),
items: (_root, _args, _context, info) => delegateToSchema({
schema: subschema,
info,
}),
}
}
})
});

test('renaming should work', async () => {
const result = await graphql(
sourceSchema,
`
query {
item {
camelCase
}
items {
edges {
node {
camelCase
}
}
}
}
`,
);

const TRANSFORMED_ITEM = {
camelCase: "I'm a camel!",
};

expect(result).toEqual({
data: {
item: TRANSFORMED_ITEM,
items: {
edges: [
{
node: TRANSFORMED_ITEM,
},
],
},
},
});
});
});

0 comments on commit 5d22553

Please sign in to comment.