-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix #1277 ensure interface has at least 1 concrete type #1280
Changes from 6 commits
47443ad
1c1e089
40a22b4
bea481c
87a5dcc
3078b1b
fb672ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
*/ | ||
|
||
import { | ||
isAbstractType, | ||
isObjectType, | ||
isInterfaceType, | ||
isUnionType, | ||
|
@@ -159,6 +160,8 @@ export class GraphQLSchema { | |
this._implementations[iface.name] = [type]; | ||
} | ||
}); | ||
} else if (isAbstractType(type) && !this._implementations[type.name]) { | ||
this._implementations[type.name] = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. guarantees that |
||
} | ||
}); | ||
} | ||
|
@@ -203,12 +206,6 @@ export class GraphQLSchema { | |
|
||
if (!possibleTypeMap[abstractType.name]) { | ||
const possibleTypes = this.getPossibleTypes(abstractType); | ||
invariant( | ||
Array.isArray(possibleTypes), | ||
`Could not find possible implementing types for ${abstractType.name} ` + | ||
'in schema. Check that schema.types is defined and is an array of ' + | ||
'all possible types in the schema.', | ||
); | ||
possibleTypeMap[abstractType.name] = possibleTypes.reduce( | ||
(map, type) => ((map[type.name] = true), map), | ||
Object.create(null), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,6 +257,9 @@ function validateTypes(context: SchemaValidationContext): void { | |
} else if (isInterfaceType(type)) { | ||
// Ensure fields are valid. | ||
validateFields(context, type); | ||
|
||
// Ensure Interfaces include at least 1 Object type. | ||
validateInterfaces(context, type); | ||
} else if (isUnionType(type)) { | ||
// Ensure Unions include valid member types. | ||
validateUnionMembers(context, type); | ||
|
@@ -355,6 +358,21 @@ function validateObjectInterfaces( | |
}); | ||
} | ||
|
||
function validateInterfaces( | ||
context: SchemaValidationContext, | ||
iface: GraphQLInterfaceType, | ||
): void { | ||
const possibleTypes = context.schema.getPossibleTypes(iface); | ||
|
||
if (possibleTypes.length === 0) { | ||
context.reportError( | ||
`Interface ${iface.name} must be implemented` + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please expand the tests further to include a test which asserts this error occurs? |
||
`by at least one Object type.`, | ||
iface.astNode, | ||
); | ||
} | ||
} | ||
|
||
function validateObjectImplementsInterface( | ||
context: SchemaValidationContext, | ||
object: GraphQLObjectType, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isPossibleType
can no longer throw, so this test is no longer needed