Skip to content

Commit

Permalink
Update logic to handle optional relations to documents
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam committed Oct 18, 2023
1 parent 8dc96e5 commit fa6de9a
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 247 deletions.
58 changes: 56 additions & 2 deletions packages/devtools/test/__snapshots__/format-runtime.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Runtime format Note model definition with views 1`] = `
exports[`Runtime format NoteBook with notes definition with enum and views 1`] = `
{
"accountData": {
"noteBookList": {
"name": "NoteBook",
"type": "connection",
},
"noteList": {
"name": "Note",
"type": "connection",
Expand All @@ -22,13 +26,32 @@ exports[`Runtime format Note model definition with views 1`] = `
},
"id": "NoteID",
},
"NoteBook": {
"accountRelation": {
"type": "list",
},
"id": "NoteBookID",
},
},
"objects": {
"Note": {
"author": {
"type": "view",
"viewType": "documentAccount",
},
"noteBook": {
"relation": {
"model": "NoteBookID",
"property": "noteBookID",
"source": "document",
},
"type": "view",
"viewType": "relation",
},
"noteBookID": {
"required": false,
"type": "streamid",
},
"status": {
"refName": "NoteStatus",
"refType": "enum",
Expand All @@ -40,7 +63,38 @@ exports[`Runtime format Note model definition with views 1`] = `
"type": "string",
},
"title": {
"indexed": true,
"required": true,
"type": "string",
},
"version": {
"type": "view",
"viewType": "documentVersion",
},
},
"NoteBook": {
"author": {
"type": "view",
"viewType": "documentAccount",
},
"notes": {
"relation": {
"model": "NoteID",
"property": "noteBookID",
"source": "queryConnection",
},
"type": "view",
"viewType": "relation",
},
"notesCount": {
"relation": {
"model": "NoteID",
"property": "noteBookID",
"source": "queryCount",
},
"type": "view",
"viewType": "relation",
},
"title": {
"required": true,
"type": "string",
},
Expand Down
8 changes: 4 additions & 4 deletions packages/devtools/test/composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
loadPostSchemaWithComments,
postSchema,
profilesSchema,
noteSchema,
noteBookSchema,
ratingSchema,
} from '@composedb/test-schemas'
import type { ModelDefinition } from '@composedb/types'
Expand Down Expand Up @@ -557,11 +557,11 @@ describe('composite', () => {
test('with indices', async () => {
const postComposite = await Composite.create({ ceramic, schema: postSchema })
const ratingComposite = await Composite.create({ ceramic, schema: ratingSchema })
const noteComposite = await Composite.create({ ceramic, schema: noteSchema })
const mergedComposite = postComposite.merge([ratingComposite, noteComposite]).toJSON()
const noteBookComposite = await Composite.create({ ceramic, schema: noteBookSchema })
const mergedComposite = postComposite.merge([ratingComposite, noteBookComposite]).toJSON()
expect(mergedComposite).toHaveProperty('indices.Post')
expect(mergedComposite).toHaveProperty('indices.Rating')
expect(mergedComposite).toHaveProperty('indices.Note')
expect(mergedComposite).toHaveProperty('indices.NoteBook')
})
})
})
Expand Down
18 changes: 14 additions & 4 deletions packages/devtools/test/format-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import {
createCommentSchemaWithPost,
createNoteSchemaWithNoteBook,
loadNoteBookSchemaWithNotes,
loadPostSchemaWithComments,
noteSchema,
noteBookSchema,
postSchema,
profilesSchema,
socialSchema,
Expand Down Expand Up @@ -44,9 +46,17 @@ describe('Runtime format', () => {
expect(runtime).toMatchSnapshot()
})

test('Note model definition with views', () => {
const noteDefinition = mockDefinitionFromSchema(noteSchema)
const runtime = createRuntimeDefinition(noteDefinition)
test('NoteBook with notes definition with enum and views', () => {
const noteBookDefinition = mockDefinitionFromSchema(noteBookSchema)
const noteDefinition = mockDefinitionFromSchema(
createNoteSchemaWithNoteBook('NoteBookID'),
noteBookDefinition.models,
)
const noteBookWithNotesDefinition = mockDefinitionFromSchema(
loadNoteBookSchemaWithNotes('NoteBookID', 'NoteID'),
noteDefinition.models,
)
const runtime = createRuntimeDefinition(noteBookWithNotesDefinition)
expect(runtime).toMatchSnapshot()
})

Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ class SchemaBuilder {
type,
args: connectionArgs,
resolve: async (doc, _, ctx): Promise<ModelInstanceDocument | null> => {
return await ctx.loadDoc(doc.content[key] as string)
const id = doc.content[key]
return id ? await ctx.loadDoc(id) : null
},
}
case 'enum':
Expand Down
8 changes: 0 additions & 8 deletions packages/runtime/test/__snapshots__/runtime.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ exports[`runtime can create a document using extra scalars 1`] = `
}
`;

exports[`runtime create a document with enum 1`] = `
{
"status": "DEFAULT",
"text": "Test node contents",
"title": "A test note",
}
`;

exports[`runtime create and query post with comments 1`] = `
"type Query {
"""Fetches objects given their IDs"""
Expand Down
219 changes: 0 additions & 219 deletions packages/runtime/test/__snapshots__/schema.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -317,225 +317,6 @@ input UpdateOptionsInput {
}"
`;

exports[`schema note 1`] = `
"type Query {
"""Fetches objects given their IDs"""
nodes(
"""The IDs of objects"""
ids: [ID!]!
): [Node]!
"""Fetches an object given its ID"""
node(
"""The ID of an object"""
id: ID!
): Node
"""Account currently authenticated on the Ceramic instance, if set"""
viewer: CeramicAccount
noteIndex(
"""Returns the items in the list that come after the specified cursor."""
after: String
"""Returns the first n items from the list."""
first: Int
"""Returns the items in the list that come before the specified cursor."""
before: String
"""Returns the last n items from the list."""
last: Int
filters: NoteFiltersInput
sorting: NoteSortingInput
): NoteConnection
noteCount(filters: NoteFiltersInput): Int!
}
"""An object with an ID"""
interface Node {
"""The id of the object."""
id: ID!
}
type CeramicAccount implements Node {
"""Globally unique identifier of the account (DID string)"""
id: ID!
"""
Whether the Ceramic instance is currently authenticated with this account or not
"""
isViewer: Boolean!
noteList(
"""Returns the items in the list that come after the specified cursor."""
after: String
"""Returns the first n items from the list."""
first: Int
"""Returns the items in the list that come before the specified cursor."""
before: String
"""Returns the last n items from the list."""
last: Int
filters: NoteFiltersInput
sorting: NoteSortingInput
): NoteConnection
noteListCount(filters: NoteFiltersInput): Int!
}
"""A connection to a list of items."""
type NoteConnection {
"""Information to aid in pagination."""
pageInfo: PageInfo!
"""A list of edges."""
edges: [NoteEdge]
}
"""Information about pagination in a connection."""
type PageInfo {
"""When paginating forwards, are there more items?"""
hasNextPage: Boolean!
"""When paginating backwards, are there more items?"""
hasPreviousPage: Boolean!
"""When paginating backwards, the cursor to continue."""
startCursor: String
"""When paginating forwards, the cursor to continue."""
endCursor: String
}
"""An edge in a connection."""
type NoteEdge {
"""The item at the end of the edge"""
node: Note
"""A cursor for use in pagination"""
cursor: String!
}
type Note implements Node {
id: ID!
status: NoteStatus
title: String!
text: String!
"""Account controlling the document"""
author: CeramicAccount!
"""Current version of the document"""
version: CeramicCommitID!
}
enum NoteStatus {
DEFAULT
IMPORTANT
ARCHIVE
}
"""A Ceramic Commit ID"""
scalar CeramicCommitID @specifiedBy(url: "https://github.com/ceramicnetwork/CIP/blob/main/CIPs/CIP-59/CIP-59.md#commitid")
input NoteFiltersInput {
where: NoteObjectFilterInput
and: [NoteFiltersInput!]
or: [NoteFiltersInput!]
not: NoteFiltersInput
}
input NoteObjectFilterInput {
title: StringValueFilterInput
}
input StringValueFilterInput {
isNull: Boolean
equalTo: String
notEqualTo: String
in: [String!]
notIn: [String!]
lessThan: String
lessThanOrEqualTo: String
greaterThan: String
greaterThanOrEqualTo: String
}
input NoteSortingInput {
title: SortOrder
}
enum SortOrder {
ASC
DESC
}
type Mutation {
createNote(input: CreateNoteInput!): CreateNotePayload
updateNote(input: UpdateNoteInput!): UpdateNotePayload
}
type CreateNotePayload {
"""Fetches an object given its ID"""
node(
"""The ID of an object"""
id: ID!
): Node
"""Account currently authenticated on the Ceramic instance, if set"""
viewer: CeramicAccount
document: Note!
clientMutationId: String
}
input CreateNoteInput {
content: NoteInput!
clientMutationId: String
}
input NoteInput {
status: NoteStatus
title: String!
text: String!
}
type UpdateNotePayload {
"""Fetches an object given its ID"""
node(
"""The ID of an object"""
id: ID!
): Node
"""Account currently authenticated on the Ceramic instance, if set"""
viewer: CeramicAccount
document: Note!
clientMutationId: String
}
input UpdateNoteInput {
id: ID!
content: PartialNoteInput!
options: UpdateOptionsInput
clientMutationId: String
}
input PartialNoteInput {
status: NoteStatus
title: String
text: String
}
input UpdateOptionsInput {
"""
Fully replace the document contents instead of performing a shallow merge
"""
replace: Boolean = false
"""Only perform mutation if the document matches the provided version"""
version: CeramicCommitID
}"
`;

exports[`schema post and comments 1`] = `
"type Query {
"""Fetches objects given their IDs"""
Expand Down
Loading

0 comments on commit fa6de9a

Please sign in to comment.