Skip to content

Commit

Permalink
chore: migrate graphql plugin to v5
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Mar 6, 2024
1 parent 04432e8 commit 508ce01
Show file tree
Hide file tree
Showing 53 changed files with 651 additions and 775 deletions.
94 changes: 46 additions & 48 deletions api-tests/plugins/graphql/crud.test.api.js
Expand Up @@ -71,8 +71,10 @@ describe('Test Graphql API End to End', () => {
mutation createPost($data: PostInput!) {
createPost(data: $data) {
data {
documentId
attributes {
name
publishedAt
bigint
nullable
category
Expand All @@ -93,7 +95,11 @@ describe('Test Graphql API End to End', () => {
data: {
createPost: {
data: {
attributes: post,
documentId: expect.any(String),
attributes: {
...post,
publishedAt: expect.any(String),
},
},
},
},
Expand All @@ -106,7 +112,7 @@ describe('Test Graphql API End to End', () => {
{
posts {
data {
id
documentId
attributes {
name
bigint
Expand All @@ -126,15 +132,18 @@ describe('Test Graphql API End to End', () => {
data: {
posts: {
data: postsPayload.map((entry) => ({
id: expect.any(String),
attributes: omit('id', entry),
documentId: expect.any(String),
attributes: omit('documentId', entry),
})),
},
},
});

// assign for later use
data.posts = res.body.data.posts.data.map(({ id, attributes }) => ({ id, ...attributes }));
data.posts = res.body.data.posts.data.map(({ documentId, attributes }) => ({
documentId,
...attributes,
}));
});

test('List posts with limit', async () => {
Expand All @@ -143,7 +152,7 @@ describe('Test Graphql API End to End', () => {
{
posts(pagination: { limit: 1 }) {
data {
id
documentId
attributes {
name
bigint
Expand All @@ -164,8 +173,8 @@ describe('Test Graphql API End to End', () => {
posts: {
data: [
{
id: expectedPost.id,
attributes: omit('id', expectedPost),
documentId: expectedPost.documentId,
attributes: omit('documentId', expectedPost),
},
],
},
Expand All @@ -179,7 +188,7 @@ describe('Test Graphql API End to End', () => {
{
posts(sort: "name:desc") {
data {
id
documentId
attributes {
name
bigint
Expand All @@ -193,8 +202,8 @@ describe('Test Graphql API End to End', () => {
});

const expectedPosts = [...data.posts].reverse().map((entry) => ({
id: expect.any(String),
attributes: omit('id', entry),
documentId: expect.any(String),
attributes: omit('documentId', entry),
}));

expect(res.statusCode).toBe(200);
Expand All @@ -213,7 +222,7 @@ describe('Test Graphql API End to End', () => {
{
posts(pagination: { start: 1 }) {
data {
id
documentId
attributes {
name
bigint
Expand All @@ -234,8 +243,8 @@ describe('Test Graphql API End to End', () => {
posts: {
data: [
{
id: expectedPost.id,
attributes: omit('id', expectedPost),
documentId: expectedPost.documentId,
attributes: omit('documentId', expectedPost),
},
],
},
Expand All @@ -249,7 +258,7 @@ describe('Test Graphql API End to End', () => {
{
posts(filters: { name: { eq: "post 2" } }) {
data {
id
documentId
attributes {
name
bigint
Expand Down Expand Up @@ -278,8 +287,8 @@ describe('Test Graphql API End to End', () => {
posts: {
data: [
{
id: expectedPost.id,
attributes: omit('id', expectedPost),
documentId: expectedPost.documentId,
attributes: omit('documentId', expectedPost),
},
],
meta: {
Expand All @@ -301,7 +310,7 @@ describe('Test Graphql API End to End', () => {
{
posts(start: 1) {
data {
id
documentId
attributes {
name
bigint
Expand Down Expand Up @@ -447,24 +456,24 @@ describe('Test Graphql API End to End', () => {

// all the posts returned are in the expected array
posts.map(prop('attributes')).forEach((post) => {
expect(expected.map(omit('id'))).toEqual(expect.arrayContaining([post]));
expect(expected.map(omit('documentId'))).toEqual(expect.arrayContaining([post]));
});

// all expected values are in the result
expected.forEach((expectedPost) => {
expect(posts.map(prop('attributes'))).toEqual(
expect.arrayContaining([omit('id', expectedPost)])
expect.arrayContaining([omit('documentId', expectedPost)])
);
});
});

test('Get One Post', async () => {
const res = await graphqlQuery({
query: /* GraphQL */ `
query getPost($id: ID!) {
post(id: $id) {
query getPost($documentId: ID!) {
post(documentId: $documentId) {
data {
id
documentId
attributes {
name
bigint
Expand All @@ -476,7 +485,7 @@ describe('Test Graphql API End to End', () => {
}
`,
variables: {
id: data.posts[0].id,
documentId: data.posts[0].documentId,
},
});

Expand All @@ -485,8 +494,8 @@ describe('Test Graphql API End to End', () => {
data: {
post: {
data: {
id: data.posts[0].id,
attributes: omit('id', data.posts[0]),
documentId: data.posts[0].documentId,
attributes: omit('documentId', data.posts[0]),
},
},
},
Expand All @@ -497,10 +506,10 @@ describe('Test Graphql API End to End', () => {
const newName = 'new post name';
const res = await graphqlQuery({
query: /* GraphQL */ `
mutation updatePost($id: ID!, $data: PostInput!) {
updatePost(id: $id, data: $data) {
mutation updatePost($documentId: ID!, $data: PostInput!) {
updatePost(documentId: $documentId, data: $data) {
data {
id
documentId
attributes {
name
}
Expand All @@ -509,7 +518,7 @@ describe('Test Graphql API End to End', () => {
}
`,
variables: {
id: data.posts[0].id,
documentId: data.posts[0].documentId,
data: {
name: newName,
},
Expand All @@ -521,7 +530,7 @@ describe('Test Graphql API End to End', () => {
data: {
updatePost: {
data: {
id: data.posts[0].id,
documentId: data.posts[0].documentId,
attributes: {
name: newName,
},
Expand All @@ -533,7 +542,7 @@ describe('Test Graphql API End to End', () => {
const newPost = res.body.data.updatePost.data;

data.posts[0] = {
id: newPost.id,
documentId: newPost.documentId,
...newPost.attributes,
};
});
Expand All @@ -542,33 +551,22 @@ describe('Test Graphql API End to End', () => {
for (const post of data.posts) {
const res = await graphqlQuery({
query: /* GraphQL */ `
mutation deletePost($id: ID!) {
deletePost(id: $id) {
data {
id
attributes {
name
nullable
bigint
category
}
}
mutation deletePost($documentId: ID!) {
deletePost(documentId: $documentId) {
documentId
}
}
`,
variables: {
id: post.id,
documentId: post.documentId,
},
});

expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
data: {
deletePost: {
data: {
id: post.id,
attributes: omit('id', post),
},
documentId: post.documentId,
},
},
});
Expand Down

0 comments on commit 508ce01

Please sign in to comment.