This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(merging): fixes merging for non root types
adds a canonical test
- Loading branch information
Showing
5 changed files
with
142 additions
and
23 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
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
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,110 @@ | ||
/* tslint:disable:no-unused-expression */ | ||
|
||
// The below is meant to be an alternative canonical schema stitching example | ||
// which relies on type merging. | ||
|
||
import { expect } from 'chai'; | ||
import { graphql } from 'graphql'; | ||
import { | ||
delegateToSchema, | ||
mergeSchemas, | ||
addMockFunctionsToSchema, | ||
makeExecutableSchema, | ||
} from '../index'; | ||
|
||
const chirpSchema = makeExecutableSchema({ | ||
typeDefs: ` | ||
type Chirp { | ||
id: ID! | ||
text: String | ||
author: User | ||
} | ||
type User { | ||
id: ID! | ||
chirps: [Chirp] | ||
} | ||
type Query { | ||
userById(id: ID!): User | ||
} | ||
`, | ||
}); | ||
|
||
addMockFunctionsToSchema({ schema: chirpSchema }); | ||
|
||
const authorSchema = makeExecutableSchema({ | ||
typeDefs: ` | ||
type User { | ||
id: ID! | ||
email: String | ||
} | ||
type Query { | ||
userById(id: ID!): User | ||
} | ||
`, | ||
}); | ||
|
||
addMockFunctionsToSchema({ schema: authorSchema }); | ||
|
||
const mergedSchema = mergeSchemas({ | ||
subschemas: [{ | ||
schema: chirpSchema, | ||
mergedTypeConfigs: { | ||
User: { | ||
selectionSet: '{ id }', | ||
merge: (originalResult, context, info, subschema, selectionSet) => delegateToSchema({ | ||
schema: subschema, | ||
operation: 'query', | ||
fieldName: 'userById', | ||
args: { id: originalResult.id }, | ||
selectionSet, | ||
context, | ||
info, | ||
skipTypeMerging: true, | ||
}), | ||
} | ||
}, | ||
}, { | ||
schema: authorSchema, | ||
mergedTypeConfigs: { | ||
User: { | ||
selectionSet: '{ id }', | ||
merge: (originalResult, context, info, subschema, selectionSet) => delegateToSchema({ | ||
schema: subschema, | ||
operation: 'query', | ||
fieldName: 'userById', | ||
args: { id: originalResult.id }, | ||
selectionSet, | ||
context, | ||
info, | ||
skipTypeMerging: true, | ||
}), | ||
} | ||
}, | ||
}], | ||
}); | ||
|
||
describe('merging using type merging', () => { | ||
it('works', async () => { | ||
const query = ` | ||
query { | ||
userById(id: 5) { | ||
chirps { | ||
id | ||
textAlias: text | ||
author { | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const result = await graphql(mergedSchema, query); | ||
|
||
expect(result.errors).to.be.undefined; | ||
expect(result.data.userById.chirps[1].id).to.not.be.null; | ||
expect(result.data.userById.chirps[1].text).to.not.be.null; | ||
expect(result.data.userById.chirps[1].author.email).to.not.be.null; | ||
}); | ||
}); |
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