Skip to content

Commit

Permalink
Remove qualified export, add wrapquery and extractfield
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Apr 23, 2018
1 parent 169e698 commit 9b834b4
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 57 deletions.
10 changes: 6 additions & 4 deletions docs/source/schema-stitching.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ import {
makeExecutableSchema,
addMockFunctionsToSchema,
mergeSchemas,
Transforms,
transformSchema,
FilterRootFields,
RenameTypes,
RenameRootFields,
} from 'graphql-tools';

// Mocked chirp schema; we don't want to worry about the schema
Expand All @@ -212,11 +214,11 @@ addMockFunctionsToSchema({ schema: chirpSchema });
// create transform schema

const transformedChirpSchema = transformSchema(chirpSchema, [
new Transforms.FilterRootFields(
new FilterRootFields(
(operation: string, rootField: string) => rootField !== 'chirpsByAuthorId'
),
new Transforms.RenameTypes((name: string) => `Chirp_${name}`),
new Transforms.RenameRootFields((name: string) => `Chirp_${name}`),
new RenameTypes((name: string) => `Chirp_${name}`),
new RenameRootFields((name: string) => `Chirp_${name}`),
]);
```

Expand Down
31 changes: 31 additions & 0 deletions docs/source/schema-transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,37 @@ RenameRootFields(

### Other

* `ExractField({ from: Array<string>, to: Array<string> })` - move selection at `from` path to `to` path.

* `WrapQuery(
path: Array<string>,
wrapper: QueryWrapper,
extractor: (result: any) => any,
)` - wrap a selection at `path` using function `wrapper`. Apply `extractor` at the same path to get the result. This is used to get a result nested inside other result

```js
transforms: [
// Wrap document takes a subtree as an AST node
new WrapQuery(
// path at which to apply wrapping and extracting
['userById'],
(subtree: SelectionSetNode) => ({
// we create a wrapping AST Field
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
// that field is `address`
value: 'address',
},
// Inside the field selection
selectionSet: subtree,
}),
// how to process the data result at path
result => result && result.address,
),
],
```

* `ReplaceFieldWithFragment(targetSchema: GraphQLSchema, mapping: FieldToFragmentMapping)`: Replace the given fields with an inline fragment. Used by `mergeSchemas` to handle the `fragment` option.

```ts
Expand Down
23 changes: 9 additions & 14 deletions src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ import {
} from './schemaRecreation';
import delegateToSchema from './delegateToSchema';
import typeFromAST, { GetType } from './typeFromAST';
import { Transform, Transforms } from '../transforms';
import {
Transform,
ExpandAbstractTypes,
ReplaceFieldWithFragment,
} from '../transforms';
import mergeDeep from '../mergeDeep';

export type OnTypeConflict = (
Expand Down Expand Up @@ -317,14 +321,8 @@ function createMergeInfo(
'Use `mergeInfo.delegateToSchema and pass explicit schema instances.',
);
const schema = guessSchemaByRootField(allSchemas, operation, fieldName);
const expandTransforms = new Transforms.ExpandAbstractTypes(
info.schema,
schema,
);
const fragmentTransform = new Transforms.ReplaceFieldWithFragment(
schema,
fragments,
);
const expandTransforms = new ExpandAbstractTypes(info.schema, schema);
const fragmentTransform = new ReplaceFieldWithFragment(schema, fragments);
return delegateToSchema({
schema,
operation,
Expand All @@ -345,11 +343,8 @@ function createMergeInfo(
...options,
transforms: [
...(options.transforms || []),
new Transforms.ExpandAbstractTypes(
options.info.schema,
options.schema,
),
new Transforms.ReplaceFieldWithFragment(options.schema, fragments),
new ExpandAbstractTypes(options.info.schema, options.schema),
new ReplaceFieldWithFragment(options.schema, fragments),
],
});
},
Expand Down
19 changes: 12 additions & 7 deletions src/test/testAlternateMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import { expect } from 'chai';
import { graphql, GraphQLSchema } from 'graphql';
import mergeSchemas from '../stitching/mergeSchemas';
import { Transforms, transformSchema } from '../transforms';
import {
transformSchema,
FilterRootFields,
RenameTypes,
RenameRootFields,
} from '../transforms';
import { propertySchema, bookingSchema } from './testingSchemas';

let linkSchema = `
Expand Down Expand Up @@ -56,20 +61,20 @@ describe('merge schemas through transforms', () => {
before(async () => {
// namespace and strip schemas
const transformedPropertySchema = transformSchema(propertySchema, [
new Transforms.FilterRootFields(
new FilterRootFields(
(operation: string, rootField: string) =>
'Query.properties' === `${operation}.${rootField}`,
),
new Transforms.RenameTypes((name: string) => `Properties_${name}`),
new Transforms.RenameRootFields((name: string) => `Properties_${name}`),
new RenameTypes((name: string) => `Properties_${name}`),
new RenameRootFields((name: string) => `Properties_${name}`),
]);
const transformedBookingSchema = transformSchema(bookingSchema, [
new Transforms.FilterRootFields(
new FilterRootFields(
(operation: string, rootField: string) =>
'Query.bookings' === `${operation}.${rootField}`,
),
new Transforms.RenameTypes((name: string) => `Bookings_${name}`),
new Transforms.RenameRootFields(
new RenameTypes((name: string) => `Bookings_${name}`),
new RenameRootFields(
(operation: string, name: string) => `Bookings_${name}`,
),
]);
Expand Down
Loading

0 comments on commit 9b834b4

Please sign in to comment.