Skip to content

Commit

Permalink
Added EJSON scalar & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorDiaconu committed Apr 9, 2019
1 parent 0458ccb commit 3759877
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions __tests__/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './morpher/client';
import './accounts/client';
import './default/client';
import './ejson/client';
30 changes: 30 additions & 0 deletions __tests__/ejson/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import client, { wsLink } from '../apolloClient';
import gql from 'graphql-tag';
import { loginWithPassword, logout } from 'meteor-apollo-accounts';
import { resolve } from 'dns';
import { EJSON } from 'meteor/ejson';

const PASSWORD = '12345';

describe('EJSON', () => {
it('Should work properly', async () => {
const response = await client.query({
query: gql`
query($input: EJSON) {
jsonTest(input: $input)
}
`,
variables: {
input: EJSON.stringify({
name: 'john',
date: new Date(),
}),
},
});

const data = EJSON.parse(response.data.jsonTest);

assert.isString(data.name);
assert.isTrue(data.date instanceof Date);
});
});
22 changes: 22 additions & 0 deletions __tests__/ejson/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { load } from 'meteor/cultofcoders:apollo';

load({
typeDefs: `
type Query {
jsonTest(input: EJSON): EJSON
}
`,
resolvers: {
Query: {
jsonTest(_, { input }, { userId }) {
console.log(`input`, input);

return {
...input,
};
},
},
},
});
1 change: 1 addition & 0 deletions __tests__/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './graphql/init';
import './morpher/server';
import './accounts/server';
import './default/server';
import './ejson/server';

initialize({
context: async () => ({
Expand Down
7 changes: 4 additions & 3 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Package.onUse(function(api) {
api.use('mongo');

api.use('tmeasday:check-npm-versions@0.3.2');
api.use('cultofcoders:grapher@1.3.7_4');
api.use('cultofcoders:grapher@1.3.10');
api.use('cultofcoders:grapher-schema-directives@0.1.8');
api.use('accounts-base', { weak: true });

Expand All @@ -31,13 +31,14 @@ Package.onTest(function(api) {

var packages = [
'ecmascript',
'cultofcoders:mocha',
'practicalmeteor:chai',
'accounts-password',
'cultofcoders:apollo-accounts',
'cultofcoders:apollo-accounts@3.4.0',
'mongo',
];

api.use(['meteortesting:mocha']);

api.use(packages);

api.mainModule('__tests__/server.js', 'server');
Expand Down
21 changes: 21 additions & 0 deletions server/scalars/EJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';
import { EJSON } from 'meteor/ejson';

export default new GraphQLScalarType({
name: 'EJSON',
description: 'EJSON custom scalar type',
parseValue(value) {
return EJSON.parse(value);
},
serialize(value) {
return EJSON.stringify(value);
},
parseLiteral(ast) {
if (ast.kind == Kind.STRING) {
return EJSON.stringify(value);
}

return null;
},
});
2 changes: 2 additions & 0 deletions server/scalars/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import Scalars from './scalars';
import DateResolver from './Date';
import GraphQLJSON from 'graphql-type-json';
import EJSON from './EJSON';
import { load } from 'graphql-load';

const typeDefs = [Scalars];
const resolvers = [
{
Date: DateResolver,
JSON: GraphQLJSON,
EJSON,
},
];

Expand Down
1 change: 1 addition & 0 deletions server/scalars/scalars.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default `
scalar Date
scalar JSON
scalar EJSON
`;

0 comments on commit 3759877

Please sign in to comment.