@@ -11,6 +11,12 @@ import { type Source } from '../language/source';
1111import { type SourceLocation , getLocation } from '../language/location' ;
1212import { printLocation , printSourceLocation } from '../language/printLocation' ;
1313
14+ const MESSAGE_DETAILS = {
15+ 'Query root type must be provided.' : {
16+ code : 'GQ0001'
17+ }
18+ }
19+
1420/**
1521 * A GraphQLError describes an Error found during the parse, validate, or
1622 * execute phases of performing a GraphQL operation. In addition to a message
@@ -27,6 +33,16 @@ export class GraphQLError extends Error {
2733 */
2834 message : string ;
2935
36+
37+ /**
38+ * A code describing the Error and referenced in the spec.
39+ *
40+ * This code is unique. More details at-
41+ *
42+ * https://spec.graphql.org/June2018/#sec-Errors
43+ */
44+ code : string ;
45+
3046 /**
3147 * An array of { line, column } locations within the source GraphQL document
3248 * which correspond to this error.
@@ -145,6 +161,14 @@ export class GraphQLError extends Error {
145161 enumerable : true ,
146162 writable : true ,
147163 } ,
164+ code : {
165+ value : MESSAGE_DETAILS [ message ] && MESSAGE_DETAILS [ message ] . code ,
166+ // By being enumerable, JSON.stringify will include `message` in the
167+ // resulting output. This ensures that the simplest possible GraphQL
168+ // service adheres to the spec.
169+ enumerable : true ,
170+ writable : true ,
171+ } ,
148172 locations : {
149173 // Coercing falsy values to undefined ensures they will not be included
150174 // in JSON.stringify() when not provided.
@@ -224,7 +248,7 @@ export class GraphQLError extends Error {
224248 * about the error's position in the source.
225249 */
226250export function printError ( error : GraphQLError ) : string {
227- let output = error . message ;
251+ let output = `${ error . code } : $ { error . message } ` ;
228252
229253 if (error.nodes) {
230254 for (const node of error.nodes) {
0 commit comments