test(int): collections-graphl
This commit is contained in:
@@ -8,40 +8,10 @@
|
||||
export interface Config {}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "access-controls".
|
||||
* via the `definition` "slugname".
|
||||
*/
|
||||
export interface AccessControl {
|
||||
export interface Slugname {
|
||||
id: string;
|
||||
restrictedField?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "restricted".
|
||||
*/
|
||||
export interface Restricted {
|
||||
id: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "read-only-collection".
|
||||
*/
|
||||
export interface ReadOnlyCollection {
|
||||
id: string;
|
||||
name?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "restricted-versions".
|
||||
*/
|
||||
export interface RestrictedVersion {
|
||||
id: string;
|
||||
name?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,186 @@
|
||||
import { openAccess } from '../helpers/configHelpers';
|
||||
import type { CollectionConfig } from '../../src/collections/config/types';
|
||||
import { devUser } from '../credentials';
|
||||
import { buildConfig } from '../buildConfig';
|
||||
import type { Post } from './payload-types';
|
||||
|
||||
export default buildConfig({
|
||||
collections: [{
|
||||
slug: 'posts',
|
||||
export interface Relation {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
const openAccess = {
|
||||
create: () => true,
|
||||
read: () => true,
|
||||
update: () => true,
|
||||
delete: () => true,
|
||||
};
|
||||
|
||||
const collectionWithName = (collectionSlug: string): CollectionConfig => {
|
||||
return {
|
||||
slug: collectionSlug,
|
||||
access: openAccess,
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
}],
|
||||
};
|
||||
};
|
||||
|
||||
export const slug = 'posts';
|
||||
export const relationSlug = 'relation';
|
||||
export default buildConfig({
|
||||
collections: [
|
||||
{
|
||||
slug,
|
||||
access: openAccess,
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
type: 'text',
|
||||
},
|
||||
{
|
||||
name: 'description',
|
||||
type: 'text',
|
||||
},
|
||||
{
|
||||
name: 'number',
|
||||
type: 'number',
|
||||
},
|
||||
// Relationship
|
||||
{
|
||||
name: 'relationField',
|
||||
type: 'relationship',
|
||||
relationTo: relationSlug,
|
||||
},
|
||||
// Relation hasMany
|
||||
{
|
||||
name: 'relationHasManyField',
|
||||
type: 'relationship',
|
||||
relationTo: relationSlug,
|
||||
hasMany: true,
|
||||
},
|
||||
// Relation multiple relationTo
|
||||
{
|
||||
name: 'relationMultiRelationTo',
|
||||
type: 'relationship',
|
||||
relationTo: [relationSlug, 'dummy'],
|
||||
},
|
||||
// Relation multiple relationTo hasMany
|
||||
{
|
||||
name: 'relationMultiRelationToHasMany',
|
||||
type: 'relationship',
|
||||
relationTo: [relationSlug, 'dummy'],
|
||||
hasMany: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
collectionWithName(relationSlug),
|
||||
collectionWithName('dummy'),
|
||||
],
|
||||
onInit: async (payload) => {
|
||||
await payload.create({
|
||||
collection: 'users',
|
||||
data: {
|
||||
email: devUser.email,
|
||||
password: devUser.password,
|
||||
},
|
||||
});
|
||||
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'post1',
|
||||
},
|
||||
});
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'post2',
|
||||
},
|
||||
});
|
||||
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'with-description',
|
||||
description: 'description',
|
||||
},
|
||||
});
|
||||
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'numPost1',
|
||||
number: 1,
|
||||
},
|
||||
});
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'numPost2',
|
||||
number: 2,
|
||||
},
|
||||
});
|
||||
|
||||
const rel1 = await payload.create<Relation>({
|
||||
collection: relationSlug,
|
||||
data: {
|
||||
name: 'name',
|
||||
},
|
||||
});
|
||||
const rel2 = await payload.create<Relation>({
|
||||
collection: relationSlug,
|
||||
data: {
|
||||
name: 'name2',
|
||||
},
|
||||
});
|
||||
|
||||
// Relation - hasMany
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'rel to hasMany',
|
||||
relationHasManyField: rel1.id,
|
||||
},
|
||||
});
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'rel to hasMany 2',
|
||||
relationHasManyField: rel2.id,
|
||||
},
|
||||
});
|
||||
|
||||
// Relation - relationTo multi
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'rel to multi',
|
||||
relationMultiRelationTo: {
|
||||
relationTo: relationSlug,
|
||||
value: rel2.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Relation - relationTo multi hasMany
|
||||
await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: {
|
||||
title: 'rel to multi hasMany',
|
||||
relationMultiRelationToHasMany: [
|
||||
{
|
||||
relationTo: relationSlug,
|
||||
value: rel1.id,
|
||||
},
|
||||
{
|
||||
relationTo: relationSlug,
|
||||
value: rel2.id,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { GraphQLClient } from 'graphql-request';
|
||||
import { initPayloadTest } from '../helpers/configHelpers';
|
||||
import config from './config';
|
||||
import payload from '../../src';
|
||||
import { RESTClient } from '../helpers/rest';
|
||||
import type { Post } from './payload-types';
|
||||
|
||||
const collection = config.collections[0]?.slug;
|
||||
const slug = config.collections[0]?.slug;
|
||||
const title = 'title';
|
||||
|
||||
let client: RESTClient;
|
||||
let client: GraphQLClient;
|
||||
|
||||
describe('collections-graphql', () => {
|
||||
beforeAll(async () => {
|
||||
const { serverURL } = await initPayloadTest({ __dirname, init: { local: false } });
|
||||
client = new RESTClient(config, { serverURL, defaultSlug: collection });
|
||||
const url = `${serverURL}${config.routes.api}${config.routes.graphQL}`;
|
||||
client = new GraphQLClient(url);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -21,24 +23,340 @@ describe('collections-graphql', () => {
|
||||
await payload.mongoMemoryServer.stop();
|
||||
});
|
||||
|
||||
it('should create', async () => {
|
||||
const title = 'hello';
|
||||
describe('CRUD', () => {
|
||||
let existingDoc: Post;
|
||||
|
||||
const { doc } = await client.create<Post>({
|
||||
slug: collection,
|
||||
data: {
|
||||
title,
|
||||
},
|
||||
beforeEach(async () => {
|
||||
existingDoc = await createPost();
|
||||
});
|
||||
|
||||
expect(doc.title).toStrictEqual(title);
|
||||
it('should create', async () => {
|
||||
const query = `mutation {
|
||||
createPost(data: {title: "${title}"}) {
|
||||
id
|
||||
title
|
||||
}
|
||||
}`;
|
||||
const response = await client.request(query);
|
||||
const doc: Post = response.createPost;
|
||||
|
||||
expect(doc).toMatchObject({ title });
|
||||
expect(doc.id.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should read', async () => {
|
||||
const query = `query {
|
||||
Post(id: "${existingDoc.id}") {
|
||||
id
|
||||
title
|
||||
}
|
||||
}`;
|
||||
const response = await client.request(query);
|
||||
const doc: Post = response.Post;
|
||||
|
||||
expect(doc).toMatchObject({ id: existingDoc.id, title });
|
||||
});
|
||||
|
||||
it('should find', async () => {
|
||||
const query = `query {
|
||||
Posts {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: existingDoc.id }));
|
||||
});
|
||||
|
||||
it('should update existing', async () => {
|
||||
const updatedTitle = 'updated title';
|
||||
|
||||
const query = `mutation {
|
||||
updatePost(id: "${existingDoc.id}", data: { title: "${updatedTitle}"}) {
|
||||
id
|
||||
title
|
||||
}
|
||||
}`;
|
||||
const response = await client.request(query);
|
||||
const doc: Post = response.updatePost;
|
||||
|
||||
expect(doc).toMatchObject({ id: existingDoc.id, title: updatedTitle });
|
||||
});
|
||||
|
||||
it('should delete', async () => {
|
||||
const query = `mutation {
|
||||
deletePost(id: "${existingDoc.id}") {
|
||||
id
|
||||
title
|
||||
}
|
||||
}`;
|
||||
const response = await client.request(query);
|
||||
const doc: Post = response.deletePost;
|
||||
|
||||
expect(doc).toMatchObject({ id: existingDoc.id });
|
||||
});
|
||||
});
|
||||
|
||||
it('should find', async () => {
|
||||
const { result } = await client.find({
|
||||
slug: collection,
|
||||
describe('Operators', () => {
|
||||
let post1: Post;
|
||||
let post2: Post;
|
||||
|
||||
beforeEach(async () => {
|
||||
post1 = await createPost({ title: 'post1' });
|
||||
post2 = await createPost({ title: 'post2' });
|
||||
});
|
||||
|
||||
expect(result.totalDocs).toStrictEqual(1);
|
||||
it('equals', async () => {
|
||||
const query = `query {
|
||||
Posts(where:{title: {equals:"${post1.title}"}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: post1.id, title: post1.title }));
|
||||
});
|
||||
|
||||
it('not_equals', async () => {
|
||||
const query = `query {
|
||||
Posts(where:{title: {not_equals:"${post1.title}"}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs[0]).toMatchObject({ id: post2.id, title: post2.title });
|
||||
});
|
||||
|
||||
it('like', async () => {
|
||||
const postWithWords = await createPost({ title: 'the quick brown fox' });
|
||||
const query = `query {
|
||||
Posts(where:{title: {like:"${postWithWords.title?.split(' ')[1]}"}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs[0]).toMatchObject({ id: postWithWords.id, title: postWithWords.title });
|
||||
});
|
||||
|
||||
it('contains', async () => {
|
||||
const query = `query {
|
||||
Posts(where:{title: {contains:"${post1.title?.slice(0, 4)}"}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: post1.id, title: post1.title }));
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: post2.id, title: post2.title }));
|
||||
});
|
||||
|
||||
it('exists - true', async () => {
|
||||
const withDescription = await createPost({ description: 'description' });
|
||||
const query = `query {
|
||||
Posts(where:{description: {exists:true}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(
|
||||
expect.objectContaining({ id: withDescription.id, title: withDescription.title }),
|
||||
);
|
||||
});
|
||||
|
||||
it('exists - false', async () => {
|
||||
const withDescription = await createPost({ description: 'description' });
|
||||
const query = `query {
|
||||
Posts(where:{description: {exists:false}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).not.toContainEqual(expect.objectContaining({ id: withDescription.id }));
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: post1.id }));
|
||||
});
|
||||
|
||||
describe('numbers', () => {
|
||||
let numPost1: Post;
|
||||
let numPost2: Post;
|
||||
|
||||
beforeEach(async () => {
|
||||
numPost1 = await createPost({ number: 1 });
|
||||
numPost2 = await createPost({ number: 2 });
|
||||
});
|
||||
|
||||
it('greater_than', async () => {
|
||||
const query = `query {
|
||||
Posts(where:{number: {greater_than:1}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: numPost2.id }));
|
||||
});
|
||||
|
||||
it('greater_than_equal', async () => {
|
||||
const query = `query {
|
||||
Posts(where:{number: {greater_than_equal:1}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: numPost1.id }));
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: numPost2.id }));
|
||||
});
|
||||
|
||||
it('less_than', async () => {
|
||||
const query = `query {
|
||||
Posts(where:{number: {less_than:2}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: numPost1.id }));
|
||||
});
|
||||
|
||||
it('less_than_equal', async () => {
|
||||
const query = `query {
|
||||
Posts(where:{number: {less_than_equal:2}}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: numPost1.id }));
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: numPost2.id }));
|
||||
});
|
||||
});
|
||||
|
||||
it('or', async () => {
|
||||
const query = `query {
|
||||
Posts(
|
||||
where: {OR: [{ title: { equals: "${post1.title}" } }, { title: { equals: "${post2.title}" } }]
|
||||
}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: post1.id }));
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: post2.id }));
|
||||
});
|
||||
|
||||
it('or - 1 result', async () => {
|
||||
const query = `query {
|
||||
Posts(
|
||||
where: {OR: [{ title: { equals: "${post1.title}" } }, { title: { equals: "nope" } }]
|
||||
}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: post1.id }));
|
||||
expect(docs).not.toContainEqual(expect.objectContaining({ id: post2.id }));
|
||||
});
|
||||
|
||||
it('and', async () => {
|
||||
const specialPost = await createPost({ description: 'special-123123' });
|
||||
|
||||
const query = `query {
|
||||
Posts(
|
||||
where: {
|
||||
AND: [
|
||||
{ title: { equals: "${specialPost.title}" } }
|
||||
{ description: { equals: "${specialPost.description}" } }
|
||||
]
|
||||
}) {
|
||||
docs {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const response = await client.request(query);
|
||||
const { docs } = response.Posts;
|
||||
|
||||
expect(docs).toContainEqual(expect.objectContaining({ id: specialPost.id }));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
async function createPost(overrides?: Partial<Post>) {
|
||||
const doc = await payload.create<Post>({
|
||||
collection: slug,
|
||||
data: { title: 'title', ...overrides },
|
||||
});
|
||||
return doc;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,49 @@ export interface Config {}
|
||||
export interface Post {
|
||||
id: string;
|
||||
title?: string;
|
||||
description?: string;
|
||||
number?: number;
|
||||
relationField?: string | Relation;
|
||||
relationHasManyField?: (string | Relation)[];
|
||||
relationMultiRelationTo?:
|
||||
| {
|
||||
value: string | Relation;
|
||||
relationTo: 'relation';
|
||||
}
|
||||
| {
|
||||
value: string | Dummy;
|
||||
relationTo: 'dummy';
|
||||
};
|
||||
relationMultiRelationToHasMany?: (
|
||||
| {
|
||||
value: string | Relation;
|
||||
relationTo: 'relation';
|
||||
}
|
||||
| {
|
||||
value: string | Dummy;
|
||||
relationTo: 'dummy';
|
||||
}
|
||||
)[];
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "relation".
|
||||
*/
|
||||
export interface Relation {
|
||||
id: string;
|
||||
name?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "dummy".
|
||||
*/
|
||||
export interface Dummy {
|
||||
id: string;
|
||||
name?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user