Skip to content

Instantly share code, notes, and snippets.

@duaneking
Created October 18, 2021 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duaneking/cce5df1856b83be6b4c3395583f281da to your computer and use it in GitHub Desktop.
Save duaneking/cce5df1856b83be6b4c3395583f281da to your computer and use it in GitHub Desktop.
JS h2o widget - hydrate initialized
/*
MIT License
Copyright (c) 2021 Duane F. King (https://www.duanefking.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import * as gql from 'gql-query-builder'
import { Entity } from 'sourced';
import { v4 as uuidv4 } from 'uuid';
/* Shim to collect data from entity for mutation. */
class EntityAsGraphQLMutationResult {
constructor(operation, varibles, fields) {
this.operation = operation;
this.varibles = varibles;
this.fields = [...fields];
}
}
/* Entity object */
class Widget extends Entity {
constructor(snapshot, events) {
super()
this.id = uuidv4();
this.price = 0;
this.literal_name = 'Name here as literal because js';
this.string_name = String('Name here as String() because js');
this.cost = 33.33;
this.wtf = false;
this.rehydrate(snapshot, events)
}
init(param) {
this.id = param.id;
this.digest('init', param);
this.emit('initialized', param, this);
}
}
/* Function: Use reflection to extract the Entity objects fields for a mutation. */
function reflect_and_initialize(entity, ignoreFields){
var entityKeys = Reflect.ownKeys(entity);
var filtered = entityKeys.filter(
function(value, index, arr){
return value !== undefined && !ignoreFields.has( value );
}
);
var varibles = []
for (const keyName of filtered) {
varibles[keyName] = entity[keyName];
}
const op = entity.constructor.name.toLowerCase() + '.initialized';
return new EntityAsGraphQLMutationResult(op, varibles, filtered);
}
// Create Entity Model object
var widget = new Widget();
// gnore fields on object we know is part of the entity, not the model.
let fromEntity = new Set( ['_events', '_eventsCount', 'eventsToEmit', 'newEvents', 'replaying', 'snapshotVersion', 'timestamp', 'version'] );
// Use reflection to extract the Entity objects fields for a mutation.
var roi = reflect_and_initialize(widget, fromEntity);
// Generate mutation using data from reflection.
const query = gql.mutation({ operation: roi.operation, variables: roi.varibles, fields: roi.fields })
// Log
console.log('GraphQL query of Entity type Widget:')
console.log(query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment