-
Notifications
You must be signed in to change notification settings - Fork 8
add createEntityPublic #240
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,100 @@ | ||||
import { type GeoSmartAccount, Graph, Id, type PropertiesParam, type RelationsParam } from '@graphprotocol/grc-20'; | ||||
import type { Entity } from '@graphprotocol/hypergraph'; | ||||
import { Type, store } from '@graphprotocol/hypergraph'; | ||||
import { useQueryClient } from '@tanstack/react-query'; | ||||
import { useSelector } from '@xstate/store/react'; | ||||
import type * as Schema from 'effect/Schema'; | ||||
import { publishOps } from '../publish-ops.js'; | ||||
|
||||
type CreateEntityPublicParams = { | ||||
space: string; | ||||
}; | ||||
|
||||
export function useCreateEntityPublic<const S extends Entity.AnyNoContext>( | ||||
type: S, | ||||
{ space }: CreateEntityPublicParams, | ||||
) { | ||||
const mapping = useSelector(store, (state) => state.context.mapping); | ||||
const queryClient = useQueryClient(); | ||||
|
||||
return async ( | ||||
data: Readonly<Schema.Schema.Type<Entity.Insert<S>>>, | ||||
{ walletClient }: { walletClient: GeoSmartAccount }, | ||||
// TODO: return the entity with this type: Promise<Entity.Entity<S>> | ||||
) => { | ||||
try { | ||||
// @ts-expect-error TODO should use the actual type instead of the name in the mapping | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider refining the type definitions so that the actual type can be used instead of relying on @ts-expect-error. This will improve type safety and code clarity.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||
const typeName = type.name; | ||||
const mappingEntry = mapping?.[typeName]; | ||||
if (!mappingEntry) { | ||||
throw new Error(`Mapping entry for ${typeName} not found`); | ||||
} | ||||
|
||||
const fields = type.fields; | ||||
const values: PropertiesParam = []; | ||||
for (const [key, value] of Object.entries(mappingEntry.properties || {})) { | ||||
let serializedValue: string = data[key]; | ||||
if (fields[key] === Type.Checkbox) { | ||||
serializedValue = Graph.serializeCheckbox(data[key]); | ||||
} else if (fields[key] === Type.Date) { | ||||
serializedValue = Graph.serializeDate(data[key]); | ||||
} else if (fields[key] === Type.Point) { | ||||
serializedValue = Graph.serializePoint(data[key]); | ||||
} else if (fields[key] === Type.Number) { | ||||
serializedValue = Graph.serializeNumber(data[key]); | ||||
} | ||||
|
||||
values.push({ | ||||
property: Id.Id(value), | ||||
value: serializedValue, | ||||
}); | ||||
} | ||||
|
||||
const relations: RelationsParam = {}; | ||||
for (const [key, relationId] of Object.entries(mappingEntry.relations || {})) { | ||||
const toIds: { toEntity: Id.Id }[] = []; | ||||
|
||||
if (data[key]) { | ||||
// @ts-expect-error - TODO: fix the types error | ||||
for (const entity of data[key]) { | ||||
if (typeof entity === 'string') { | ||||
toIds.push({ toEntity: Id.Id(entity) }); | ||||
} else { | ||||
toIds.push({ toEntity: Id.Id(entity.id) }); | ||||
} | ||||
} | ||||
relations[Id.Id(relationId)] = toIds; | ||||
} | ||||
} | ||||
|
||||
const { ops } = Graph.createEntity({ | ||||
types: mappingEntry.typeIds, | ||||
id: data.id, | ||||
values, | ||||
relations, | ||||
}); | ||||
|
||||
const { cid, txResult } = await publishOps({ | ||||
ops, | ||||
space, | ||||
name: `Create entity ${data.id}`, | ||||
walletClient, | ||||
network: 'TESTNET', | ||||
}); | ||||
// TODO: temporary fix until we get the information from the API when a transaction is confirmed | ||||
await new Promise((resolve) => setTimeout(resolve, 2000)); | ||||
queryClient.invalidateQueries({ | ||||
queryKey: [ | ||||
'hypergraph-public-entities', | ||||
// @ts-expect-error - TODO: find a better way to access the type.name | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review the access to type.name and update the typing accordingly to avoid the need for an @ts-expect-error suppression. This change can enhance maintainability and type correctness.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||
type.name, | ||||
space, | ||||
], | ||||
}); | ||||
return { success: true, cid, txResult }; | ||||
} catch (error) { | ||||
console.error(error); | ||||
return { success: false, error: 'Failed to create entity' }; | ||||
} | ||||
}; | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider addressing the underlying type issues in the createEntity invocation so that the @ts-expect-error suppression can be removed. Improving type safety here will increase code robustness.
Copilot uses AI. Check for mistakes.