Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement GetYdoc query and Ydoc model #316

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/graphql/models/Ydoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql';

import YdocVersion from './YdocVersion';

const Ydoc = new GraphQLObjectType({
name: 'Ydoc',
fields: () => ({
data: {
type: GraphQLString,
// https://www.elastic.co/guide/en/elasticsearch/reference/current/binary.html
description: 'Binary that stores as base64 encoded string',
},
versions: {
type: new GraphQLList(YdocVersion),
description:
'Ydoc snapshots which are used to restore to specific version',
resolve: async ({ versions }) => {
return versions || [];
},
},
}),
});

export default Ydoc;
13 changes: 13 additions & 0 deletions src/graphql/models/YdocVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GraphQLObjectType, GraphQLString } from 'graphql';

export default new GraphQLObjectType({
name: 'YdocVersion',
fields: () => ({
createdAt: { type: GraphQLString },
snapshot: {
type: GraphQLString,
// https://www.elastic.co/guide/en/elasticsearch/reference/current/binary.html
description: 'Binary that stores as base64 encoded string',
},
}),
});
12 changes: 12 additions & 0 deletions src/graphql/queries/GetYdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';

import Ydoc from 'graphql/models/Ydoc';

export default {
type: Ydoc,
args: {
id: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: async (rootValue, { id }, { loaders }) =>
loaders.docLoader.load({ index: 'ydocs', id }),
};
26 changes: 26 additions & 0 deletions src/graphql/queries/__fixtures__/GetYdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
'/ydocs/doc/foo': {
data: 'mock data1',
versions: [
{
createdAt: '2023-09-07T08:14:14.005Z',
snapshot: 'mock snapshot1',
},
{
createdAt: '2023-09-07T08:16:45.613Z',
snapshot: 'mock snapshot2',
},
{
createdAt: '2023-09-07T08:18:32.467Z',
snapshot: 'mock snapshot3',
},
{
createdAt: '2023-09-07T08:18:49.500Z',
snapshot: 'mock snapshot4',
},
],
},
'/ydocs/doc/foo2': {
data: 'mock data2',
},
};
40 changes: 40 additions & 0 deletions src/graphql/queries/__tests__/GetYdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import gql from 'util/GraphQL';
import { loadFixtures, unloadFixtures } from 'util/fixtures';
import fixtures from '../__fixtures__/GetYdoc';

describe('GetYdoc', () => {
beforeAll(() => loadFixtures(fixtures));
afterAll(() => unloadFixtures(fixtures));

it('should get the specified doc', async () => {
expect(
await gql`
{
GetYdoc(id: "foo") {
data
versions {
createdAt
snapshot
}
}
}
`({}, { user: { id: 'test', appId: 'test' } })
).toMatchSnapshot();
});

it('should return empty versions when there is none', async () => {
expect(
await gql`
{
GetYdoc(id: "foo2") {
data
versions {
createdAt
snapshot
}
}
}
`({}, { user: { id: 'test', appId: 'test' } })
).toMatchSnapshot();
});
});
40 changes: 40 additions & 0 deletions src/graphql/queries/__tests__/__snapshots__/GetYdoc.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GetYdoc should get the specified doc 1`] = `
Object {
"data": Object {
"GetYdoc": Object {
"data": "mock data1",
"versions": Array [
Object {
"createdAt": "2023-09-07T08:14:14.005Z",
"snapshot": "mock snapshot1",
},
Object {
"createdAt": "2023-09-07T08:16:45.613Z",
"snapshot": "mock snapshot2",
},
Object {
"createdAt": "2023-09-07T08:18:32.467Z",
"snapshot": "mock snapshot3",
},
Object {
"createdAt": "2023-09-07T08:18:49.500Z",
"snapshot": "mock snapshot4",
},
],
},
},
}
`;

exports[`GetYdoc should return empty versions when there is none 1`] = `
Object {
"data": Object {
"GetYdoc": Object {
"data": "mock data2",
"versions": Array [],
},
},
}
`;
2 changes: 2 additions & 0 deletions src/graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import GetArticle from './queries/GetArticle';
import GetReply from './queries/GetReply';
import GetUser from './queries/GetUser';
import GetCategory from './queries/GetCategory';
import GetYdoc from './queries/GetYdoc';
import ListArticles from './queries/ListArticles';
import ListReplies from './queries/ListReplies';
import ListCategories from './queries/ListCategories';
Expand Down Expand Up @@ -41,6 +42,7 @@ export default new GraphQLSchema({
GetReply,
GetUser,
GetCategory,
GetYdoc,
ListArticles,
ListReplies,
ListCategories,
Expand Down
Loading