-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(projection): improve nested merging and add tests
- Loading branch information
Showing
3 changed files
with
250 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* @flow */ | ||
|
||
import { graphql } from '../graphql'; | ||
import type { GraphQLResolveInfo, GraphQLObjectType } from '../graphql'; | ||
import { getProjectionFromAST, extendByFieldProjection } from '../projection'; | ||
import TypeComposer from '../typeComposer'; | ||
import gqc from '../gqc'; | ||
|
||
const Level2TC = TypeComposer.create({ | ||
name: 'Level2', | ||
fields: { | ||
field2a: 'String', | ||
field2b: 'Int', | ||
withProjection2: { | ||
type: 'Int', | ||
projection: { | ||
field2b: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
const Level1TC = TypeComposer.create({ | ||
name: 'Level1', | ||
fields: { | ||
field1a: [Level2TC], | ||
field1b: 'Int', | ||
withProjection1: { | ||
type: 'Int', | ||
projection: { | ||
field1b: true, | ||
field1a: { field2a: true }, | ||
}, | ||
}, | ||
}, | ||
}); | ||
const resolve = jest.fn(() => ({})); | ||
gqc.rootQuery().addFields({ field0: { type: Level1TC, resolve } }); | ||
const schema = gqc.buildSchema(); | ||
|
||
const getResolveInfo = async (query: string): Promise<GraphQLResolveInfo> => { | ||
resolve.mockClear(); | ||
const res = await graphql(schema, query); | ||
if (res && res.errors) { | ||
throw new Error(res.errors[0]); | ||
} | ||
return resolve.mock.calls[0][3]; | ||
}; | ||
|
||
describe('projection', () => { | ||
describe('getProjectionFromAST()', () => { | ||
it('simple query', async () => { | ||
const info = await getResolveInfo(` | ||
query { | ||
field0 { | ||
field1a { field2a } | ||
field1b | ||
} | ||
} | ||
`); | ||
expect(getProjectionFromAST(info)).toEqual({ | ||
field1a: { field2a: {} }, | ||
field1b: {}, | ||
}); | ||
}); | ||
|
||
it('inline fragments', async () => { | ||
const info = await getResolveInfo(` | ||
query { | ||
field0 { | ||
field1a { field2a } | ||
... { | ||
field1a { field2b } | ||
field1b | ||
} | ||
} | ||
} | ||
`); | ||
expect(getProjectionFromAST(info)).toEqual({ | ||
field1a: { field2a: {}, field2b: {} }, | ||
field1b: {}, | ||
}); | ||
}); | ||
|
||
it('fragment spreads', async () => { | ||
const info = await getResolveInfo(` | ||
query { | ||
field0 { | ||
...Frag | ||
field1b | ||
} | ||
} | ||
fragment Frag on Level1 { | ||
field1a { | ||
field2b | ||
} | ||
} | ||
`); | ||
expect(getProjectionFromAST(info)).toEqual({ | ||
field1a: { field2b: {} }, | ||
field1b: {}, | ||
}); | ||
}); | ||
|
||
it('fragment spreads with deep merge', async () => { | ||
const info = await getResolveInfo(` | ||
query { | ||
field0 { | ||
...Frag | ||
field1a { | ||
field2a | ||
} | ||
} | ||
} | ||
fragment Frag on Level1 { | ||
field1a { | ||
field2b | ||
} | ||
} | ||
`); | ||
expect(getProjectionFromAST(info)).toEqual({ | ||
field1a: { field2a: {}, field2b: {} }, | ||
}); | ||
}); | ||
|
||
it('extend by field.projection', async () => { | ||
const info = await getResolveInfo(` | ||
query { | ||
field0 { | ||
withProjection1 | ||
} | ||
} | ||
`); | ||
expect(getProjectionFromAST(info)).toEqual({ | ||
withProjection1: {}, | ||
field1b: true, | ||
field1a: { field2a: true }, | ||
}); | ||
}); | ||
|
||
it('extend by field.projection deep', async () => { | ||
const info = await getResolveInfo(` | ||
query { | ||
field0 { | ||
field1a { | ||
withProjection2 | ||
} | ||
} | ||
} | ||
`); | ||
// console.dir(info, { colors: true, depth: 3 }); | ||
expect(getProjectionFromAST(info)).toEqual({ | ||
field1a: { withProjection2: {}, field2b: true }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('extendByFieldProjection()', () => { | ||
it('first level', () => { | ||
const type: GraphQLObjectType = (schema.getType('Level1'): any); | ||
const projection = { | ||
withProjection1: true, | ||
}; | ||
expect(extendByFieldProjection(type, projection)).toEqual({ | ||
field1a: { field2a: true }, | ||
field1b: true, | ||
withProjection1: true, | ||
}); | ||
}); | ||
|
||
it('second level', () => { | ||
const type: GraphQLObjectType = (schema.getType('Level1'): any); | ||
const projection = { | ||
field1a: { withProjection2: {} }, | ||
}; | ||
expect(extendByFieldProjection(type, projection)).toEqual({ | ||
field1a: { field2b: true, withProjection2: {} }, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters