mirror of
https://github.com/strapi/strapi.git
synced 2025-12-24 21:54:24 +00:00
keep graphql date value as a string
This commit is contained in:
parent
959be3e653
commit
f12c0ac4ee
@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const { GraphQLDate } = require('graphql-iso-date/dist');
|
||||
|
||||
const parseAndCast = parseFn => (...args) => {
|
||||
const parsedValue = parseFn(...args);
|
||||
|
||||
if (parsedValue instanceof Date) {
|
||||
return parsedValue.toISOString().split('T')[0];
|
||||
}
|
||||
|
||||
return parsedValue;
|
||||
};
|
||||
|
||||
// GraphQLDate casts the date string to new Date, we want to keep it as a string so we cast it back to a string
|
||||
// see https://github.com/excitement-engineer/graphql-iso-date/issues/106
|
||||
GraphQLDate.parseValue = parseAndCast(GraphQLDate.parseValue);
|
||||
GraphQLDate.parseLiteral = parseAndCast(GraphQLDate.parseLiteral);
|
||||
|
||||
module.exports = GraphQLDate;
|
||||
@ -2,11 +2,12 @@
|
||||
|
||||
const GraphQLJSON = require('graphql-type-json');
|
||||
const GraphQLLong = require('graphql-type-long');
|
||||
const { GraphQLDateTime, GraphQLDate } = require('graphql-iso-date/dist');
|
||||
const { GraphQLDateTime } = require('graphql-iso-date/dist');
|
||||
const { GraphQLUpload } = require('graphql-upload');
|
||||
const { asNexusMethod } = require('nexus');
|
||||
|
||||
const TimeScalar = require('./time');
|
||||
const GraphQLDate = require('./date');
|
||||
|
||||
module.exports = () => ({
|
||||
JSON: asNexusMethod(GraphQLJSON, 'json'),
|
||||
|
||||
117
packages/plugins/graphql/tests/fields/date.test.e2e.js
Normal file
117
packages/plugins/graphql/tests/fields/date.test.e2e.js
Normal file
@ -0,0 +1,117 @@
|
||||
'use strict';
|
||||
|
||||
// Helpers.
|
||||
const { createTestBuilder } = require('../../../../../test/helpers/builder');
|
||||
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
||||
const { createAuthRequest } = require('../../../../../test/helpers/request');
|
||||
|
||||
const builder = createTestBuilder();
|
||||
let strapi;
|
||||
let rq;
|
||||
let graphqlQuery;
|
||||
|
||||
const postModel = {
|
||||
attributes: {
|
||||
myDate: {
|
||||
type: 'date',
|
||||
},
|
||||
},
|
||||
singularName: 'post',
|
||||
pluralName: 'posts',
|
||||
displayName: 'Post',
|
||||
description: '',
|
||||
collectionName: '',
|
||||
};
|
||||
|
||||
describe('Test Graphql API End to End', () => {
|
||||
beforeAll(async () => {
|
||||
await builder.addContentType(postModel).build();
|
||||
|
||||
strapi = await createStrapiInstance();
|
||||
rq = await createAuthRequest({ strapi });
|
||||
|
||||
graphqlQuery = body => {
|
||||
return rq({
|
||||
url: '/graphql',
|
||||
method: 'POST',
|
||||
body,
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await strapi.destroy();
|
||||
await builder.cleanup();
|
||||
});
|
||||
|
||||
describe('GraphQL - Date field', () => {
|
||||
test.each(['2022-03-17', null])('Can create an entity with date equals: %s', async value => {
|
||||
const res = await graphqlQuery({
|
||||
query: /* GraphQL */ `
|
||||
mutation createPost($data: PostInput!) {
|
||||
createPost(data: $data) {
|
||||
data {
|
||||
attributes {
|
||||
myDate
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
data: {
|
||||
myDate: value,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const { body } = res;
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(body).toEqual({
|
||||
data: {
|
||||
createPost: {
|
||||
data: {
|
||||
attributes: { myDate: value },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test.each(['2022-03-17T15:06:57.878Z', {}, [], 'something'])(
|
||||
'Cannot create an entity with date equals: %s',
|
||||
async value => {
|
||||
const res = await graphqlQuery({
|
||||
query: /* GraphQL */ `
|
||||
mutation createPost($data: PostInput!) {
|
||||
createPost(data: $data) {
|
||||
data {
|
||||
attributes {
|
||||
myDate
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
data: {
|
||||
myDate: value,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const { body } = res;
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(body).toMatchObject({
|
||||
errors: [
|
||||
{
|
||||
extensions: { code: 'BAD_USER_INPUT' },
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user