diff --git a/graphql/schema/gqlschema.go b/graphql/schema/gqlschema.go index 5ae4b11dce9..54c08d5d43c 100644 --- a/graphql/schema/gqlschema.go +++ b/graphql/schema/gqlschema.go @@ -1726,7 +1726,7 @@ func addAggregationResultType(schema *ast.Schema, defn *ast.Definition) { func addGetQuery(schema *ast.Schema, defn *ast.Definition, generateSubscription bool) { hasIDField := hasID(defn) hasXIDField := hasXID(defn) - if !hasIDField && !hasXIDField { + if !hasIDField && (defn.Kind == "INTERFACE" || !hasXIDField) { return } @@ -1749,12 +1749,12 @@ func addGetQuery(schema *ast.Schema, defn *ast.Definition, generateSubscription }, }) } - if hasXIDField { - name := xidTypeFor(defn) + if hasXIDField && defn.Kind != "INTERFACE" { + name, dtype := xidTypeFor(defn) qry.Arguments = append(qry.Arguments, &ast.ArgumentDefinition{ Name: name, Type: &ast.Type{ - NamedType: "String", + NamedType: dtype, NonNull: !hasIDField, }, }) @@ -2363,13 +2363,13 @@ func idTypeFor(defn *ast.Definition) string { return "ID" } -func xidTypeFor(defn *ast.Definition) string { +func xidTypeFor(defn *ast.Definition) (string, string) { for _, fld := range defn.Fields { if hasIDDirective(fld) { - return fld.Name + return fld.Name, fld.Type.Name() } } - return "" + return "", "" } func appendIfNotNull(errs []*gqlerror.Error, err *gqlerror.Error) gqlerror.List { diff --git a/graphql/schema/testdata/schemagen/input/interface-with-id-directive-and-ID-field.graphql b/graphql/schema/testdata/schemagen/input/interface-with-id-directive-and-ID-field.graphql new file mode 100644 index 00000000000..5f3952dae3f --- /dev/null +++ b/graphql/schema/testdata/schemagen/input/interface-with-id-directive-and-ID-field.graphql @@ -0,0 +1,5 @@ +interface Student { + rollNo: String! @id + regNo: ID! + name: String! +} \ No newline at end of file diff --git a/graphql/schema/testdata/schemagen/output/interface-with-id-directive-and-ID-field.graphql b/graphql/schema/testdata/schemagen/output/interface-with-id-directive-and-ID-field.graphql new file mode 100755 index 00000000000..c7a1d34e10f --- /dev/null +++ b/graphql/schema/testdata/schemagen/output/interface-with-id-directive-and-ID-field.graphql @@ -0,0 +1,356 @@ +####################### +# Input Schema +####################### + +interface Student { + rollNo: String! @id + regNo: ID! + name: String! +} + +####################### +# Extended Definitions +####################### + +""" +The Int64 scalar type represents a signed 64‐bit numeric non‐fractional value. +Int64 can represent values in range [-(2^63),(2^63 - 1)]. +""" +scalar Int64 + +""" +The DateTime scalar type represents date and time as a string in RFC3339 format. +For example: "1985-04-12T23:20:50.52Z" represents 20 minutes and 50.52 seconds after the 23rd hour of April 12th, 1985 in UTC. +""" +scalar DateTime + +input IntRange{ + min: Int! + max: Int! +} + +input FloatRange{ + min: Float! + max: Float! +} + +input Int64Range{ + min: Int64! + max: Int64! +} + +input DateTimeRange{ + min: DateTime! + max: DateTime! +} + +input StringRange{ + min: String! + max: String! +} + +enum DgraphIndex { + int + int64 + float + bool + hash + exact + term + fulltext + trigram + regexp + year + month + day + hour + geo +} + +input AuthRule { + and: [AuthRule] + or: [AuthRule] + not: AuthRule + rule: String +} + +enum HTTPMethod { + GET + POST + PUT + PATCH + DELETE +} + +enum Mode { + BATCH + SINGLE +} + +input CustomHTTP { + url: String! + method: HTTPMethod! + body: String + graphql: String + mode: Mode + forwardHeaders: [String!] + secretHeaders: [String!] + introspectionHeaders: [String!] + skipIntrospection: Boolean +} + +type Point { + longitude: Float! + latitude: Float! +} + +input PointRef { + longitude: Float! + latitude: Float! +} + +input NearFilter { + distance: Float! + coordinate: PointRef! +} + +input PointGeoFilter { + near: NearFilter + within: WithinFilter +} + +type PointList { + points: [Point!]! +} + +input PointListRef { + points: [PointRef!]! +} + +type Polygon { + coordinates: [PointList!]! +} + +input PolygonRef { + coordinates: [PointListRef!]! +} + +type MultiPolygon { + polygons: [Polygon!]! +} + +input MultiPolygonRef { + polygons: [PolygonRef!]! +} + +input WithinFilter { + polygon: PolygonRef! +} + +input ContainsFilter { + point: PointRef + polygon: PolygonRef +} + +input IntersectsFilter { + polygon: PolygonRef + multiPolygon: MultiPolygonRef +} + +input PolygonGeoFilter { + near: NearFilter + within: WithinFilter + contains: ContainsFilter + intersects: IntersectsFilter +} + +input GenerateQueryParams { + get: Boolean + query: Boolean + password: Boolean + aggregate: Boolean +} + +input GenerateMutationParams { + add: Boolean + update: Boolean + delete: Boolean +} + +directive @hasInverse(field: String!) on FIELD_DEFINITION +directive @search(by: [DgraphIndex!]) on FIELD_DEFINITION +directive @dgraph(type: String, pred: String) on OBJECT | INTERFACE | FIELD_DEFINITION +directive @id on FIELD_DEFINITION +directive @withSubscription on OBJECT | INTERFACE +directive @secret(field: String!, pred: String) on OBJECT | INTERFACE +directive @auth( + password: AuthRule + query: AuthRule, + add: AuthRule, + update: AuthRule, + delete: AuthRule) on OBJECT | INTERFACE +directive @custom(http: CustomHTTP, dql: String) on FIELD_DEFINITION +directive @remote on OBJECT | INTERFACE | UNION | INPUT_OBJECT | ENUM +directive @cascade(fields: [String]) on FIELD +directive @lambda on FIELD_DEFINITION +directive @cacheControl(maxAge: Int!) on QUERY +directive @generate( + query: GenerateQueryParams, + mutation: GenerateMutationParams, + subscription: Boolean) on OBJECT | INTERFACE + +input IntFilter { + eq: Int + le: Int + lt: Int + ge: Int + gt: Int + between: IntRange +} + +input Int64Filter { + eq: Int64 + le: Int64 + lt: Int64 + ge: Int64 + gt: Int64 + between: Int64Range +} + +input FloatFilter { + eq: Float + le: Float + lt: Float + ge: Float + gt: Float + between: FloatRange +} + +input DateTimeFilter { + eq: DateTime + le: DateTime + lt: DateTime + ge: DateTime + gt: DateTime + between: DateTimeRange +} + +input StringTermFilter { + allofterms: String + anyofterms: String +} + +input StringRegExpFilter { + regexp: String +} + +input StringFullTextFilter { + alloftext: String + anyoftext: String +} + +input StringExactFilter { + eq: String + in: [String] + le: String + lt: String + ge: String + gt: String + between: StringRange +} + +input StringHashFilter { + eq: String + in: [String] +} + +####################### +# Generated Types +####################### + +type DeleteStudentPayload { + student(filter: StudentFilter, order: StudentOrder, first: Int, offset: Int): [Student] + msg: String + numUids: Int +} + +type StudentAggregateResult { + count: Int + rollNoMin: String + rollNoMax: String + nameMin: String + nameMax: String +} + +type UpdateStudentPayload { + student(filter: StudentFilter, order: StudentOrder, first: Int, offset: Int): [Student] + numUids: Int +} + +####################### +# Generated Enums +####################### + +enum StudentHasFilter { + rollNo + name +} + +enum StudentOrderable { + rollNo + name +} + +####################### +# Generated Inputs +####################### + +input StudentFilter { + rollNo: StringHashFilter + regNo: [ID!] + has: StudentHasFilter + and: [StudentFilter] + or: [StudentFilter] + not: StudentFilter +} + +input StudentOrder { + asc: StudentOrderable + desc: StudentOrderable + then: StudentOrder +} + +input StudentPatch { + name: String +} + +input StudentRef { + regNo: ID + rollNo: String @id +} + +input UpdateStudentInput { + filter: StudentFilter! + set: StudentPatch + remove: StudentPatch +} + +####################### +# Generated Query +####################### + +type Query { + getStudent(regNo: ID): Student + queryStudent(filter: StudentFilter, order: StudentOrder, first: Int, offset: Int): [Student] + aggregateStudent(filter: StudentFilter): StudentAggregateResult +} + +####################### +# Generated Mutations +####################### + +type Mutation { + updateStudent(input: UpdateStudentInput!): UpdateStudentPayload + deleteStudent(filter: StudentFilter!): DeleteStudentPayload +} + diff --git a/graphql/schema/testdata/schemagen/output/interface-with-id-directive.graphql b/graphql/schema/testdata/schemagen/output/interface-with-id-directive.graphql index 5a090ffe1f6..b1bead6ceb9 100755 --- a/graphql/schema/testdata/schemagen/output/interface-with-id-directive.graphql +++ b/graphql/schema/testdata/schemagen/output/interface-with-id-directive.graphql @@ -452,7 +452,6 @@ input UpdateLibraryInput { ####################### type Query { - getLibraryItem(refID: String!): LibraryItem queryLibraryItem(filter: LibraryItemFilter, order: LibraryItemOrder, first: Int, offset: Int): [LibraryItem] aggregateLibraryItem(filter: LibraryItemFilter): LibraryItemAggregateResult getBook(refID: String!): Book