graphql-compose-relay
Version:
Plugin for `graphql-compose` which wraps graphql types with Relay specific logic.
105 lines (97 loc) • 4.15 kB
JavaScript
;
var _graphqlCompose = require("graphql-compose");
var _graphql = require("graphql-compose/lib/graphql");
var _composeWithRelay = require("../composeWithRelay");
var _userTC = require("../__mocks__/userTC");
var _globalId = require("../globalId");
describe('composeWithRelay', () => {
const userComposer = (0, _composeWithRelay.composeWithRelay)(_userTC.userTC);
const queryTC = (0, _composeWithRelay.composeWithRelay)(_graphqlCompose.schemaComposer.Query);
const mutationTC = (0, _composeWithRelay.composeWithRelay)(_graphqlCompose.schemaComposer.Mutation);
describe('basic checks', () => {
it('should return ObjectTypeComposer', () => {
expect(userComposer).toBeInstanceOf(_graphqlCompose.ObjectTypeComposer);
});
it('should throw error if got a not ObjectTypeComposer', () => {
expect(() => (0, _composeWithRelay.composeWithRelay)(123)).toThrowError('should provide ObjectTypeComposer instance');
});
it('should throw error if ObjectTypeComposer without recordIdFn', () => {
const tc = _userTC.userTC.clone('AnotherUserType2');
delete tc._gqcGetRecordIdFn;
expect(() => (0, _composeWithRelay.composeWithRelay)(tc)).toThrowError('should have recordIdFn');
});
it('should thow error if typeComposer does not have findById resolver', () => {
const tc = _userTC.userTC.clone('AnotherUserType');
tc.removeResolver('findById');
expect(() => (0, _composeWithRelay.composeWithRelay)(tc)).toThrowError("does not have resolver with name 'findById'");
});
});
describe('when pass RootQuery type composer', () => {
it('should add `node` field to RootQuery', () => {
const nodeField = queryTC.getField('node');
expect(nodeField.type.getType()).toBeInstanceOf(_graphql.GraphQLInterfaceType);
expect(nodeField.type.getTypeName()).toBe('Node');
});
});
describe('when pass User type composer (not RootQuery)', () => {
it('should add or override id field', () => {
const idField = userComposer.getFieldConfig('id');
expect(idField.description).toContain('globally unique ID');
});
it('should make id field NonNull', () => {
const idField = userComposer.getFieldConfig('id');
expect(idField.type).toBeInstanceOf(_graphql.GraphQLNonNull);
});
it('should resolve globalId in `user.id` field', async () => {
queryTC.setField('user', _userTC.userTC.getResolver('findById'));
const schema = new _graphql.GraphQLSchema({
query: queryTC.getType()
});
const query = `{
user(_id: 1) {
id
name
}
}`;
const result = await (0, _graphql.graphql)(schema, query);
expect(result.data.user.id).toBe((0, _globalId.toGlobalId)('User', 1));
expect(result.data.user.name).toBe('Pavel');
});
it('should resolve globalId in `node.id` field', async () => {
queryTC.setField('user', _userTC.userTC.getResolver('findById'));
const schema = new _graphql.GraphQLSchema({
query: queryTC.getType()
});
const query = `{
node(id: "${(0, _globalId.toGlobalId)('User', 1)}") {
...user
}
}
fragment user on User {
id
name
}`;
const result = await (0, _graphql.graphql)(schema, query);
expect(result.data.node.id).toBe((0, _globalId.toGlobalId)('User', 1));
expect(result.data.node.name).toBe('Pavel');
});
it('should passthru clientMutationId in mutations', async () => {
mutationTC.setField('createUser', _userTC.userTC.getResolver('createOne'));
const schema = new _graphql.GraphQLSchema({
query: queryTC.getType(),
mutation: mutationTC.getType()
});
const query = `mutation {
createUser(input: { name: "Ok", clientMutationId: "123" }) {
record {
name
}
clientMutationId
}
}`;
const result = await (0, _graphql.graphql)(schema, query);
expect(result.data.createUser.record.name).toBe('Ok');
expect(result.data.createUser.clientMutationId).toBe('123');
});
});
});