import SchemaTransformer from '@tcwd/jsonschema-graphql'; import { Wedadata } from '@tcwd/wedadata.js'; import { graphql } from 'graphql'; export const models = [ { name: 'article', type: 'database', schema: { properties: { _id: { type: 'string', }, title: { type: 'string', }, author: { type: 'string', format: 'related', 'x-parent': { parentDataSourceName: 'writer', }, 'x-relation': { type: 'oneway', target: 'writer', }, }, tags: { type: 'array', format: 'related', 'x-parent': { parentDataSourceName: 'tag', }, items: { type: 'string', }, 'x-relation': { type: 'manyway', target: 'tag', }, }, // arrayObject: { // type: 'array', // title: 'inlineTag', // items: { // type: 'object', // properties: { // name: { // type: 'string', // }, // }, // }, // }, }, 'x-defaultMethods': ['wedaGetItem', 'wedaGetList', 'wedaGetRecords', 'wedaCreate', 'wedaUpdate', 'wedaDelete'], }, }, { name: 'writer', type: 'database', schema: { properties: { _id: { type: 'string', }, name: { type: 'string', }, }, 'x-defaultMethods': ['wedaGetItem', 'wedaGetList', 'wedaGetRecords', 'wedaCreate', 'wedaUpdate', 'wedaDelete'], }, }, { name: 'tag', type: 'database', schema: { properties: { _id: { type: 'string', }, name: { type: 'string', }, }, 'x-defaultMethods': ['wedaGetItem', 'wedaGetList', 'wedaGetRecords', 'wedaCreate', 'wedaUpdate', 'wedaDelete'], }, }, ]; export function runCase(fetchers) { const transformer = new SchemaTransformer(models, { fetchers, }); const schema = transformer.getSchema(); const client = new Wedadata({ async fetchData(source, variables) { return graphql({ schema, source, variableValues: variables, }) as any; }, }); const article = client.dataSources.article; const writer = client.dataSources.writer; const tag = client.dataSources.tag; let _id0; let title = Math.random() + ''; test('create', async () => { const res = await article.create({ title, }); _id0 = res._id; expect(_id0).toBeTruthy(); }); test('getItem', async () => { const res = await article.getItem({ _id: _id0, }); expect(res._id).toEqual(_id0); expect(res.title).toEqual(title); const res0 = await article.getItem({ title, }); expect(res0._id).toEqual(_id0); expect(res0.title).toEqual(title); }); const newStr = Math.random() + ''; test('update', async () => { const res = await article.update({ where: { _id: _id0 }, data: { title: newStr, }, }); expect(res.updated).toEqual(1); const item = await article.getItem({ _id: _id0, }); expect(item.title).toEqual(newStr); }); let _id1, _id2; const str1 = Math.random() + ''; test('getList', async () => { await article.create({ title: str1, }); await article.create({ title: str1, }); const res = await article.getList(); expect(res.length).toEqual(3); _id1 = res[1]._id; _id2 = res[2]._id; expect(res[1].title).toEqual(str1); expect(res[2].title).toEqual(str1); const res0 = await article.getItem({ title: str1, }); expect(res0._id).toEqual(_id1); expect(res0.title).toEqual(str1); }); test('getRecords', async () => { const res = await article.getRecords({ where: { title: str1, }, }); expect(res.total).toEqual(2); expect(res.records[0]._id).toEqual(_id1); expect(res.records[1]._id).toEqual(_id2); }); test('delete', async () => { const res = await article.delete({ _id: _id0, }); expect(res.deleted).toEqual(1); await article.delete({ title: str1, }); const { records, total } = await article.getRecords(); expect(total).toEqual(0); expect(records.length).toEqual(0); }); describe('关联', () => { let authorName = `justan${Math.random()}`; let title = `title${Math.random()}`; let tagname = `tag${Math.random()}`; it('onyway', async () => { const resy = await writer.create({ name: authorName, }); expect(resy._id).toBeTruthy(); const resx = await article.create({ title, author: resy._id, }); expect(resx._id).toBeTruthy(); const itemx = await article.getItem( { _id: resx._id, }, { fields: `{ _id title author { _id name } } `, }, ); // console.debug(itemx); expect(itemx._id).toEqual(resx._id); expect(itemx.title).toEqual(title); expect(itemx.author._id).toEqual(resy._id); expect(itemx.author.name).toEqual(authorName); }); it('manyway', async () => { const resz = await tag.create({ name: tagname, }); expect(resz._id).toBeTruthy(); const resx = await article.create({ title, tags: [resz._id], }); expect(resx._id).toBeTruthy(); const itemx = await article.getItem( { _id: resx._id, }, { fields: `{ _id title tags { _id name } } `, }, ); // console.debug(itemx); expect(itemx.tags.length).toEqual(1); expect(itemx.tags[0]._id).toEqual(resz._id); expect(itemx.tags[0].name).toEqual(tagname); }); }); }