diff --git a/src/SDK.ts b/src/SDK.ts index c857fc9f2..256a57e18 100644 --- a/src/SDK.ts +++ b/src/SDK.ts @@ -60,6 +60,9 @@ export class SDK { } } + graph(query: string, variables: Variables, withHeaders: true): Observable + graph(query: string, variables: Variables, withHeaders: false): Observable + graph(query: string): Observable graph(query: string, variables?: Variables, withHeaders: boolean = false) { if (this.graphQLClientOption == null) { throw Error('GraphQL server should be specified.') @@ -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! }) } diff --git a/src/utils/internalTypes.ts b/src/utils/internalTypes.ts index aaa100868..2d76fa4f5 100644 --- a/src/utils/internalTypes.ts +++ b/src/utils/internalTypes.ts @@ -64,9 +64,18 @@ export interface GraphQLRequestContext { variables?: Variables } -export interface GraphQLResponse { - data: T - errors?: Error[] +// see: http://facebook.github.io/graphql/June2018/#sec-Errors +export type GraphQLError = { + message: string + path?: Array + locations?: Array<{ line: number, column: number }> + extensions?: { [key: string ]: any } +} + +// see: http://facebook.github.io/graphql/June2018/#sec-Response-Format +export interface GraphQLResponse { + 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