cjoudrey / graphql-schema-linter

Validate GraphQL schema definitions against a set of rules
MIT License
694 stars 62 forks source link

Cannot read property 0 on undefined :) #188

Closed allexysleeps closed 5 years ago

allexysleeps commented 5 years ago

Hi, guys, I'm getting the following classic error :)

TypeError: Cannot read property '0' of undefined

Error is happening here:

function groupErrorsBySchemaFilePath(errors, schemaSourceMap) {
  return errors.reduce(function (groupedErrors, error) {
    var path = schemaSourceMap.getOriginalPathForLine(error.locations[0].line);

    var offsetForPath = schemaSourceMap.getOffsetForPath(path);
    error.locations[0].line = error.locations[0].line - offsetForPath.startLine + 1;

    groupedErrors[path] = groupedErrors[path] || [];
    groupedErrors[path].push(error);

    return groupedErrors;
  }, {});
}

Here is the error that falls

{ GraphQLError: Query root type must be provided.
    at SchemaValidationContext.reportError (/home/alexshulev/Development/tp/digital-api-schema/digital-api-root-schema/node_modules/graphql/type/validate.js:90:19)
    at validateRootTypes (/home/alexshulev/Development/tp/digital-api-schema/digital-api-root-schema/node_modules/graphql/type/validate.js:109:13)
    at validateSchema (/home/alexshulev/Development/tp/digital-api-schema/digital-api-root-schema/node_modules/graphql/type/validate.js:52:3)
    at validateSchemaDefinition (/home/alexshulev/Development/tp/digital-api-schema/digital-api-root-schema/node_modules/graphql-schema-linter/lib/validator.js:56:48)
    at run (/home/alexshulev/Development/tp/digital-api-schema/digital-api-root-schema/node_modules/graphql-schema-linter/lib/runner.js:66:56)
    at Object.<anonymous> (/home/alexshulev/Development/tp/digital-api-schema/digital-api-root-schema/node_modules/graphql-schema-linter/lib/cli.js:15:32)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
  message: 'Query root type must be provided.',
  ruleName: 'invalid-graphql-schema' }
cjoudrey commented 5 years ago

Thanks for the bug report, I'll take a look shortly.

What version are you running?

Also, could you share your .graphql schema so that I can reproduce the bug?

Thanks! 😃

allexysleeps commented 5 years ago

Hi @cjoudrey. I'm running "0.2.1"

I guess it would work for schema files without type Query

interface Address {
  id: String
  line1: String
  line2: String
}

type BaseAddress implements Address {
  id: String
  line1: String
  line2: String
}

I've reproduced it with the following schema

cjoudrey commented 5 years ago

I'll take a look at fixing the bug, it shouldn't be crashing on you. 😄

That said, the error that it is returning (and will continue to return after the bug is fixed) is normal.

graphql-schema-linter expects to be passed a valid GraphQL schema.

The schema you pasted above isn't technically valid according to the spec:

The query root operation type must be provided and must be an Object type.

There may be a way to improve this behaviour by having graphql-schema-linter attempt to lint the schema anyway instead of only returning that error, but I haven't looked into that.

cjoudrey commented 5 years ago

I just realized this is a dupe of #172. I'm going to close this one for now. Thanks for the reminder though.

cjoudrey commented 5 years ago

Fixed: https://github.com/cjoudrey/graphql-schema-linter/pull/189 😄

allexysleeps commented 4 years ago

Hi, @cjoudrey. Sorry, I forgot to answer.

Thank you for the fix.