Skip to content

Commit

Permalink
fix(graphql): 补充简单的 errors 字段处理机制
Browse files Browse the repository at this point in the history
  • Loading branch information
chuan6 committed Sep 30, 2018
1 parent b54c1ad commit e1ca6ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/SDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export class SDK {
}
}

graph<T extends object>(query: string, variables: Variables, withHeaders: true): Observable<T & { headers: Headers }>
graph<T extends object>(query: string, variables: Variables, withHeaders: false): Observable<T>
graph<T extends object>(query: string): Observable<T>
graph<T extends object>(query: string, variables?: Variables, withHeaders: boolean = false) {
if (this.graphQLClientOption == null) {
throw Error('GraphQL server should be specified.')
Expand All @@ -77,11 +80,12 @@ export class SDK {
{ ...this.graphQLClientOption, includeHeaders: true }
)
.map(({ headers, body }) => {
if (withHeaders) {
const data: object = body.data
return ({ ...data, headers: headers })
const { errors, data } = body
if (errors) {
const errmsg = errors.map(({ message }) => `${message}`)
throw new Error(errmsg.join('\n'))
}
return body.data
return withHeaders ? { ...(data! as any), headers } : data!
})
}

Expand Down
15 changes: 12 additions & 3 deletions src/utils/internalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ export interface GraphQLRequestContext {
variables?: Variables
}

export interface GraphQLResponse<T = any> {
data: T
errors?: Error[]
// see: http://facebook.github.io/graphql/June2018/#sec-Errors
export type GraphQLError = {
message: string
path?: Array<string | number>
locations?: Array<{ line: number, column: number }>
extensions?: { [key: string ]: any }
}

// see: http://facebook.github.io/graphql/June2018/#sec-Response-Format
export interface GraphQLResponse<T = { [key: string]: any }> {
data?: T | null // see: http://facebook.github.io/graphql/June2018/#sec-Data
errors?: GraphQLError[] // see: http://facebook.github.io/graphql/June2018/#sec-Errors
extensions?: any
status: number
[key: string]: any
Expand Down

0 comments on commit e1ca6ed

Please sign in to comment.