Skip to content

Commit

Permalink
Merge pull request #77 from mattiasewers/master
Browse files Browse the repository at this point in the history
fix query types
  • Loading branch information
elie222 authored Aug 13, 2019
2 parents d1056ec + 092cf3d commit 7a2f11e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { observable, action } from "mobx"
export type CaseHandlers<T, R> = {
loading(): R
error(error: any): R
data(data: T): R
data(data: { [key: string]: T }): R
}

export type FetchPolicy =
Expand All @@ -25,14 +25,14 @@ export interface QueryOptions {

export class Query<T = unknown> implements PromiseLike<T> {
@observable loading = false
@observable.ref data: T | undefined = undefined
@observable.ref data: { [key: string]: T } | undefined = undefined
@observable error: any = undefined

public query: string
public promise!: Promise<T>
public promise!: Promise<{ [key: string]: T }>
private fetchPolicy: FetchPolicy
private cacheKey: string
private onResolve!: (data: T) => void
private onResolve!: (data: { [key: string]: T }) => void
private onReject!: (error: any) => void

constructor(
Expand Down Expand Up @@ -81,7 +81,7 @@ export class Query<T = unknown> implements PromiseLike<T> {
}

private initPromise() {
const promise = new Promise<T>((resolve, reject) => {
const promise = new Promise<{ [key: string]: T }>((resolve, reject) => {
this.onResolve = resolve
this.onReject = reject
})
Expand Down Expand Up @@ -110,11 +110,11 @@ export class Query<T = unknown> implements PromiseLike<T> {
} else {
try {
this.loading = false
const normalized: { [key: string]: any } = {}
const normalized: { [key: string]: T } = {}
Object.keys(data).forEach(key => {
normalized[key] = this.store.merge(data[key])
})
this.data = normalized as T
this.data = normalized
this.onResolve(this.data!)
} catch (e) {
this.onFailure(e)
Expand All @@ -128,7 +128,7 @@ export class Query<T = unknown> implements PromiseLike<T> {
if (this.onReject) this.onReject(error)
}

refetch = (): Promise<T> => {
refetch = (): Promise<{ [key: string]: T }> => {
return Promise.resolve().then(() => {
if (this.loading) return this.currentPromise()
this.initPromise()
Expand Down

0 comments on commit 7a2f11e

Please sign in to comment.