How to create this GraphQL compatible server to use with RxDB? #9599
-
Hello! Can you please explain how to apply this backend logic with Hasura GraphQL Engine for replication conflict resolution? > https://rxdb.info/replication-graphql.html Creating a compatible GraphQL Server At the server-side, there must exist an endpoint which returns newer rows when the last checkpoint is used as input. For example lets say you create a Query pullHuman which returns a list of document writes that happened after the given checkpoint. For the push-replication, you also need a Mutation pushHuman which lets RxDB update data of documents by sending the previous document state and the new client document state. Also for being able to stream all ongoing events, we need a Subscription called streamHuman. input HumanInput {
id: ID!,
name: String!,
lastName: String!,
updatedAt: Float!,
deleted: Boolean!
}
type Human {
id: ID!,
name: String!,
lastName: String!,
updatedAt: Float!,
deleted: Boolean!
}
input Checkpoint {
id: String!,
updatedAt: Float!
}
type HumanPullBulk {
documents: [Human]!
checkpoint: Checkpoint
}
type Query {
pullHuman(checkpoint: Checkpoint, limit: Int!): HumanPullBulk!
}
input HumanInputPushRow {
assumedMasterState: HeroInputPushRowT0AssumedMasterStateT0
newDocumentState: HeroInputPushRowT0NewDocumentStateT0!
}
type Mutation {
# Returns a list of all conflicts
# If no document write caused a conflict, return an empty list.
pushHuman(rows: [HumanInputPushRow!]): [Human]
}
# headers are used to authenticate the subscriptions
# over websockets.
input Headers {
AUTH_TOKEN: String!;
}
type Subscription {
streamHuman(headers: Headers): HumanPullBulk!
} The GraphQL resolver for the pullHuman would then look like: const rootValue = {
pullHuman: args => {
const minId = args.checkpoint : args.checkpoint.id : '';
const minUpdatedAt = args.checkpoint : args.checkpoint.updatedAt : 0;
// sorted by updatedAt first and the id as second
const sortedDocuments = documents.sort((a, b) => {
if (a.updatedAt > b.updatedAt) return 1;
if (a.updatedAt < b.updatedAt) return -1;
if (a.updatedAt === b.updatedAt) {
if (a.id > b.id) return 1;
if (a.id < b.id) return -1;
else return 0;
}
});
// only return documents newer than the input document
const filterForMinUpdatedAtAndId = sortedDocuments.filter(doc => {
if (doc.updatedAt < minUpdatedAt) return false;
if (doc.updatedAt > minUpdatedAt) return true;
if (doc.updatedAt === minUpdatedAt) {
// if updatedAt is equal, compare by id
if (doc.id > minId) return true;
else return false;
}
});
// only return some documents in one batch
const limitedDocs = filterForMinUpdatedAtAndId.slice(0, args.limit);
// use the last document for the checkpoint
const lastDoc = limitedDocs[limitedDocs.length - 1];
const retCheckpoint = {
id: lastDoc.id,
updatedAt: lastDoc.updatedAt
}
return {
documents: limitedDocs,
checkpoint: retCheckpoint
}
return limited;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@endlessuniverse However, you won't be able to use subscriptions on a Remote Schema, as a current limitation |
Beta Was this translation helpful? Give feedback.
@endlessuniverse
If you intend to use custom GraphQL server (with custom resolvers), you can do so with Remote Schema : doc
However, you won't be able to use subscriptions on a Remote Schema, as a current limitation